I am using Hibernate for the O-R mapping between my application and the HSQLDB.
It is a very simple java application. When I open the application the objects are loaded with the data in the databse. Then after the user makees changes to the data. He saves it by clicking the Save button. Now the changes are commited to the HSQLDB through Hibernate i.e, made peersistent . But when I close the application, the committed changes of one particular entity is uncommited. When I closely checked, i found that it is uncommitted after the System.gc() call. But this happens only for one particular entity. All other entities are persistent.
What is the problem here? Please help.
Hibernate version: 3.0
Mapping documents:
<?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>
<!-- Database connection settings -->
<property name="connection.driver_class">org.hsqldb.jdbcDriver</property>
<property name="connection.url">jdbc:hsqldb:hsql://localhost</property>
<property name="connection.username">sa</property>
<property name="connection.password"></property>
<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">1</property>
<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.HSQLDialect</property>
<!-- Enable Hibernate's automatic session context management -->
<property name="current_session_context_class">thread</property>
<!-- Disable the second-level cache -->
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>
<!-- Drop and re-create the database schema on startup-->
<property name="hbm2ddl.auto">create</property>
<mapping resource="maps/ProjectEntity.hdb.map"/>
<mapping resource="maps/PlanEntity.hdb.map"/>
<mapping resource="maps/ActivityEntity.hdb.map"/>
<mapping resource="maps/TaskEntity.hdb.map"/>
<mapping resource="maps/Calendar.hdb.map"/>
<mapping resource="maps/PrerequisiteCategory.hdb.map"/>
<mapping resource="maps/PrerequisiteEntity.hdb.map"/>
<mapping resource="maps/PrerequisiteMgmtType.hdb.map"/>
<mapping resource="maps/PrerequisiteTrackingInfo.hdb.map"/>
<mapping resource="maps/ResourceAllocation.hdb.map"/>
<mapping resource="maps/ResourceEntity.hdb.map"/>
<mapping resource="maps/TaskACPMInfo.hdb.map"/>
<mapping resource="maps/TaskBuffer.hdb.map"/>
<mapping resource="maps/TaskCPMInfo.hdb.map"/>
<mapping resource="maps/TaskKCAInfo.hdb.map"/>
<mapping resource="maps/TaskPrerequisiteInfo.hdb.map"/>
<mapping resource="maps/TaskWorkInfo.hdb.map"/>
<mapping resource="maps/TrackingParameter.hdb.map"/>
<mapping resource="maps/UserSettings.hdb.map"/>
<mapping resource="maps/Variance.hdb.map"/>
<mapping resource="maps/ReportEntity.hdb.map"/>
</session-factory>
</hibernate-configuration>
Code between sessionFactory.openSession() and session.close():
/**
* Returns the ThreadLocal Session instance. Lazy initialize the
* <code>SessionFactory</code> if needed.
*
* @return Session
* @throws HibernateException
*/
public Session openSession() {
Session session = sessionFactory.getCurrentSession();
if(!session.getTransaction().isActive()){
//Lavanyasession.getTransaction().commit();
session.beginTransaction();
}
return session;
}
/**
* The behaviour of this method depends on the session context you have
* configured. This factory is intended to be used with a hibernate.cfg.xml
* including the following property <property
* name="current_session_context_class">thread</property> This would return
* the current open session or if this does not exist, will create a new
* session
*
* @return
*/
public Session getCurrentSession() {
Session session = sessionFactory.getCurrentSession();
return session;
}
public Transaction beginTransaction(Session session) {
if(!session.getTransaction().isActive()){
return session.beginTransaction();
}
else
{
return session.getTransaction();
}
}
private org.hibernate.SessionFactory getSessionFactory () {
return sessionFactory;
}
/**
* This save function commits the current transaction to the database. Needs to be called when the save feature
* is invoked.
* TODO Have to include the persistance in the TEMP tables to remain after commit action is invoked.
* Right now the tables get flushed.
*/
public boolean save(){
Session session = getCurrentSession();
session.flush();
Transaction transaction = session.getTransaction();
if(!transaction.isActive())
{
transaction = beginTransaction(session);
}
transaction.commit();
return transaction.wasCommitted();
}
public boolean rollback () {
Transaction transaction = getCurrentSession().getTransaction();TransactionManager.getInstance().openSession();
transaction.rollback();
return transaction.wasRolledBack();
}
/**
* Saving the current session transaction that is passed in the parameter
*
*/
public void save(Session session){
if (session != null){
Transaction transaction = session.
getTransaction();
if(transaction.isActive()){
transaction.commit();
}
session.close();
}
}
public void flush () {
Session session = getCurrentSession();
if(session.getTransaction().isActive()){
session.flush();
}
}
/**
* This closes the session factory object. This should only be called
* once when the application is being shutdown.
*
*/
public void close(){
if (sessionFactory != null){
if(getCurrentSession().getTransaction().isActive()) {
getCurrentSession().flush();
}
sessionFactory.close();
}
}
Name and version of the database you are using: HSQLDB
|