By Uday Kumar | Article Rating: |
|
November 13, 2005 03:00 AM EST | Reads: |
24,650 |
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
Published November 13, 2005 Reads 24,650
Copyright © 2005 SYS-CON Media, Inc. — All Rights Reserved.
Syndicated stories and blog feeds, all rights reserved by the author.
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.
![]() |
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 everything seems fine , but i'm getting WARNING: Could not bind factory to JNDI |
- Agile Adoption – Crossing the Chasm
- Write Once Run Anywhere or Cross Platform Mobile Development Tools
- Cloud Expo New York: The Java EE 7 Platform - Developing for the Cloud
- Cross-Platform Mobile Website Development – a Tool Comparison
- Architecture Governance – the TOGAF Way
- It's the Java vs. C++ Shootout Revisited!
- Twelve New Programming Languages: Is Cloud Responsible?
- Scaling Java and JSP Apps with Distributed Caching
- Cloud Expo New York Speaker Profile: Arun Gupta – Oracle
- Agile Development & Enterprise Architecture Practice – Can They Coexist?
- How to Get Started with Java and NetBeans
- Component Development and Assembly Using OSGi Services
- Agile Adoption – Crossing the Chasm
- Graal, a Dynamic Java Compiler in the Works
- Write Once Run Anywhere or Cross Platform Mobile Development Tools
- Cloud Expo New York: The Java EE 7 Platform - Developing for the Cloud
- Cross-Platform Mobile Website Development – a Tool Comparison
- Architecture Governance – the TOGAF Way
- Google Analytics with Monitis Dashboard
- It's the Java vs. C++ Shootout Revisited!
- Twelve New Programming Languages: Is Cloud Responsible?
- Scaling Java and JSP Apps with Distributed Caching
- Cloud Expo New York Speaker Profile: Arun Gupta – Oracle
- Agile Development & Enterprise Architecture Practice – Can They Coexist?
- A Cup of AJAX? Nay, Just Regular Java Please
- Java Developer's Journal Exclusive: 2006 "JDJ Editors' Choice" Awards
- JavaServer Faces (JSF) vs Struts
- The i-Technology Right Stuff
- Rich Internet Applications with Adobe Flex 2 and Java
- Java vs C++ "Shootout" Revisited
- Bean-Managed Persistence Using a Proxy List
- Reporting Made Easy with JasperReports and Hibernate
- Creating a Pet Store Application with JavaServer Faces, Spring, and Hibernate
- Why Do 'Cool Kids' Choose Ruby or PHP to Build Websites Instead of Java?
- What's New in Eclipse?
- i-Technology Predictions for 2007: Where's It All Headed?
- ');
for(i = 0; i < google_ads.length; ++i)
{
document.write('
- ');
document.write('' + google_ads[i].line1 + '
'); document.write('' + google_ads[i].visible_url + '
'); document.write(google_ads[i].line2 + ' ' + google_ads[i].line3); document.write(' ');
}
document.write('