Java Reference
In-Depth Information
Hibernate since you have named the columns in the database the same as the attributes in your object.
For simplicity, you will mark the
Transaction
attribute as
@Transient
so that Hibernate ignores it.
Once you have Hibernate as part of your project and map your classes, you can configure Hibernate.
To configure Hibernate, you will need to configure a SessionFactory, update your transaction manager,
and create a
hibernate.cfg.xml
to tell Hibernate where to find the domain objects. Let's look at the
updates to the
launch-context.xml
file first. Here you will add the session factory as well as change the
transaction manager to use Spring's HibernateTransactionManager. Listing 7-45 shows the updates to
the
launch-context.xml
.
Listing 7-45.
Launch-config.xml Updates
…
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSyource" ref="dataSyource" />
<property name="configLocation">
<value>classpath:hibernate.cfg.xml</value>
</property>
<property name="configurationClass">
<value>org.hibernate.cfg.AnnotationConfiguration</value>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
</props>
</property>
</bean>
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager"
lazy-init="true">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
…
The SessionFactory you are using requires three things: a dataSource to provide a database
connection to the Hibernate Session it creates, the location of the Hibernate configuration file (which is
in the root of your class path in this case), and, since you are using Hibernate's annotations to map your
domain objects, the
org.hibernate.cfg.AnnotationConfiguration
class for Hibernate to be able to read
the mappings. The only other dependency you provide for the SessionFactory is configuring Hibernate
to log and format the SQL it generates via the hibernateProperties.
The other change you made to the
launch-context.xml
file is to change the transactionManager
implementation you are using. You will be using Spring's
org.springframework.orm.hibernate.HibernateTransactionManager
instead of the
org.springframework.jdbc.datasource.DataSourceTransactionManager
you have been using up to this
point.
To configure Hibernate itself, you need to provide a
hibernate.cfg.xml
file in the root of your
classpath. To do this, you create a new file in
<myprojectname>/src/main/resources
called
hibernate.cfg.xml
. Listing 7-46 shows that its sole use in your example is to list your domain classes for
Hibernate.