Adding Hibernate to a Java Application - Changing the Application Configuration
(Page 3 of 4 )
In order to get the new DAOs working with jPetStore, we need to modify some configuration files. First, we’ll need to create the global hibernate.properties file, which tells Hibernate which database to use and how to use it. jPetStore is currently configured to use a local instance of Hypersonic SQL, with a username of “sa” and a blank password (NEVER do this in a production environment). The hibernate.properties file looks like this:
hibernate.connection.driver_class = org.hsqldb.jdbcDriver
hibernate.connection.url = jdbc:hsqldb:hsql://localhost:9002
hibernate.connection.username = sa
hibernate.connection.password =
hibernate.dialect=net.sf.hibernate. dialect.HSQLDialect
hibernate.show_sql=true
This file should be saved in the project root file, next to the other global configuration files. Hibernate will look for it by name.
Next, open upjPetStore’s dataAccessContext-*.xml files (one is dataAccessContext-jta.xml and the other is dataAccessContext-local.xml). In each, there is a section that mapes the DAOs for the project. Change each mapping to point to the new DAO, and eliminate the now unnecessary properties. For example, the original mapping forProductDaowas:
<bean id="productDao" class="org. springframework.samples.jpetstore.dao. ibatis.
SqlMapProductDao">
<property name="dataSource"><ref local="dataSource"/></property>
<property name="sqlMap"><ref local="sqlMap"/></property>
</bean>
This now becomes:
<bean id="productDao" class="org. springframework.samples.jpetstore.dao. hibernate.
HibernateProductDao"/>
We can eliminate the properties because the Hibernate versions of the DAOs do not require any configuration information to be passed in by the controller; Hibernate manages those issues for us.
Once you have successfully changed all the DAO references, the last remaining piece is to include the necessary jar files in your class path. Hibernate requires the following jars: hibernate2.jar, cglib2.jar, ehcache.jar, commons-collections.jar, dom4j.jar, and jta.jar (all of which are included in the Hibernate download).
Next: Spring’s Built-In Hibernate Support >>
More Java Articles
More By O'Reilly Media
|
This article is excerpted from the book Better, Faster, Lighter Java, written by Bruce A. Tate and Justin Gehtland (O'Reilly; ISBN: 0596006764). Check it out today at your favorite bookstore. Buy this book now.
|
|