Hibernate session.delete()问题
刚学Hibernate,发现删除不了问题,来这里请教下大家。代码为:packagecom;importorg.hibernate.Session;publicclassD...
刚学Hibernate,发现删除不了问题,来这里请教下大家。
代码为:
package com;
import org.hibernate.Session;
public class DeleteUser {
public static void main(String[] args) {
Session session=HibernateUtil.getSession();
try{
User user1=(User)session.get(User.class, new Integer("2"));
session.delete(user1);
session.flush();
}catch(Exception e){
System.out.println("对象删除失败");
e.printStackTrace();
}finally{
HibernateUtil.closesession();
}
}
}
控制台提示为:
Hibernate: select user0_.id as id0_0_, user0_.name as name0_0_, user0_.sex as sex0_0_, user0_.password as password0_0_ from user user0_ where user0_.id=?
Hibernate: delete from user where id=?
但去数据库查看,ID为2的user没被删除掉。
请大家帮忙下。
将session.flush();删除,换为:
session.beginTransaction();
session.delete(user1);
session.getTransaction().commit();
能正确删除数据,
为什么呢? 展开
代码为:
package com;
import org.hibernate.Session;
public class DeleteUser {
public static void main(String[] args) {
Session session=HibernateUtil.getSession();
try{
User user1=(User)session.get(User.class, new Integer("2"));
session.delete(user1);
session.flush();
}catch(Exception e){
System.out.println("对象删除失败");
e.printStackTrace();
}finally{
HibernateUtil.closesession();
}
}
}
控制台提示为:
Hibernate: select user0_.id as id0_0_, user0_.name as name0_0_, user0_.sex as sex0_0_, user0_.password as password0_0_ from user user0_ where user0_.id=?
Hibernate: delete from user where id=?
但去数据库查看,ID为2的user没被删除掉。
请大家帮忙下。
将session.flush();删除,换为:
session.beginTransaction();
session.delete(user1);
session.getTransaction().commit();
能正确删除数据,
为什么呢? 展开
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询