簡體   English   中英

org.hibernate.HibernateException:兩個打開的會話

[英]org.hibernate.HibernateException: two open sessions

我是休眠技術的新手,但遇到以下例外:

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,您可以從代碼中看到以下內容:

而不是使用

openSession()

嘗試

getSession() 

URL解決了類似問題

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM