即使在我删除了对象之后,我仍然可以加载它并打印它的 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());
}
}