views:

715

answers:

3

I am having a bitch of a time getting this simple application working. I've got a configuration file in my default class path, using annotations with the correct class entries in the config file, and a default Session factory class that generated by the hibernate eclipse plug in tool. If I only rely on the default classpath config file, I get a hibernate.properties not found. If I add another call early on in app lifecycle that sets the path to the file for the factory object, I get a Duplicate class/entity mapping error. Why would it fail to find the properties, or why might be seeing that error even though the config file is on the classpath? What is the proper way to set the file path to the configuration file dynamically so I don't have to rely on the config file built into the app's classpath?

A: 

this isn't really an answer, just further observations that wouldn't fit into a comment:

as another follow up, to help any would be rescuers, I am now getting partially through the configuration and initialization process during the app run. I now have hibernate.cfg.xml in my default classpath. If I just run with that, I get properties not found error. If I add a call to the session factory class ...setFilePath(pathToConfigFileAsString) method, I get two different results, depending on whether this is the first run of a clean build, or subsequent runs. On first run. we now die during AnnotationsConfiguration on a particular class, not output stating why or anything. On a subsequent run, we'll make it all the way to DEBUG org.hibernate.cfg.AnnotationBinder - Processing annotations of and then just die, also with not helpful debug as to what's happening. Why would there be two different outcomes based on whether the app has run before?

Bill
You should have edited your question to include this instead of posting an answer.
André Neves
A: 

I frankly don't know what your Eclipse-generated SessionFactory code is doing, but if your hibernate.cfg.xml is REALLY in your root src folder, this should succed in creating a SessionFactory for you.

SessionFactory sf = new AnnotationConfiguration().configure().buildSessionFactory();

The configure() method may take an argument which indicates which config file to use. If omitted, the "root src folder" hibernate.cfg.xml is implied.

Perhaps playing with that could give us more info on the problem.

André Neves
+1  A: 

Thanks Andre, I've gained a little more insight and have basically solved my initial problem.

Basically, I decided to get rid of the generated SessionFactory class and am just using a regular SessionFactory. The problem I was having was due to the static initialization that was called each time I referenced the factory class, and basically, my app had two sessions when I was thinking there was only one. Now, as for the configuration, I read section 3.7 in the Configuration chapter of the conceptual docs, and it misleadingly indicates that you can pass, or at least what I interpreted as a "path" to another file like such:

SessionFactory sf = new AnnotationConfiguration().configure("what/i/though/was/path/to/file").buildSessionFactory();

but turns out it will look for the "name" of the config file at the same place: the default classpath package. At that point, I decided to load a property from plain old java properties that would specify the name, and just keep all the named files on the classpath. In this way, I could load the right file for the right environment. Now if I could get past this deployment only issue of:

Sep 14 05:25:53 localhost.localdomain (SessionFactoryObjectFactory.java:82) INFO org.hibernate.impl.SessionFactoryObjectFactory - Not binding factory to JNDI, no JNDI name configured

they sure don't make it easy to use...;-) Thanks again, I really appreciate the response.

Bill