Hibenate configuration parameters
Here is a general hibernate session configuration file:
hibernate.cfg.xml
<?xml version="1.0" encoding="utf-8"?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <property name="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</property> <property name="hibernate.use_outer_join">true</property> <property name="hibernate.show_sql">false</property> <!-- cache properties --> <property name="hibernate.cache.use_query_cache">false</property> <property name="hibernate.generate_statistics">false</property> <property name="hibernate.format_sql">true</property> <property name="hibernate.use_sql_comments">true</property> <property name="hibernate.jdbc.batch_size">20</property> <property name="hibernate.cache.use_second_level_cache">true</property> <property name="hibernate.cache.provider_class">net.sf.ehcache.hibernate.EhCacheProvider</property> <property name="net.sf.ehcache.configurationResourceName">/ehcache.xml</property> <!-- mapping files --> <mapping resource="domain.hbm.xml"/> <!--cache entities --> <class-cache class="com.wgu.domain.Attribute" usage="read-write"/> <collection-cache collection="com.wgu.domain.AttributeType.values" usage="read-write"/> </session-factory> </hibernate-configuration>
For more details on the meaning of configuration properties, please rerfer to Hibernate session configuration specification.
Integrate Hibernate with Spring
You may find out there are some critical properties missed in previous configuration file:
hibernate.connection.driver_class | JDBC driver class |
hibernate.connection.url | JDBC URL |
hibernate.connection.username | database user |
hibernate.connection.password | database user password |
hibernate.connection.pool_size | maximum number of pooled connections |
That's because we use hibernate with Spring.
Configure SessionFactory
<bean id="mySessionFactory" lazy-init="true" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="configLocation"> <value>classpath:/hibernate.cfg.xml</value> </property> <property name="hibernateProperties"> <ref bean="hibernateProperties"/> </property> <property name="dataSource"> <ref bean="DataSource"/> </property> <property name="lobHandler"> <ref bean="LobHandler"/> </property> </bean>
Configure Datasource
JDBC Connection Configuration
<bean id="DataSource" lazy-init="true" class="org.apache.commons.dbcp.BasicDataSource" autowire="default" dependency-check="default"> <property name="driverClassName"> <value>oracle.jdbc.driver.OracleDriver </value> </property> <property name="url"> <value>jdbc:oracle:thin:@${db.host}:${db.port}:${db.service} </value> </property> <property name="username"> <value>${db.user}</value> </property> <property name="password"> <value>${db.password}</value> </property> <property name="maxActive"> <value>100</value> </property> <property name="defaultAutoCommit"> <value>false</value> </property> <property name="testOnBorrow"> <value>true</value> </property> <property name="minEvictableIdleTimeMillis"> <value>5000</value> </property> <property name="timeBetweenEvictionRunsMillis"> <value>10000</value> </property> <property name="numTestsPerEvictionRun"> <value>5</value> </property> <property name="validationQuery"> <value>SELECT COUNT(*) FROM DUAL</value> </property> </bean>
Datasource JNDI Configuration
<bean id="hibernateProperties" class="java.util.Properties"> <constructor-arg index="0"> <props> <prop key="hibernate.show_sql">false</prop> <prop key="hibernate.connection.datasource">java:comp/env/jdbc/dataSource</prop> <prop key="hibernate.transaction.manager_lookup_class">org.hibernate.transaction.WeblogicTransactionManagerLookup</prop> </props> </constructor-arg> </bean> <jee:jndi-lookup id="DataSource" jndi-name="dataSource"/>
Configure Hibernate use SSL through JDBC connection
<bean id="dataSource" class="oracle.jdbc.pool.OracleDataSource"> <property name="URL"><value><!-- JDBC URL that specifies SSL connection --></value></property> <!-- other relevant properties, like user and password --> <property name="connectionProperties> <value> oracle.net.ssl_cipher_suites: (ssl_rsa_export_with_rc4_40_md5, ssl_rsa_export_with_des40_cbc_sha) oracle.net.ssl_client_authentication: false oracle.net.ssl_version: 3.0 oracle.net.encryption_client: REJECTED oracle.net.crypto_checksum_client: REJECTED </value> </property> </bean>Please refer to stackoverflow.com.