Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp
Hibernate version: 3.0.5
Mapping documents:
<!--##############################
HIBERNATE CONFIG
##############################-->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource"><ref local="dataSource"/></property>
<property name="hibernateProperties"><ref local="hibernateProperties"/></property>
<property name="mappingResources"><ref bean="mappingResources" /></property>
</bean>
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName"><value>org.postgresql.Driver</value></property>
<property name="url"><value>jdbc:postgresql://localhost:5432/SIMAGE</value></property>
<property name="username"><value>postgres</value></property>
<property name="password"><value>sspw2005</value></property>
</bean>
<bean id="hibernateProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
<property name="properties">
<props>
<prop key="hibernate.connection.username">xxxx</prop>
<prop key="hibernate.connection.password">xxxx</prop>
<prop key="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</prop>
<prop key="hibernate.show_sql">false</prop>
</props>
</property>
</bean>
<bean id="mappingResources" class="java.util.ArrayList">
<constructor-arg>
<list>
<value>/hibernate/User.hbm.xml</value>
<value>/hibernate/Project.hbm.xml</value>
<value>/hibernate/TreeNode.hbm.xml</value>
</list>
</constructor-arg>
</bean>
<bean id="HibernateUtil" class="br.com.simageWeb.util.HibernateUtil">
<property name="sessionFactory"><ref bean="sessionFactory"/></property>
</bean>
<!--##############################
PROJECT MAPPING
##############################-->
<?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>
<class name="br.com.simageWeb.models.Project" table='"SIM_PROJECT"'>
<id name="pkProject" type="long" unsaved-value="null" >
<column name="pkproject" sql-type="integer" not-null="true"/>
<generator class="increment">
<param name="sequence">seq_project</param>
</generator>
</id>
<property name="name">
<column name="name" length="50"/>
</property>
<property name="mission">
<column name="mission" length="50"/>
</property>
<property name="problem">
<column name="problem" length="50"/>
</property>
<property name="level">
<column name="level"/>
</property>
<property name="excluded">
<column name="excluded"/>
</property>
<property name="type">
<column name="type"/>
</property>
<property name="launchingType">
<column name="launchingtype"/>
</property>
<property name="simulationsCount">
<column name="simulationscount"/>
</property>
<set name="users" table='"SIM_USERPROJECT"'>
<key column="fkproject"/>
<many-to-many column="fkuser" class="br.com.simageWeb.models.User"/>
</set>
<set name="SCFs" cascade="all" table='"SIM_PROJECTSCF"'>
<key column="fkproject"/>
<many-to-many column="fktreenode" class="br.com.simageWeb.models.TreeNode"/>
</set>
</class>
</hibernate-mapping>
Code between sessionFactory.openSession() and session.close():
public class HibernateUtil {
private static Log log = LogFactory.getLog(HibernateUtil.class);
private static SessionFactory sessionFactory;
public static final ThreadLocal session = new ThreadLocal();
public static Session currentSession() {
Session s = (Session) session.get();
// Open a new Session, if this Thread has none yet
if (s == null) {
s = sessionFactory.openSession();
session.set(s);
}
return s;
}
public static void closeSession() {
Session s = (Session) session.get();
if (s != null)
s.close();
session.set(null);
}
public SessionFactory getSessionFactory() {
return HibernateUtil.sessionFactory;
}
public void setSessionFactory(SessionFactory sessionFactory) {
HibernateUtil.sessionFactory = sessionFactory;
}
}
public class ProjectDaoPostgres {
....
public boolean store(Object o) {
Session session = HibernateUtil.currentSession();
Transaction tx = session.beginTransaction();
session.persist(o);
tx.commit();
return true;
}
...
public Iterator retrieveAll() {
Session session = HibernateUtil.currentSession();
Transaction tx = session.beginTransaction();
Query query = session.createQuery("from Project where excluded != 1 order by name");
tx.commit();
return query.iterate();
}
...
}
Full stack trace of any exception that occurs:
Name and version of the database you are using:
PostgreSQL 8.0.1
The generated SQL (show_sql=true):
Debug level Hibernate log excerpt: