我有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)

最佳答案

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

09-04 07:44