我在休眠技术的新手,我有以下异常艰难:org.hibernate.HibernateException:两次公开会议
org.hibernate.HibernateException:非法尝试到 集合有两个打开的会话关联
我得到这个,当我尝试做一个新的行到我的数据库。
我的设置/代码:
<?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>
<!-- Paramètres de connexion à la base de données -->
<!-- <property name="connection.driver_class">com.mysql.jdbc.Driver</property> -->
<!-- <property name="connection.url">jdbc:mysql://localhost:3306/bh</property> -->
<!-- <property name="connection.username">root</property> -->
<!-- <property name="connection.password"></property> -->
<!-- <property name="dialect">org.hibernate.dialect.MySQLDialect</property> -->
<property name="connection.driver_class">org.postgresql.Driver</property>
<property name="connection.url">jdbc:postgresql://localhost:5432/projetForum</property>
<property name="connection.username">postgres</property>
<property name="connection.password">esct</property>
<property name="dialect">org.hibernate.dialect.PostgreSQLDialect</property>
<!-- Comportement pour la conservation des tables -->
<property name="hbm2ddl.auto">update</property>
<!-- Activation : affichage en console, commentées et formatées -->
<property name="show_sql">true</property>
<property name="hibernate.format_sql">true</property>
<property name="use_sql_comments">true</property>
<!-- Fichiers à mapper -->
<mapping class="com.forum.beans.Utilisateur" />
<mapping class="com.forum.beans.Topic" />
<mapping class="com.forum.beans.Post" />
</session-factory>
</hibernate-configuration>
会话持有人:导致错误
package com.forum.utils;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
public class HibernateUtils {
private static final SessionFactory sessionFactory;
// Crée une unique instance de la SessionFactory à partir de
// hibernate.cfg.xml
static {
try {
sessionFactory = new AnnotationConfiguration().configure()
.buildSessionFactory();
} catch (HibernateException ex) {
throw new RuntimeException("Problème de configuration : "
+ ex.getMessage(), ex);
}
}
// Renvoie une session Hibernate
public static Session getSession() throws HibernateException {
return sessionFactory.openSession();
}
}
代码:
Transaction tx = null;
try {
s = HibernateUtils.getSession();
tx = s.beginTransaction();
s.persist(u);
tx.commit();
} catch (Exception e) {
if (tx != null)
tx.rollback();
System.out.println(e);
}
您还没有指定你是如何得到ü对象解决。我觉得你是通过hibernate加载它,并且它仍然连接到另一个会话。 –
很容易在这里发布,并得到一个快速解决这个问题,比如果你再次遇到一个奇怪的行为,而不是一次又一次地发布。如果你想学习:当Singleton(设计模式)构造2个对象或更多时,去检查哪些是这些情况。几乎可以肯定你现在遇到你的那种情况之一。请不要告诉我一个Singleton是Singleton,因为只有一个是他的。它应该只有一个,但我可以编写一个代码,这使得其中的2个:) – 2013-08-16 19:52:23
看起来好像你正在坚持一个已经绑定到另一个会话的对象,试着使用sessionFactory.getCurrentSession()而不是sessionFactory.openSession() )并查看它是否正常工作,您是否使用OpenSessionInViewFilter过滤器? –