Java

Recent site activity

Home‎ > ‎

Hibernate tips and tricks

Hibernate configuration parameters


Parameter Description Valid values
 hibernate.format_sql When used in conjunction with hibernate.show_sql, this parameter will format the generated SQL that is displayed in the log.
true, false
hibernate.show_sql
Toggles the logging of generated SQL.
true, false

Spring 2.5+ configuration for Hibernate

Here is a sample Spring application context configuration that sets up all the necessary Hibernate pieces for use within a Spring context.

 1 <beans xmlns="http://www.springframework.org/schema/beans"
 2 	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 3 	xmlns:aop="http://www.springframework.org/schema/aop"
 4 	xmlns:context="http://www.springframework.org/schema/context"
 5 	xmlns:tx="http://www.springframework.org/schema/tx"
 6 	xsi:schemaLocation="http://www.springframework.org/schema/beans
 7 	http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
 8 	http://www.springframework.org/schema/aop
 9 	http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
10 	http://www.springframework.org/schema/context
11 	http://www.springframework.org/schema/context/spring-context-2.5.xsd
12 	http://www.springframework.org/schema/tx
13 	http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
14 
15 	<context:annotation-config />
16 	<context:component-scan base-package="springmvc.examples" />
17     <aop:aspectj-autoproxy/>
18     <tx:annotation-driven transaction-manager="transactionManager"/>
19 
20 
21 	<!-- =================================================================== -->
22 	<!-- Spring-managed DataSource.                                          -->
23 	<!-- =================================================================== -->
24 	<bean id="dataSource"
25 		class="org.apache.commons.dbcp.BasicDataSource" scope="singleton">
26 		<property name="driverClassName" value="org.hsqldb.jdbcDriver" />
27 		<property name="url" value="jdbc:hsqldb:file:springmvc_examples_db" />
28 		<property name="username" value="sa" />
29 		<property name="password" value="" />
30 		<property name="maxWait" value="10000" />
31 		<property name="removeAbandoned" value="true" />
32 		<property name="maxActive" value="100" />
33 		<property name="maxIdle" value="5" />
34 		<property name="removeAbandonedTimeout" value="30" />
35 		<property name="logAbandoned" value="true" />
36 	</bean>
37 
38 	<!-- ======================================================================================= -->
39 	<!-- Hibernate SessionFactory                                                                -->
40 	<!-- ======================================================================================= -->
41 	<bean id="sessionFactory"
42 		class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
43 		<property name="dataSource" ref="dataSource" />
44 		<property name="exposeTransactionAwareSessionFactory" value="true" />
45 		<property name="annotatedClasses">
46 			<list>
47 				<value>springmvc.examples.domain.Critique</value>
48 				<value>springmvc.examples.domain.User</value>
49 				<value>springmvc.examples.domain.Wine</value>
50 				<value>springmvc.examples.domain.Winery</value>
51 			</list>
52 		</property>
53 		<property name="hibernateProperties">
54 			<props>
55 				<prop key="hibernate.dialect">
56 					org.hibernate.dialect.HSQLDialect
57 				</prop>
58 				<prop key="hibernate.format_sql">true</prop>
59 				<prop key="hibernate.show_sql">true</prop>
60 				<prop key="hibernate.hbm2ddl.auto">create-drop</prop>
61 				<prop key="hibernate.cache.provider_class">
62 					org.hibernate.cache.NoCacheProvider
63 				</prop>
64 				<prop key="hibernate.transaction.factory_class">
65 					org.hibernate.transaction.JDBCTransactionFactory
66 				</prop>
67 			</props>
68 		</property>
69 	</bean>
70 
71 	<!-- ======================================================================================= -->
72 	<!-- Hibernate transaction manager                                                           -->
73 	<!-- ======================================================================================= -->
74 	<bean id="transactionManager"
75 		class="org.springframework.orm.hibernate3.HibernateTransactionManager">
76 		<property name="sessionFactory" ref="sessionFactory" />
77 	</bean>
78 
79 	<!-- ======================================================================================= -->
80 	<!-- JDBC template.                                                                          -->
81 	<!-- ======================================================================================= -->
82 	<bean id="jdbcTemplate"
83 		class="org.springframework.jdbc.core.JdbcTemplate"
84 		scope="singleton">
85 		<property name="dataSource" ref="dataSource" />
86 	</bean>
87 
88     <!-- ==================================================================================== -->
89     <!-- DbUnit testing abstraction.                                                          -->
90     <!-- ==================================================================================== -->
91     <bean id="databaseTester" class="org.dbunit.DataSourceDatabaseTester">
92         <constructor-arg index="0" ref="dataSource" />
93     </bean>
94 
95 
96 </beans>
97