hibernate.cfg.xml - place under root of classpath, override property file (see an example at the end)
hibernate.properties - under root of classpath
org.hibernate.cfg.Configuration - the representation of configurations and is used to obtain SessionFactory
new Configuration()
.configure() - use the default hibernate.cfg.xml under root classpath
.configure("xxx.xml") - configure use the specified configuration file
Add mappings (if not by configuration file)
.addResource("xxx.xml") - add xml mapping file from classpath
.addClass(org.hibernate.sample.EntitySomething.class) - find the xml besides the class
Set properties (if not by configuration file)
.setProperty(..., ...) - sets propertie manually
.setProperties() - set collectively
file "hibernate.properties" under classpath root
set system properties (java -Dproperty=value)
include <property> in hibernate.cfg.xml
Build session factory
SessionFactory factory = cfg.buildSessionFactory()
Basics - using direct connection (if not in JEE application server):
hibernate.connection.driver_class = org.postgresql.Driver
hibernate.connection.url = jdbc:postgresql://localhost/mydatabase
hibernate.connection.username = myuser
hibernate.connection.password = secret
hibernate.dialect = org.hibernate.dialect.PostgreSQL82Dialect
Arbitrary connection property can be passed to connection, for example, "hibernate.connection.charSet"
Dialect in most cases can be recognized.
Pooling:
Use hibernate's own pooling
simply set "hibernate.connection.pool_size"
not recommended for production use
Use third parties pooling
do not set hibernate.connection.pool_size
set third party's pooling options
example for c3po below
hibernate.c3p0.min_size=5
hibernate.c3p0.max_size=20
hibernate.c3p0.timeout=1800
hibernate.c3p0.max_statements=50
Use DataSource registered with JNDI in application server
hibernate.connection.datasource datasource JNDI name
hibernate.jndi.url URL of the JNDI provider (optional)
hibernate.jndi.class class of the JNDI InitialContextFactory (optional)
hibernate.connection.username database user (optional)
hibernate.connection.password database user password (optional)
An example:
hibernate.connection.datasource = java:/comp/env/jdbc/test
hibernate.transaction.factory_class = \
org.hibernate.transaction.JTATransactionFactory
hibernate.transaction.manager_lookup_class = \
org.hibernate.transaction.JBossTransactionManagerLookup
hibernate.dialect = org.hibernate.dialect.PostgreSQL82Dialect
Only some interesting ones discussed, others see reference
hibernate.dialect - can be omitted in most cases and hibernate will detect
hibernate.show_sql - true|false, show all sql to console
hibernate.default_schema - qualify table names with schema
hibernate.session_factory_name - generated factory will be bind to the name, e.g. "jndi/composite/name"
hibernate.max_fetch_depth - (recommend 0-3, 0 disables default outer fetch), for outer join fetch for single-ended association (one-to-one, many-to-one)
Container managed data source: use JDBC connections provided via JNDI that is managed by container. JTA compatible TransactionManager and ResourceManager take care of transaction. Or can use Hibernate Transaction API.
Automatic JNDI binding. Set "hibernate.session_factory_name"
JTA session binding: Hibernate Session automatic bound to JTA transactions.
JMX deployment
(to be further studied)
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<!-- a SessionFactory instance listed as /jndi/name -->
<session-factory
name="java:hibernate/SessionFactory">
<!-- properties -->
<property name="connection.datasource">java:/comp/env/jdbc/MyDB</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="show_sql">false</property>
<property name="transaction.factory_class">
org.hibernate.transaction.JTATransactionFactory
</property>
<property name="jta.UserTransaction">java:comp/UserTransaction</property>
<!-- mapping files -->
<mapping resource="org/hibernate/auction/Item.hbm.xml"/>
<mapping resource="org/hibernate/auction/Bid.hbm.xml"/>
<!-- cache settings -->
<class-cache class="org.hibernate.auction.Item" usage="read-write"/>
<class-cache class="org.hibernate.auction.Bid" usage="read-only"/>
<collection-cache collection="org.hibernate.auction.Item.bids" usage="read-write"/>
</session-factory>
</hibernate-configuration>