hibernate session.save()不反映在数据库中 [英] hibernate session.save() does not reflect in database

查看:171
本文介绍了hibernate session.save()不反映在数据库中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据我对hibernate的理解(请确认)

1-您必须 session.close()如果你通过 getSessionFactory()。openSession()

获取它 - 不需要 session.close() 如果您通过 getSessionFactory()。getCurrentSession()获取它。它在 commit()后自动关闭。
使用 getSessionFactory()。getCurrentSession()时,我们可以使用

<3>必须在活动事务中执行所有数据库活动,以便我们可以在最后提交()。

$ b 4- Hibernate只有在 flush()后才会对所有保存,更新和删除操作进行排队并将它们提交给数据库服务器。 em> 操作或 提交 事件或 关闭会话 。 (根据javadoc)



如果我考虑 1 & 4 ,那么下面的代码应该可以工作:

  Session session = HibernateUtil.getSessionFactory()。openSession (); 
AccountDetails ac = new AccountDetails();
//执行设定操作
session.save(ac);
session.close();
System.out.println(新帐户存储);

它不起作用,即它运行平稳但不反映(存储)在数据库中。为什么这样呢?当我在一个事务中写入代码并提交时,它就被存储了。



我想我错过了一件基本的事情。请澄清。当您使用SessionFactory.openSession()创建会话时,不会创建任何事务,因此您的操作将在外部执行的事务上下文。为了看到你的变化,你必须开始一个新的交易,或作为持续交易的一部分执行你的操作。从文档:

一个典型的交易应该使用以下习惯用法:

  Session sess = factory.openSession(); 
交易tx;
尝试{
tx = sess.beginTransaction();
//做一些工作
...
tx.commit();

catch(Exception e){
if(tx!= null)tx.rollback();
throw e;
}
finally {
sess.close();
}


According to my understanding in hibernate (please confirm)

1- You have to session.close() if you get it by getSessionFactory().openSession().
2- No need to session.close() if you get it by getSessionFactory().getCurrentSession(). It is automatically closed after commit().

3- @2 When using getSessionFactory().getCurrentSession(), we have to do all DB activities inside an active transaction so that we can commit() at the end.

4- Hibernate en-queues all save, update, and delete operations and submits them to the database server only after a flush() operation or committing the transaction or closing of the session in which these operations occur.(as per javadoc)

From the above points if I consider 1 & 4, then the following code should work:

Session session = HibernateUtil.getSessionFactory().openSession();  
AccountDetails ac = new AccountDetails();  
//perform set operations  
session.save(ac);  
session.close();  
System.out.println("new account stored.");

BUT it is not working i.e. it runs smoothly but does not reflect(store) in database.Why this is so ? When I write the code inside a transaction and commit, then it is stored.

I think I am missing a basic thing. Please clarify.

解决方案

When you create session using SessionFactory.openSession(), no transaction is created, so your operations are executed outside of transaction context. In order to see your changes, you have to start a new transaction, or perform your operations as a part of ongoing transaction. From documentation:

A typical transaction should use the following idiom:

Session sess = factory.openSession();
 Transaction tx;
 try {
     tx = sess.beginTransaction();
     //do some work
     ...
     tx.commit();
 }
 catch (Exception e) {
     if (tx!=null) tx.rollback();
     throw e;
 }
 finally {
     sess.close();
 }

这篇关于hibernate session.save()不反映在数据库中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆