YOUR FEEDBACK
NGASI Releases AppServer Manager 8.1
Dave Jenkins wrote: The remote server management is a welcomed added feature...


2007 West
GOLD SPONSORS:
Active Endpoints
Your SOA Needs BPEL for Orchestration
BEA
Virtualized SOA: Adaptive Infrastructure for Demanding Applications
Nexaweb
Overcoming Bandwidth Challenges with Nexaweb
TIBCO
What is Service Virtualization?
SILVER SPONSORS:
WSO2
Using Web Services Technologies and FOSS Solutions
Click For 2007 East
Event Webcasts

2008 East
PLATINUM SPONSORS:
Appcelerator
Think Fast: Accelerate AJAX Development with Appcelerator
GOLD SPONSORS:
DreamFace Interactive
The Ultimate Framework for Creating Personalized Web 2.0 Mashups
ICEsoft
AJAX and Social Computing for the Enterprise
Kaazing
Enterprise Comet: Real–Time, Real–Time, or Real–Time Web 2.0?
Nexaweb
Now Playing: Desktop Apps in the Browser!
Sun
jMaki as an AJAX Mashup Framework
POWER PANELS:
The Business Value
of RIAs
What Lies Beyond AJAX?
KEYNOTES:
Douglas Crockford
Can We Fix the Web?
Anthony Franco
2008: The Year of the RIA
Click For 2007 Event Webcasts
SYS-CON.TV
TOP THREE LINKS YOU MUST CLICK ON


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

Digg This!

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

About 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 wrote: 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?
read & respond »
Daniel wrote: 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
read & respond »
chetan Kumar wrote: 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.NoInitialCon textException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file: java.naming.factor y.initial at javax.namin g.spi.NamingManager.getIn itialContext(NamingManage r.java:645) at javax.nam ing.InitialContext.getDef aultInitCtx(InitialContex t.java:247) at javax.nam ing.InitialContext.getURL OrDefaultInitCtx(InitialC ontext.java:284) at java x.naming.InitialContext.g etNameParser(InitialConte xt.java:439) at org.hibe rnate.util.NamingHelper.b ind(NamingHelper.java:52) at org.hibernate.impl.Se ssionFactoryObjectFactory .addInstance(SessionFacto ryObjectFac...
read & respond »
LATEST JAVA STORIES & POSTS
Real-Time Kaazing Solution and Sun's Glassfish Forge RIA Alliance
Kaazing Corporation and Sun Microsystems announced an alliance to deliver the scalable and advanced real-time Web 2.0 platform. The integration between Kaazing's real-time Rich Internet Application (RIA) solution, Enterprise Comet, and Sun Microsystems' open source Java EE applic
Virtualization Journal Attracts JavaOne Attendees to SYS-CON Media Booth
Virtualization Journal now reaches more than 60,000 online readers with monthly digital editions and weekly newsletters. The premier issue of the magazine's print edition, which debuts on May 6, 2008, at JavaOne in San Francisco, as a media sponsor of this event, will be availabl
Sun Challenges Linux
Sun's mule train has finally pulled into Indiana after three years on the road. Indiana is the Linux-friendly Fedora-like OpenSolaris project meant to move the Solaris-shy Linux community off Linux and on to Solaris tempted by Solaris widgetry like the highly scalable, rollback-e
AJAX World - Sun Talks Up its Late-to-the-Party AIR-Silverlight Rival
At Java One this week Sun has been selling its year -old-but-still-upcoming - and definitely late-to-the-party - Adobe AIR- and Microsoft Silverlight-competitive JavaFX Rich Client environment as a potential revenue-generator capable of putting ads on mobile applications and JavaF
MySQL Backs Off Closed Source Plan
MySQL has backed off a plan to charge for some encryption and compression backup widgetry in the next version of the database - and, heavens, NOT OPEN SOURCE THE STUFF, an idea it trotted a few weeks ago and predictably caught hell for. Sun, which bought MySQL for a billion dolla
JavaOne Archives - Dvorak Comments on AMD Intel Lawsuit on SYS-CON.TV
Conference in San Francisco. Dvorak held forth on a number of topics, including the new AMD/Intel lawsuit, the viability of Java and Sun, the value of (or lack thereof) of corporate PR, and whether or not a new book about Silicon Valley is really worth reading.
SUBSCRIBE TO THE WORLD'S MOST POWERFUL NEWSLETTERS
SUBSCRIBE TO OUR RSS FEEDS & GET YOUR SYS-CON NEWS LIVE!
Click to Add our RSS Feeds to the Service of Your Choice:
Google Reader or Homepage Add to My Yahoo! Subscribe with Bloglines Subscribe in NewsGator Online
myFeedster Add to My AOL Subscribe in Rojo Add 'Hugg' to Newsburst from CNET News.com Kinja Digest View Additional SYS-CON Feeds
Publish Your Article! Please send it to editorial(at)sys-con.com!

Advertise on this site! Contact advertising(at)sys-con.com! 201 802-3021

SYS-CON FEATURED WHITEPAPERS

ADS BY GOOGLE
BREAKING JAVA NEWS
Day Software to Present at Henry Stewart DAM Show
Day Software (SWX:DAYN) (OTCQX:DYIHY), a leading provider of global content management