Hibernate:对象已删除但仍可由Hibernate加载

时间:2013-08-23 19:50:06

标签: java mysql sql hibernate

如果删除对象后我甚至可以加载它并打印其ID,这似乎很荒谬。这是我用来从Prof实体检索数据的类:

public class ProfCrud {
SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
Session session ; 

 public ArrayList<Object> findProfByProfId( int id){
     session = sessionFactory.openSession();  
    Query Q = session.getNamedQuery("findProfByProfId");
    Q.setLong("id", id);
    List<Object> objet = Q.list();
    session.close(); 
    return (ArrayList<Object>) objet;   
}
public void deleteProfByProfId(int id){
    ProfCrud pc= new ProfCrud();

            session = sessionFactory.openSession();  
    session = HibernateUtil.getSessionFactory().openSession();
    Transaction transaction = null;
    try {
        transaction = session.beginTransaction();     
        Prof c = (Prof) pc.findProfByProfId(id).get(0);
                session.delete(c);
            transaction.commit();
    } catch (HibernateException e) {
        transaction.rollback();
        e.printStackTrace();
    } finally {
        session.close();
    }   

    System.out.println(((Prof) pc.findProfByProfId(id).get(0)).getId());
}

}

1 个答案:

答案 0 :(得分:0)

在打印之前我打开了一个会话,启动了一个事务并关闭了它。现在我无法加载该对象。我不知道为什么但是如果任何人能够澄清它我会感激的话就有用了。

session = sessionFactory.openSession();  
    session = HibernateUtil.getSessionFactory().openSession();
    transaction = session.beginTransaction();     
    transaction.commit();
    session.close();