I am using Hibernate 2.1.1 with JDK 1.4.2, Resin 2.1.12 and SAPDB 7.4.
Let me start off by saying that I am not doing a SchemaUpdate anywhere in my code. Yet I am getting the following error in my log file.
ERROR net.sf.hibernate.tool.hbm2ddl.SchemaUpdate - Unsuccessful: alter table FREIGHTS foreign key FKD335B5907D7B26E0 (UPDATEBY) references USERS
ERROR net.sf.hibernate.tool.hbm2ddl.SchemaUpdate - [-8006] (at 54): Data types must be compatible
I don't know if this is related or not but I am also getting java.lang.OutOfMemoryException even though I explicitly close all my sessions in a finally after the try-catch loop. Additionally, I turned the hibernate.cglib.use_reflection_optimizer to false in my hibernate.cfg.xml file (given below).
Here are my questions.1. Is there any situation where SchemaUpdate would be executed by calling any of the methods I have specified below?
2. Is there any apparent reason for the java.lang.OutOfMemoryException based on my HibernateManager object (code given below)?
These questions do seem cryptic to me as well because I don't know what other information I can provide that may enable anyone to help me out.
The only methods that I call on my net.sf.hibernate.Session object are
load() to get an instance of an object by the Id value
createCriteria() to setup conditions to query results
beginTransaction() to create a transaction before doing a saveOrUpdate()
saveOrUpdate() to perform data update functions
flush() to flush persistent objects before committing the transaction
The HibernateManager object is as follows
Code:
package com.foo.bar.utilities;
import net.sf.hibernate.HibernateException;
import net.sf.hibernate.Session;
import net.sf.hibernate.SessionFactory;
import net.sf.hibernate.cfg.Configuration;
import org.apache.log4j.Logger;
/**
* Description of the Class
*
* @author gpani
* @version 1.1
* @since JDK 1.4.2
*/
public class HibernateManager
{
/** The ThreadLocal for the HibernateManager */
public final static ThreadLocal sessionThread = new ThreadLocal();
/** The SessionFactory for the HibernateManager */
private final static SessionFactory sessionFactorySAP;
/**
* This is the logger used by Log4J based on the setup in <i>log4j.properties
* </i>
*/
private static Logger logger = Logger.getLogger(HibernateManager.class.getName());
/**
* The value
*
* @exception HibernateException Description of the Exception
*/
public static void closeSession()
throws HibernateException
{
Session session = (Session) sessionThread.get();
sessionThread.set(null);
if (session != null)
{
session.close();
}
}
/**
* The value
*
* @return Description of the Return Value
* @exception HibernateException Description of the Exception
*/
public static Session currentSession()
throws HibernateException
{
Session session = (Session) sessionThread.get();
/** Open a new Session, if this Thread has none yet */
if (session == null)
{
session = sessionFactorySAP.openSession();
sessionThread.set(session);
}
return session;
}
static
{
try
{
/**
* File name need not be specified here. By default hibernate.cfg.xml is chosen.
* However, if the file name is changed in the application, it will need to
* be modified here
*/
sessionFactorySAP = new Configuration().configure("/hibernate.cfg.xml").buildSessionFactory();
logger.debug("Successfully created sessionFactory for SAPDB");
}
catch (HibernateException e)
{
logger.debug("HibernateException building SessionFactory: " + e.getMessage());
throw new RuntimeException("HibernateException building SessionFactory: " + e.getMessage(), e);
}
}
}
The structure that I follow in my code to do any Hibernate functions is as follows:
Code:
public boolean save(Freight freight)
{
String sMethodName = "save(" + freight + "): ";
Session hibernateSession = null;
Transaction transaction = null;
boolean saved = false;
try
{
if (freight != null)
{
hibernateSession = HibernateManager.currentSession();
transaction = hibernateSession.beginTransaction();
hibernateSession.saveOrUpdate(freight);
hibernateSession.flush();
transaction.commit();
saved = true;
}
}
catch (HibernateException e)
{
try
{
transaction.rollback();
}
catch (Exception ex)
{
logger.warn("Exception encountered rolling back transaction due to HibernateException in " + sMethodName + Utilities.getStackTrace(ex));
}
logger.warn("Hibernate Exception in " + sMethodName + Utilities.getStackTrace(e));
}
catch (Exception e)
{
try
{
transaction.rollback();
}
catch (Exception ex)
{
logger.warn("Exception encountered rolling back transaction due to General Exception in " + sMethodName + Utilities.getStackTrace(ex));
}
logger.warn("General Exception in " + sMethodName + Utilities.getStackTrace(e));
}
finally
{
if (hibernateSession != null)
{
try
{
hibernateSession.close();
}
catch (HibernateException e)
{
logger.warn("Hibernate Exception closing session in " + sMethodName + Utilities.getStackTrace(e));
}
}
try
{
HibernateManager.closeSession();
}
catch (HibernateException e)
{
logger.warn("Hibernate Exception closing HibernateManager in " + sMethodName + Utilities.getStackTrace(e));
}
}
return saved;
}
Code:
<?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" [
<!ENTITY fileList SYSTEM "file:hbnfiles.xml">
]>
<hibernate-configuration>
<!-- a SessionFactory instance listed as /jndi/name -->
<session-factory>
<property name="hibernate.connection.datasource">java:comp/env/hibernate/sapdb</property>
<property name="dialect">net.sf.hibernate.dialect.SAPDBDialect</property>
<property name="show_sql">true</property>
<property name="transaction.factory_class">net.sf.hibernate.transaction.JDBCTransactionFactory</property>
<property name="hibernate.cache.provider_class">net.sf.hibernate.cache.HashtableCacheProvider</property>
<property name="hibernate.hbm2ddl.auto">update</property>
<property name="hibernate.cglib.use_reflection_optimizer"> false</property>
<!-- mapping files -->
<!--&fileList;-->
<!-- site mappings -->
<mapping resource="com/foo/bar/beans/site/Freight.hbm.xml"/>
<!-- site access mappings -->
<mapping resource="com/foo/bar/beans/site/access/UserGroup.hbm.xml"/>
<!-- user mappings -->
<mapping resource="com/foo/bar/beans/user/User.hbm.xml"/>
</session-factory>
</hibernate-configuration>