|
|
YOUR FEEDBACK
Did you read today's front page stories & breaking news?
SOA World Conference
Virtualization Conference $200 Savings Expire May 16, 2008... – Register Today!
SYS-CON.TV |
TOP THREE LINKS YOU MUST CLICK ON Hibernate | JSF | Spring
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
By: Uday Kumar
Nov. 13, 2005 03:00 AM
Digg This!
Hibernate Tutorial - Small Corrections 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
LATEST JAVA STORIES & POSTS
SUBSCRIBE TO THE WORLD'S MOST POWERFUL NEWSLETTERS SUBSCRIBE TO OUR RSS FEEDS & GET YOUR SYS-CON NEWS LIVE!
|
SYS-CON FEATURED WHITEPAPERS MOST READ THIS WEEK BREAKING JAVA NEWS
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||