Home arrow Java arrow Page 3 - Adding Hibernate to a Java Application
JAVA

Adding Hibernate to a Java Application


This article concludes our discussion covering the addition of a search service to a Java application. It is excerpted from chapter 10 of the book Better, Faster, Lighter Java, written by Bruce A. Tate and Justin Gehtland (O'Reilly; ISBN: 0596006764).

Author Info:
By: O'Reilly Media
Rating: 5 stars5 stars5 stars5 stars5 stars / 5
November 30, 2006
TABLE OF CONTENTS:
  1. · Adding Hibernate to a Java Application
  2. · Hibernate Mappings for Existing Domain Objects
  3. · Changing the Application Configuration
  4. · Spring’s Built-In Hibernate Support

print this article
SEARCH DEVARTICLES

TOOLS YOU CAN USE

advertisement
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).


blog comments powered by Disqus
JAVA ARTICLES

- Deploying Multiple Java Applets as One
- Deploying Java Applets
- Understanding Deployment Frameworks
- Database Programming in Java Using JDBC
- Extension Interfaces and SAX
- Entities, Handlers and SAX
- Advanced SAX
- Conversions and Java Print Streams
- Formatters and Java Print Streams
- Java Print Streams
- Wildcards, Arrays, and Generics in Java
- Wildcards and Generic Methods in Java
- Finishing the Project: Java Web Development ...
- Generics and Limitations in Java
- Getting Started with Java Web Development in...

Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 



© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 5 - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials