Java
  Home arrow Java arrow Page 3 - Adding Hibernate to a Java Application
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
JAVA

Adding Hibernate to a Java Application
By: O'Reilly Media
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 3
    2006-11-30

    Table of Contents:
  • Adding Hibernate to a Java Application
  • Hibernate Mappings for Existing Domain Objects
  • Changing the Application Configuration
  • Spring’s Built-In Hibernate Support

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    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).

    More Java Articles
    More By O'Reilly Media


       · This article is an excerpt from the book "Better, Faster, Lighter Java," published...
     

    Buy this book now. 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.

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







    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 5 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek