Win a copy of 97 Things Every Java Programmer Should Know this week in the Java in General forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Frits Walraven
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • salvin francis
  • fred rosenberger

Problem in Initializing SessionFactory

 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi folks,
I am new to hibernate.I am using hibernate 3. I have a problem in initialization.I doing static initialization of SessionFactory like this


But the problem is I am putting the hibernate.cfg.xml in the WEB-INF directory.In doing so it cannot locate the configuration file and throwing exception /hibernate.cfg.xml not found
I know it can be written like

where
But how can i supply the fullpath from my Action class(I am using struts) to this java class. . At the same time I really want this static initialization to happen.
Please help me if anyone finds a solution to this problem.
 
author and cow tipper
Posts: 5000
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Which development tool are you using?

Just put it in the root folder of your Java source code. Actually, at runtime it needs to go in the WEB-INF\classes folder, so make sure everything that is the source folder gets moved to the WEB-INF\classes folder at runtime.

As just a test, try copying the file into WEB-INF\classes and see what happens when you re-run it.

I'm assuming that this is a bit of learning and proof of concept, so your POJO classes are right there in your web module, right? We like to put POJO classes in a separate project or JAR when we're building a sophisticated project, but it tends to complicate things a little too much when you're just learning.

Here's a little tidbit on where to put the hibernate.cfg.xml file from my book Hibernate Made Easy.


Where do you put the hibernate.cfg.xml file???

Of course, if you're customizing the sample hibernate.cfg.xml file that comes with the Hibernate Core download, you'll have to save the edited file to a more conspicuous place on your workstation. The only rule about the hibernate.cfg.xml file is that it must be on your classpath. I typically place it in the src directory where I write my Java code, and make sure it gets copied into the build folder where my compiled byte-code gets placed. In this instance, I'm just going to save the hibernate.cfg.xml file in the C:\_hiblib folder, and make sure this folder is on the classpath whenever I compile or run my Java code.


Getting Started with Hibernate Tutorial



And post back. Don't leave us hanging on for an answer!

-Cameron McKenzie
 
Saptarshi Chakraborty
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am using Eclipse JBoss IDE . I tried that approach by copying the file to the classes directory and it worked fine,but is this a right approach ??In development time we put all the configuration files inside the WEB-INF directory just like struts-config.xml,web.xml.
Yes you are right right I put all the pojos,along with the *.hbm.xml files inside a different jar file .This is because I tried to follow the enterprise development structure and the design patterns (front-controller,business delegate, etc) and I am quite familiar with DAO pattern also .That is why I am trying the same approach simply by using hibernate instead of normal sqls .
 
Cameron Wallace McKenzie
author and cow tipper
Posts: 5000
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think your desire to put all the configuration files in the WEB-INF folder is a noble idea, but I think it might not be the right approach. Remember, that folder is for web centric configuration files. But hibernate.cfg.xml is not web-centric. I think you'd have a very difficult time convincing an architecture review board that having the hibernate.cfg.xml file in the WEB-INF folder was a defendable design. Maybe you can convince me otherwise?

-Cameron McKenzie
 
Ew. You guys are ugly with a capital UG. Here, maybe this tiny ad can help:
Devious Experiments for a Truly Passive Greenhouse!
https://www.kickstarter.com/projects/paulwheaton/greenhouse-1
    Bookmark Topic Watch Topic
  • New Topic