I'm trying desperately to get hibernate to load the hibernate.cfg.xml file from a path I set in the class that does all the factory configuration. It's not working. Here's the guts of the class that's supposed to do that:
Code:
private static Logger log = Logger.getLogger(MMHibernateSessionFactoryAnnotations.class.getName());
/**
* Location of hibernate.cfg.xml file.
* Location should be on the classpath as Hibernate uses
* #resourceAsStream style lookup for its configuration file.
* The default classpath location of the hibernate config file is
* in the default package. Use #setConfigFile() to update
* the location of the configuration file for the current session.
*/
private static String CONFIG_FILE_LOCATION = PostEC.hibernatePropertiesFilePath;
private static final ThreadLocal<Session> threadLocal = new ThreadLocal<Session>();
private static Configuration configuration = new AnnotationConfiguration();
private static org.hibernate.SessionFactory sessionFactory;
private static String configFile = CONFIG_FILE_LOCATION;
static {
try {
log.debug("The factory thinks the hibernate config file is at: " + configFile);
configuration.configure(configFile);
sessionFactory = configuration.buildSessionFactory();
} catch (Exception e) {
System.err
.println("%%%% Error Creating SessionFactory %%%%");
e.printStackTrace();
}
}
private MMHibernateSessionFactoryAnnotations() {
}
/**
* Returns the ThreadLocal Session instance. Lazy initialize
* the <code>SessionFactory</code> if needed.
*
* @return Session
* @throws HibernateException
*/
public static Session getSession() throws HibernateException {
Session session = (Session) threadLocal.get();
log.debug("getting a session");
if (session == null || !session.isOpen()) {
if (sessionFactory == null) {
rebuildSessionFactory();
}
session = (sessionFactory != null) ? sessionFactory.openSession()
: null;
threadLocal.set(session);
}
return session;
}
/**
* Rebuild hibernate session factory
*
*/
public static void rebuildSessionFactory() {
log.debug("rebuilding session factory");
try {
configuration.configure(configFile);
sessionFactory = configuration.buildSessionFactory();
} catch (Exception e) {
System.err
.println("%%%% Error Creating SessionFactory %%%%");
e.printStackTrace();
}
}
/**
* Close the single hibernate session instance.
*
* @throws HibernateException
*/
public static void closeSession() throws HibernateException {
Session session = (Session) threadLocal.get();
threadLocal.set(null);
if (session != null) {
session.close();
}
}
/**
* return session factory
*
*/
public static org.hibernate.SessionFactory getSessionFactory() {
return sessionFactory;
}
/**
* return session factory
*
* session factory will be rebuilded in the next call
*/
public static void setConfigFile(String configFile) {
log.debug("the new configFile string is: " + configFile);
MMHibernateSessionFactoryAnnotations.configFile = configFile;
sessionFactory = null;
}
/**
* return hibernate configuration
*
*/
public static Configuration getConfiguration() {
return configuration;
}
And here's the code I use to get it to do what it says it will do and doesn't:
Code:
log.debug("the path to the hibernate config from properties: " + PostEC.hibernatePropertiesFilePath);
MMHibernateSessionFactoryAnnotations.setConfigFile(PostEC.hibernatePropertiesFilePath);
Session theSess = MMHibernateSessionFactoryAnnotations.getSession();
The property is set, and it prints out properly, and all I get is the hateful hibernate.properties not found, like this:
Code:
Time spent de-serializing 1 records from disk: 525 ms at:1.9083969465648853/second
Sep 08 02:01:53 (Environment.java:514) INFO org.hibernate.cfg.Environment - Hibernate 3.2.5
Sep 08 02:01:53 (Environment.java:547) INFO org.hibernate.cfg.Environment - hibernate.properties not found
and the the app exits.
This factory class was generated using the hibernate eclipse plug ins, and given that I had to pretty much redo the pojo files it also generated by hand, I wouldn't be surprised if this generated class failed as well. However, this link indicates that it is the correct approach:
https://www.hibernate.org/171.htmlHow many chickens and to what god must they be sacrificed to get this working?