I am using hibernate 3.2.5, and I am learning to generate automatic code from mapping files. To generate code I use Hibernate Code Generation Dialog for Eclipse. The problem I have, is with generated DAOs. They all try to get SessionFactory from JNDI context, but I don't use JNDI context. I use the HibernateUtil class that is fount on tutorial. The important files are:
Mapping file
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="cars">
<class name="Option" table="OPTIONS">
<id name="id" column="OPTION_ID" type="integer">
<meta attribute="use-in-tostring">true</meta>
<meta attribute="use-in-equals">true</meta>
<generator class="native" />
</id>
<property name="optionName" type="string" column="OPTION_NAME" unique="true" not-null="true" />
</class>
</hibernate-mapping>
hibernate.cfg.xml
Code:
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory name="SessionFactory">
<property name="hibernate.bytecode.use_reflection_optimizer">false</property>
<property name="hibernate.cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.password">password</property>
<property name="hibernate.connection.pool_size">1</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/cars</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.current_session_context_class">thread</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.show_sql">true</property>
<mapping resource="cars/Option.hbm.xml" />
</session-factory>
</hibernate-configuration>
Generated DAO file:
Code:
public class OptionHome {
private static final Log log = LogFactory.getLog(OptionHome.class);
private final SessionFactory sessionFactory = getSessionFactory();
protected SessionFactory getSessionFactory() {
try {
return (SessionFactory) new InitialContext().lookup("SessionFactory");
}
catch (Exception e) {
log.error("Could not locate SessionFactory in JNDI", e);
throw new IllegalStateException("Could not locate SessionFactory in JNDI");
}
}
........ //Some more irrelevant code
}
And the HibernateUtil.java file:
Code:
public class HibernateUtil {
private static final SessionFactory sessionFactory;
static {
try {
// Create the SessionFactory from hibernate.cfg.xml
sessionFactory = new Configuration().configure().buildSessionFactory();
} catch (Throwable ex) {
// Make sure you log the exception, as it might be swallowed
System.err.println("Initial SessionFactory creation failed." + ex);
throw new ExceptionInInitializerError(ex);
}
}
public static SessionFactory getSessionFactory() {
return sessionFactory;
}
}
The question is: Is there any way to generate a code that refers to
HibernateUtil.getSessionFactory() instead of
(SessionFactory) new InitialContext().lookup("SessionFactory") ?
If not, there is an easy way to setup InitialContext? (Preferably, not using a web server with EJB container)