The Wayback Machine - https://web.archive.org/web/20120511013505/http://java.sys-con.com/node/152657

Welcome!

Java Authors: Liz McMillan, Patrick Burke, Kevin Nikkhoo, Srinivasan Sundara Rajan, Jim Vogt

Related Topics: Java

Java: Article

Uday Kumar's Java Blog – Getting Hibernate 3.x Up and Running with Tomcat

A Few Minor Changes to the Hibernate Tutorial at Hibernate.org

Hibernate Tutorial - Small Corrections

I am back again after a hiatus. I was busy dealing with some work issues and some health issues.

I was reviewing the Hibernate tutorial at http://www.hibernate.org/hib_docs/v3/reference/en/html/quickstart.html and am publishing a few minor changes which might help the new initiate to get up and running quickly. This link refers to getting Hibernate 3.x up and running with Tomcat 4.1. I ran it with Tomcat 5.5.2.

The tutorial uses PostgreSQL 7.4 and I used MySQL 4.0.21. So the driver file to copy is mysql-connector-java-3.0.15-ga-bin.jar. This can be ideally copied to the TOMCAT_HOME \shared\lib directory ( where TOMCAT_HOME is where you installed Tomcat ). The reason for placing it in the shared\lib as opposed to the common\lib is because, as the Tomcat documentation states: the files placed in common are visible to Tomcat internal code and all webapps whereas those in shared\lib are visible to all web apps only. In this case the MySql drivers are not needed for the internal code.

The following libraries were copied to the context class path which in the case of the tutorial would typically be:

TOMCAT_HOME\webapps\quickstart\WEB_INF\lib  (the context created in my case was called quickstart)

ant-antlr.jar

asm.jar, cglib-2.1.2.jar, commons-collections-2.1.1.jar,commons-dbcp-1.2.1.jar, commons-logging-1.0.4.jar,

commons-pool-1.2.jar, dom4j-1.6.1.jar, ehcache-1.1.jar,  hibernate3.jar, log4j-1.2.11.jar

The next section in the tutorial refers to the creation of a Context. The only change here is based on style: the tutorial recommends adding this Context entry to TOMCAT-HOME\conf\server.xml. However for Tomcat 5.x the documentation recommends that these type of context entries shouldnt be placed directly into the server.xml but instead should be in a separate set of directories that are either web application specific as in META_INF\context.xml or in TOMCAT_HOME\conf sub-directories.

I chose to place the context definitions in TOMCAT_HOME\conf\Catalina\HOST_NAME in a file called quickstart.xml.

The next change is with reference to the cfg.xml file. Hibernate uses a hibernate.properties file as the default for loading config information. Thus if you were to code as follows for getting a SessionFactory instance:

SessionFactory sessionFactory = new Configuration().buildSessionFactory();

then the hibernate.properties file in the context classpath will be used for loading the configuration information.

If you want instead to use the hibernate.cfg.xml file for loading the configuration information, then the following method call should be done:

SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();

The configure method of the Configuration class is hard coded to use the hibernate.cfg.xml file.

The tutorial uses the cfg.xml file, but I ran into errors because of improper configuration parameters: so I switched to the properties file instead. I just used the default values in the properties file that came with the distribution.  I am sure that I didnt tweak the cfg.xml file properly as I just used the entries given in the tutorial directly but I didnt have the patience to investigate so switched to the less expressive properties file instead.

The mapping file is an important entity here as it is the artifact which allows us to map objects onto the rdbms tables. When making the above switch to the hibernate.properties file for the configuration information, however, there is no place to specify the mapping file name.  So another minor change that is required while building the configuration is as follows:

Configuration cfg = new Configuration().addClass(Cat.class)

where you substitute your persistent class name instead of Cat.class.  The addClass method in Configuration.java will then do a getName call on the Class passed in as input argument and suffix it with hbm.xml after replacing '.' with a '/' and try to get the appropriate mapping file from the same package location as the persistent class. So you would of course place the mapping file in the same package directory structure as your persistent class.

Other than that there are no major changes in the tutorial and things will go smoothly.

posted Sunday, 13 November 2005

More Stories By Uday Kumar

Uday Kumar is the vice president of Technology for Covansys Corporation, a U.S.-based corporation that specializes in providing custom application builds and integration services to its customers. He consults as a chief architect and guides development teams building enterprise architecture solutions using J2EE and .NET technologies and also runs the E-Business practice for Covansys’ Western region. His professional interests include operating systems research and virtual machine implementations. Uday is currently researching the comparative merits of virtual machines such as Parrot, Microsoft’s CLR, and the JVM. He builds Perl applications as a hobby.

Comments (3) View Comments

Share your thoughts on this story.

Add your comment
You must be signed in to add a comment. Sign-in | Register

In accordance with our Comment Policy, we encourage comments that are on topic, relevant and to-the-point. We will remove comments that include profanity, personal attacks, racial slurs, threats of violence, or other inappropriate material that violates our Terms and Conditions, and will block users who make repeated violations. We ask all readers to expect diversity of opinion and to treat one another with dignity and respect.


Most Recent Comments
subrahmanyam 09/04/06 03:10:51 AM EDT

Hai its nice and good to see the data of hibernate and i am new to hibernate please give me information about those abstract classes?

Daniel 05/24/06 08:32:00 AM EDT

I didn't understand why did you say to use shared instead of common for the jdbc driver. It doesn't in my project here when I use this. Also, the link for your tutorial is broken

chetan Kumar 04/17/06 06:49:51 AM EDT

I'm using the same configuration as u are
hibernate 3 , mysql 5 and tomcat 5 with JDK 1.5

everything seems fine , but i'm getting

WARNING: Could not bind factory to JNDI
javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file: java.naming.factory.initial
at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:645)
at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:247)
at javax.naming.InitialContext.getURLOrDefaultInitCtx(InitialContext.java:284)
at javax.naming.InitialContext.getNameParser(InitialContext.java:439)
at org.hibernate.util.NamingHelper.bind(NamingHelper.java:52)
at org.hibernate.impl.SessionFactoryObjectFactory.addInstance(SessionFactoryObjectFactory.java:90)
at org.hibernate.impl.SessionFactoryImpl.(SessionFactoryImpl.java:291)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1176)
at com.contata.chetank.managers.HibernateSessionFactory.(HibernateSessionFactory.java:14)
at com.contata.chetank.daoImpl.Test.main(Test.java:17)

'); var i; document.write(''); } // -->
 

About JAVA Developer's Journal
Java Developer's Journal presents insight, technical explanations and new ideas related to Java and covers the technology that affects Java developers and their companies.

ADD THIS FEED TO YOUR ONLINE NEWS READER Add to Google My Yahoo! My MSN