Problems with Session and transaction handling?
Read this:
http://hibernate.org/42.html
Hello every one
i use hibernate 3.2
HSQLDB standalone
I have a problem saving to the database HSQLDB standalone
I can do the other operation (get , delete, update) but i can't save new instance to the database..
if i run the code i don't get any error, nothing happens and no exception thrown
i tried assigned key and native key generators
but both give the save problem
any idea?
Mapping file
Code:
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="model.beans.Rooster" table="ROOSTERS" schema="PUBLIC">
<id name="roosterId" type="java.lang.Long">
<column name="ROOSTER_ID" />
<generator class="assigned"></generator>
</id>
<property name="fieldNum" type="java.lang.Integer">
<column name="FIELDNUM" not-null="false"></column>
</property>
<property name="dayNum" type="java.lang.Integer">
<column name="DAY_NUM" not-null="true" />
</property>
<property name="startTime" type="java.util.Calendar">
<column name="START_TIME" length="6" not-null="true" />
</property>
<property name="endTime" type="java.util.Calendar">
<column name="END_TIME" length="6" not-null="true" />
</property>
<set name="matchPlannings" inverse="true">
<key>
<column name="FK_ROOSTERID" />
</key>
<one-to-many class="model.beans.MatchPlanning" />
</set>
</class>
</hibernate-mapping>
the save method in the dao class Code:
public void saveOrUpdate(Rooster obj) {
log.debug("saving Roosters instance");
try {
HibernateSessionFactory.getSession().saveOrUpdate(obj);
System.out.println("Saving rooster with id"+obj.getRoosterId() + " ok");
log.debug("save successful");
} catch (RuntimeException re) {
log.error("save failed", re);
throw re;
}
}
session factory Code:
package model.beans;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.cfg.Configuration;
/**
* Configures and provides access to Hibernate sessions, tied to the
* current thread of execution. Follows the Thread Local Session
* pattern, see {@link http://hibernate.org/42.html }.
*/
public class HibernateSessionFactory {
/**
* 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 = "/hibernate.cfg.xml";
private static final ThreadLocal<Session> threadLocal = new ThreadLocal<Session>();
private static Configuration configuration = new Configuration();
private static org.hibernate.SessionFactory sessionFactory;
private static String configFile = CONFIG_FILE_LOCATION;
private HibernateSessionFactory() {
}
/**
* 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();
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() {
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) {
HibernateSessionFactory.configFile = configFile;
sessionFactory = null;
}
/**
* return hibernate configuration
*
*/
public static Configuration getConfiguration() {
return configuration;
}
}