eclipse Hibernate:非法尝试将代理与两个打开的会话相关联

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14504591/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 19:51:18  来源:igfitidea点击:

Hibernate: illegally attempted to associate a proxy with two open Sessions

javaeclipsehibernatetomcat

提问by Pishist

I have 2 methods:

我有两种方法:

public static Ticket issueTicket(User user,Service service,String[] seats) {
    Session ticSess= DB.factory.openSession();
    ticSess.beginTransaction();
    Date d= new Date();
    Ticket ticket=new Ticket(d, service, user);

    ticSess.save(ticket);
    ticSess.getTransaction().commit();
    int seatCount=seats.length;
    for (int i=0;i<seatCount;i++){
        int seatID=Integer.parseInt(seats[i]);
        Seat seat=getSeatByID(seatID);
        seat.setTicket(ticket);
        ticSess.update(seat);
    }
    return ticket;


}

and,

和,

public static Seat getSeatByID(int seatID) {
    Session proSess = DB.factory.openSession();
    proSess.beginTransaction();
    Seat c = (Seat) (proSess.load(Seat.class, seatID));
    proSess.getTransaction().commit();

    return c;
}

when I call issueTicket method I get:

当我调用 issueTicket 方法时,我得到:

illegally attempted to associate a proxy with two open Sessions

and If I close the session in getSeatByID method there will be another error telling that the session is closed. Here is the Stack Trace:

如果我在 getSeatByID 方法中关闭会话,则会出现另一个错误,表明会话已关闭。这是堆栈跟踪:

at org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:164)
at org.hibernate.proxy.AbstractLazyInitializer.getImplementation(AbstractLazyInitializer.java:285)
at org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer.invoke(JavassistLazyInitializer.java:185)
at ir.ac.shirazu.cse.Terminal.Seat_$$_javassist_9.setTicket(Seat_$$_javassist_9.java)
at ir.ac.shirazu.cse.Database.DB.issueTicket(DB.java:231)

采纳答案by Adam Dyga

Try closing proSessin getSeatByID()before returning. Currently the Seatindeed remains attached to session opened in getSeatByID().

请尝试关闭proSessgetSeatByID()返回之前。目前Seat确实仍然附加到在 getSeatByID().

回答by Harsha

I got same problem . But after using singleton pattern for session i'm done. I'm using Hibernate 4.2.x.

我遇到了同样的问题。但是在会话中使用单例模式后,我就完成了。我正在使用 Hibernate 4.2.x。

This is my session class is used to get sessions for DB transactions etc.

这是我的会话类,用于获取数据库事务等的会话。

public class SessionClass {

static Session session = PoolManager.getSession();

public static Session getSession() {
    if (session != null || session.isOpen()) {
        return session;
    } else {
        session = PoolManager.getSession();
        return session;
    }
}

}

}

Hibernate Helper Class I'm using.

我正在使用的 Hibernate Helper 类。

public class PoolManager {
private static final SessionFactory sessionFactory;
private static final ServiceRegistry serviceRegistry;

static {
    try {
        // Create the SessionFactory from standard (hibernate.cfg.xml) 
        // config file.
        Configuration configuration = new Configuration();
        configuration.configure();

        serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry();
        sessionFactory = configuration.buildSessionFactory(serviceRegistry);
    } catch (Throwable ex) {
        // Log the exception. 
        System.err.println("Initial SessionFactory creation failed." + ex);
        throw new ExceptionInInitializerError(ex);
    }
}

public static Session getSession() {
    return sessionFactory.openSession();
}

}

}

回答by Moa

I ran into this issue when trying to associate a entity from a Envers Session.

我在尝试从 Envers 会话关联实体时遇到了这个问题。

Fixed that by "refreshing" said entity (retrieving it on my non-Envers session via PK fetch) before bubbling it up to my algorithm.

通过“刷新”所述实体(通过 PK 获取在我的非 Envers 会话中检索它)在将其冒泡到我的算法之前修复了该问题。

回答by shaik Shaik

use session.opensession().get(.....).. instead of session.opensession().load(.....)

使用session.opensession().get(.....).. 而不是session.opensession().load(.....)

回答by Farzad

what if you do a proSess.evict(c) before committing proSess?

如果您在提交 proSess 之前执行 proSess.evict(c) 会怎样?