1.关于hibernate的save方法保存失败的原因,没有保存Session,只是利用hibernateUtils.opensession()去获取session。

错误的代码如下:

        //没有将session保存下来
        Transaction tc = HibernateUtil.openSession().beginTransaction();
        Person person = new Person();
        person.setMoney(1000);
	person.setName("1111111");
	HibernateUtil.openSession().save(person);
	HibernateUtil.openSession().flush();
        tc.commit();

修改后,正确的代码如下:

        //将session保存下来
        Session session = HibernateUtil.openSession();
        Transaction tx = session.beginTransaction();
        Person person = new Person();
        person.setMoney(1000);
        person.setName("1111111");
        session.save(person);
        tx.commit();

 

     

相关文章:

  • 2021-07-22
  • 2022-12-23
  • 2021-10-05
  • 2022-12-23
  • 2021-09-20
  • 2021-06-05
  • 2021-11-28
猜你喜欢
  • 2022-12-23
  • 2021-05-25
  • 2022-12-23
  • 2021-11-23
  • 2022-12-23
  • 2021-10-20
  • 2022-12-23
相关资源
相似解决方案