简体   繁体   English

org.hibernate.HibernateException:两个打开的会话

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

I am newbie in hibernate technology and I am struggling with the following exception: 我是休眠技术的新手,但遇到以下例外:

org.hibernate.HibernateException: Illegal attempt to associate a collection with two open sessions org.hibernate.HibernateException:非法尝试将一个集合与两个打开的会话相关联

I get this when I try to make a new line into my DB. 当我尝试在数据库中添加新行时,会出现此错误。

My setting/code: 我的设置/代码:

<?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>

A session holder: 会议主持人:

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();
    }
}

Code that causes the error : 导致错误的代码:

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);
        }

Welcome to Hibernate, what I can see from your code: 欢迎使用Hibernate,您可以从代码中看到以下内容:

Instead of using 而不是使用

openSession()

try 尝试

getSession() 

Similar issue solved at URL URL解决了类似问题

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 春季-&gt; org.hibernate.HibernateException:非法尝试将一个集合与两个打开的会话相关联 - spring -> org.hibernate.HibernateException: Illegal attempt to associate a collection with two open sessions “org.hibernate.HibernateException:非法尝试将集合与两个打开的会话相关联”,即使上下文是线程 - "org.hibernate.HibernateException: Illegal attempt to associate a collection with two open sessions" even when the context is Thread 删除函数org.hibernate.HibernateException:非法尝试将一个集合与两个打开的会话相关联 - Delete Function org.hibernate.HibernateException: Illegal attempt to associate a collection with two open sessions org.hibernate.HibernateException错误 - org.hibernate.HibernateException error 基本的休眠程序中的org.hibernate.HibernateException - org.hibernate.HibernateException in a basic hibernate program org.hibernate.HibernateException:当前事务不在进行中 - org.hibernate.HibernateException: Current transaction is not in progress org.hibernate.HibernateException:缺少表:全部 - org.hibernate.HibernateException: Missing table: all org.hibernate.HibernateException:无法实例化resultclass - org.hibernate.HibernateException: Could not instantiate resultclass org.hibernate.HibernateException:无法获得连接 - org.hibernate.HibernateException: Not able to obtain connection org.hibernate.HibernateException:找不到会话 - org.hibernate.HibernateException: No Session found
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM