I've got many problems when I try to use Hibernate 3.1 in a J2EE web application.
To solve my problem I wrote a little test on my persistance and when I launch it, this exception is returned :
Code:
Exception in thread "main" org.hibernate.SessionException: Session was already closed
at org.hibernate.impl.SessionImpl.close(SessionImpl.java:270)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.hibernate.context.ThreadLocalSessionContext$TransactionProtectionWrapper.invoke(ThreadLocalSessionContext.java:301)
at $Proxy0.close(Unknown Source)
at persistance.TestPersist.main(TestPersist.java:46)
There is my simple test :
Code:
public static void main(String[] args) {
Authentification auth = new Authentification();
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
Transaction tx1 = session.beginTransaction();
User us = new User();
us.setNom("Martin");
us.setPrenom("Philippe");
us.setAdresse("18, rue des cerisiers");
us.setCp(63000);
us.setVille("Clermont Ferrand");
us.setTel("0473658941");
us.setMailPerso("lekaille@gmail.com");
auth.setLogin("toto");
auth.setPass("tintin");
session.persist(auth);
us.setAuthentification(auth);
session.persist(us);
tx1.commit();
session.close();
}
My Hibernate configuration :
Code:
hibernate-configuration>
<session-factory >
<!-- local connection properties -->
<property name="hibernate.connection.url">jdbc:postgresql://localhost:5432/transfert</property>
<property name="hibernate.connection.driver_class">org.postgresql.Driver</property>
<property name="hibernate.connection.username">postgres</property>
<property name="hibernate.connection.password">******</property>
<!-- property name="hibernate.connection.pool_size"></property -->
<!-- dialect for PostgreSQL -->
<property name="dialect">org.hibernate.dialect.PostgreSQLDialect</property>
<property name="hibernate.show_sql">true</property>
<property name="hibernate.transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</property>
<property name="hibernate.jdbc.batch_size">0</property>
<property name="hibernate.sql_show">true</property>
<!-- Enable Hibernate's automatic session context management -->
<property name="current_session_context_class">thread</property>
<mapping resource="persistance/personne.hbm.xml"></mapping>
<mapping resource="persistance/fichier.hbm.xml"></mapping>
<mapping resource="persistance/authentification.hbm.xml"></mapping>
<mapping resource="persistance/mail.hbm.xml"></mapping>
I don't understand why it doesn't work correctly ?