2

我有两种方法:

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;


}

和,

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

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

illegally attempted to associate a proxy with two open Sessions

如果我在 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)
4

5 回答 5

0

我有同样的问题。但是在使用单例模式进行会话之后,我就完成了。我正在使用休眠 4.2.x。

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

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

}

我正在使用的休眠助手类。

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

}

于 2014-09-03T06:27:37.443 回答
0

proSessgetSeatByID()返回之前尝试关闭。目前,Seat确实仍然附加到在 中打开的会话 getSeatByID()

于 2013-01-24T15:17:48.710 回答
0

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

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

于 2016-04-13T20:28:21.180 回答
0

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

于 2017-08-21T14:32:00.903 回答
-1

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

于 2013-01-24T15:22:25.950 回答