【发布时间】:2014-05-14 11:33:21
【问题描述】:
为什么我们总是需要在休眠中启动事务来保存、插入、删除或更新?
hibernate的自动提交功能默认为false吗?
喜欢这个
public static void main(String[] args) {
System.out.println("creating empopbjects");
emp e1=new emp("a","x",1234);
emp e2=new emp("b","y",324);
emp e3=new emp("c","z",23345);
System.out.println("saving emp objects..");
Session s=myfactory.getsession();
s.save(e1);
s.save(e2);
s.save(e3);
s.close();
System.out.println("successfully saved");
}
这不会保存任何东西,而如果我添加 Transaction 则只会添加它?为什么会这样?
public static void main(String[] args) {
System.out.println("creating empopbjects");
emp e1=new emp("a","x",1234);
emp e2=new emp("b","y",324);
emp e3=new emp("c","z",23345);
System.out.println("saving emp objects..");
Session s=myfactory.getsession();
Transaction t =s.beginTransaction();
s.save(e1);
s.save(e2);
s.save(e3);
t.commit();
s.close();
System.out.println("successfully saved");
}
【问题讨论】:
标签: java hibernate jpa orm transactions