Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp
Hibernate version:
3
Mapping documents:
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-2.0.dtd">
<!-- DO NOT EDIT: This is a generated file that is synchronized -->
<!-- by MyEclipse Hibernate tool integration. -->
<hibernate-configuration>
<session-factory>
<!-- properties -->
<!--
<property name="connection.username">mysql</property>
<property name="connection.url">jdbc:mysql://127.0.0.1:3306/applicationExceptions</property>
<property name="dialect">net.sf.hibernate.dialect.MySQLDialect</property>
<property name="connection.password">password</property>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property> -->
<property name="hibernate.connection.datasource">applicationExceptions.dataSource</property>
<property name="hibernate.jndi.url">t3://127.0.0.1:7001</property>
<property name="hibernate.jndi.class">weblogic.jndi.WLInitialContextFactory</property>
<property name="show_sql">false</property>
<property name="use_outer_join">true</property>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="dialect">net.sf.hibernate.dialect.MySQLDialect</property>
<!-- mapping files -->
<mapping resource="com/whatever/applicationExceptions/data/TError.hbm.xml"/>
</session-factory>
</hibernate-configuration>
Code between sessionFactory.openSession() and session.close():
// entire code:
ublic class HibernateSessionFactory {
/**
* Location of hibernate.cfg.xml file.
* NOTICE: Location should be on the classpath as Hibernate uses
* #resourceAsStream style lookup for its configuration file. That
* is place the config file in a Java package - the default location
* is the default Java package.<br><br>
* Examples: <br>
* <code>CONFIG_FILE_LOCATION = "/hibernate.conf.xml".
* CONFIG_FILE_LOCATION = "/com/foo/bar/myhiberstuff.conf.xml".</code>
*/
// private static String CONFIG_FILE_LOCATION = "/hibernate.cfg.xml";
private static String CONFIG_FILE_LOCATION = "/persistence_hibernate.cfg.xml";
/** Holds a single instance of Session */
private static final ThreadLocal threadLocal = new ThreadLocal();
/** The single instance of hibernate configuration */
private static final Configuration cfg = new Configuration();
/** The single instance of hibernate SessionFactory */
private static net.sf.hibernate.SessionFactory sessionFactory;
/**
* Returns the ThreadLocal Session instance. Lazy initialize
* the <code>SessionFactory</code> if needed.
*
* @return Session
* @throws HibernateException
*/
public static Session currentSession() throws HibernateException {
Session session = (Session) threadLocal.get();
if (session == null) {
if (sessionFactory == null) {
try {
cfg.configure(CONFIG_FILE_LOCATION);
sessionFactory = cfg.buildSessionFactory();
}
catch (Exception e) {
System.err.println("%%%% Error Creating SessionFactory %%%%");
e.printStackTrace();
}
}
session = sessionFactory.openSession();
threadLocal.set(session);
}
return session;
}
/**
* 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();
}
}
/**
* Default constructor.
*/
private HibernateSessionFactory() {
}
Full stack trace of any exception that occurs:
Name and version of the database you are using:
MySql 5
**********************************************************
Hello friends -
I'm new to Hibernate and was wondering if anyone could give me some insight as to the usefulness / appropriateness of the SessionFactory defined above. This code was generated by the MyEclipse plug-in. It does work, but I can't help but notice that it doesn't actually implement SessionFactory and find myself wondering what is being left out in order to provide a quick and dirty mechanism for getting to the database.
Can anyone give me any guides to other existing SessionFactory implementations that are available and their opinions on them? It does look like the hibernate source came with an implementation class that I can try also.
Thanks!
-pd