Hibernate4 中为什么我没有用commit()方法直接用save就存到数据库了? 求大神讲解下Hibernate4 中为什么我没有用commit()方法直接用save就存到数据库了? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 public class Main { public static void main(String[] args) { SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory(); Student student = new Student(); student.setAge(11); student.setName("save"); student.setSex("男"); student.setAcademicYear("99999"); Session session = sessionFactory.getCurrentSession(); Transaction ts = session.beginTransaction(); session.save(student); }} 貌似明白了 只要打印出 sql语句就 执行到数据库了?Hibernate: insert into student (_name, _age, _sex, _academicYear, _id) values (?, ?, ?, ?, ?)我把student.hbm.xml 中的<id name="id" column="_id"> <generator class="native"/></id>改为<id name="id" column="_id"></id>后再测试 只save()不commit()就不会对数据库 操作了 你是getCurrentSession的话自动提交但是你后面说的那种情况很奇怪,照理说和主键木有关系啊等大神解释 我测试下 getCurrentSession 改成openSession 试试 谢谢 public static void main(String[] args) { SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory(); Student student = new Student(); student.setAge(11); student.setName("save"); student.setSex("男"); student.setAcademicYear("99999"); //Session session = sessionFactory.getCurrentSession(); Session session = sessionFactory.openSession(); Transaction ts = session.beginTransaction(); //Student student2 = (Student)session.load(Student.class, 1); //System.out.println(student2.getName()); session.save(student); //ts.commit(); }貌似不行_id _name _age _sex _academicYear 1 save 11 男 99999 还是有数据 我前几天用4.1.8 session不是你这样取的啊!!! Session session = HibernateUtil.getSessionFactory().getCurrentSession(); session.beginTransaction(); Event theEvent = new Event(); theEvent.setTitle(title); theEvent.setDate(theDate); session.save(theEvent); session.getTransaction().commit();4.1.8 final 文档里就是上面的写法啊 怎么在用户的留言回复呀 你会结贴不呢?原因是数据源conn默认的提交方式,hibernate封装的jdbc,因为jdbc默认是的conn提交方式是true 这样 就应该不能提交了 public static void main(String[] args) throws Exception { SessionFactory sf=HibernateSessionFactory.getSessionFactory(); Session session=sf.openSession(); System.out.println(session.connection().getAutoCommit()); session.connection().setAutoCommit(false); User u=new User(); u.setName("aaa"); session.save(u); } hibernate4 好像变动很大、要看你hibernate文件是如何配置事务的、getCurrentSession();这样获取的session是就可以不用手动关闭了、其实跟session有关、我的猜测是 getCurrentSession() 获取到了你配置的事务、最后将数据库flush到了数据库中、以上也都是个人猜测而已、没有看hibernate4的源码、只晓得hibernate4相比hibernate3变动挺大的 该结贴 了总结下 测试的程序SessionFactory sessionFactory = HibernateUtil.getAnnotationSessionFactory(); Session session = sessionFactory.getCurrentSession(); Transaction ts = session.beginTransaction(); Husband h1 = new Husband(); h1.setName("w1"); Wife w1 = new Wife(); w1.setName("ll"); h1.setMyWife(w1); session.save(w1); session.save(h1); //session.clear() 在这调用 clear()commit后数据库有内容说明save 自动提交了 w1.setName("wwww"); w1.setName("qqq"); //session.clear() 在这里调用clear()不会访问数据库 缓存清空了 try{ ts.commit(); }catch(Exception e){ System.out.println("回滚"); ts.rollback(); }还有 我用了Annotation后 只save()不commit() 数据库里没有内容。用xml会有内容 可能是 scottxzj 说的吧 我在试试结贴 session.connection() 在 4.1.8 中已废除了 郁闷 失误 没有结贴 测试 失败!!!try { System.out.println(session.connection().getAutoCommit()); session.connection().setAutoCommit(false);} catch (HibernateException e1) { e1.printStackTrace();} catch (SQLException e1) { e1.printStackTrace();}测试结果falseHibernate: insert into Wife (name) values (?)Hibernate: insert into Husband (myWife_id, name) values (?, ?) struts2标签如何判断两时间之差? eclipse 3.7 中文乱码 flex与java通信使用blazeds 一个SQL语句问题 关于 InterruptedException 异常 面试成功,复试让我准备演讲稿 怎么将xml里面的<;变成< 提问: 在j2ee中,所有的本地Home接口都派生自名为()的公共接口?? sql service 在hibernate配置文件中有什么写错的么 为什么Cannot open connection 现在最流行的语言是什么哪?想知道就进来喝一杯 J2EE整合三大框架后采用中文查询报错 关于JAVA中使用log4J,不用配置文件,卡主了
public static void main(String[] args) {
SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
Student student = new Student();
student.setAge(11);
student.setName("save");
student.setSex("男");
student.setAcademicYear("99999");
Session session = sessionFactory.getCurrentSession();
Transaction ts = session.beginTransaction();
session.save(student);
}
}
Hibernate: insert into student (_name, _age, _sex, _academicYear, _id) values (?, ?, ?, ?, ?)我把student.hbm.xml 中的
<id name="id" column="_id">
<generator class="native"/>
</id>改为
<id name="id" column="_id">
</id>后
再测试 只save()不commit()就不会对数据库 操作了
但是你后面说的那种情况很奇怪,照理说和主键木有关系啊
等大神解释
SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
Student student = new Student();
student.setAge(11);
student.setName("save");
student.setSex("男");
student.setAcademicYear("99999");
//Session session = sessionFactory.getCurrentSession();
Session session = sessionFactory.openSession();
Transaction ts = session.beginTransaction();
//Student student2 = (Student)session.load(Student.class, 1);
//System.out.println(student2.getName());
session.save(student);
//ts.commit();
}貌似不行
_id _name _age _sex _academicYear
1 save 11 男 99999
还是有数据
session.beginTransaction(); Event theEvent = new Event();
theEvent.setTitle(title);
theEvent.setDate(theDate);
session.save(theEvent); session.getTransaction().commit();
4.1.8 final 文档里就是上面的写法啊
怎么在用户的留言回复呀
这样 就应该不能提交了 public static void main(String[] args) throws Exception {
SessionFactory sf=HibernateSessionFactory.getSessionFactory();
Session session=sf.openSession();
System.out.println(session.connection().getAutoCommit());
session.connection().setAutoCommit(false);
User u=new User();
u.setName("aaa");
session.save(u);
}
最后将数据库flush到了数据库中、以上也都是个人猜测而已、没有看hibernate4的源码、只晓得hibernate4相比hibernate3变动挺大的
Session session = sessionFactory.getCurrentSession();
Transaction ts = session.beginTransaction();
Husband h1 = new Husband();
h1.setName("w1");
Wife w1 = new Wife();
w1.setName("ll");
h1.setMyWife(w1);
session.save(w1);
session.save(h1);
//session.clear() 在这调用 clear()commit后数据库有内容说明save 自动提交了
w1.setName("wwww");
w1.setName("qqq");
//session.clear() 在这里调用clear()不会访问数据库 缓存清空了
try{
ts.commit();
}catch(Exception e){
System.out.println("回滚");
ts.rollback();
}还有 我用了Annotation后 只save()不commit() 数据库里没有内容。用xml会有内容 可能是 scottxzj 说的吧 我在试试结贴
try {
System.out.println(session.connection().getAutoCommit());
session.connection().setAutoCommit(false);
} catch (HibernateException e1) {
e1.printStackTrace();
} catch (SQLException e1) {
e1.printStackTrace();
}
测试结果
false
Hibernate:
insert
into
Wife
(name)
values
(?)
Hibernate:
insert
into
Husband
(myWife_id, name)
values
(?, ?)