简体   繁体   English

休眠 session.save() 不反映在数据库中

[英]hibernate session.save() does not reflect in database

According to my understanding in hibernate (please confirm)根据我在hibernate中的理解(请确认)

1- You have to session.close() if you get it by getSessionFactory().openSession() . 1- 如果你通过getSessionFactory().openSession()得到它,你必须session.close() getSessionFactory().openSession()
2- No need to session.close() if you get it by getSessionFactory().getCurrentSession() . 2- 如果您通过getSessionFactory().getCurrentSession()获得它,则不需要session.close() getSessionFactory().getCurrentSession() It is automatically closed after commit() .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. 3- @2使用getSessionFactory().getCurrentSession() ,我们必须在活动事务中执行所有数据库活动,以便我们可以在最后提交()。

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) 4- Hibernate 将所有保存、更新和删除操作排入队列,并仅在执行flush()操作或提交事务或关闭发生这些操作的会话后才将它们提交到数据库服务器。(根据 javadoc)

From the above points if I consider 1 & 4 , then the following code should work:从以上几点,如果我考虑1 & 4 ,那么下面的代码应该可以工作:

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 ie 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.当您使用 SessionFactory.openSession() 创建会话时,不会创建任何事务,因此您的操作是在事务上下文之外执行的。 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();
 }

I was also trying to understand the point: save() can perform Insert operation outside transaction boundary so I did something like this我也试图理解这一点: save() 可以在事务边界外执行插入操作,所以我做了这样的事情

SessionFactory factory = new Configuration().configure()
                    .buildSessionFactory();
            Session session = factory.openSession();

            session.save(user);

            session.close();

But data not inserted in database.So I again tried this and now it worked for me and data inserted sucessfully:但是数据没有插入数据库中。所以我再次尝试了这个,现在它对我有用并且数据插入成功:

in configuration file:在配置文件中:

  <property name="connection.autocommit">true</property>

and in java code:并在java代码中:

    SessionFactory factory = new Configuration().configure()
            .buildSessionFactory();
    Session session = factory.openSession();

    session.save(user);

    session.flush();
    session.close();

Yeah session.save is not saved to database without have the transaction.是的session.save在没有事务的情况下不会保存到数据库中。 Only way to do it is by using the唯一的方法是使用

<property name="connection.autocommit">true</property>

If this property is used you no need transaction and also you do not need session.flush() or session.close()如果使用此属性,则不需要事务,也不需要session.flush()session.close()

session.close() is only responsible to close the session https://docs.jboss.org/hibernate/orm/3.5/javadocs/org/hibernate/Session.html#close() session.close() 只负责关闭会话https://docs.jboss.org/hibernate/orm/3.5/javadocs/org/hibernate/Session.html#close()

how to work with hibernate session: https://docs.jboss.org/hibernate/orm/3.3/reference/en/html/transactions.html#transactions-basics如何使用休眠会话: https : //docs.jboss.org/hibernate/orm/3.3/reference/en/html/transactions.html#transactions-basics

// Non-managed environment idiom
Session sess = factory.openSession();
Transaction tx = null;
try {
    tx = sess.beginTransaction();

    // do some work
    ...

    tx.commit();
}

catch (RuntimeException e) {
    if (tx != null) tx.rollback();
    throw e; // or display error message
}
finally {
    sess.close();
}

As it wrote in the documentation: a much more flexible solution is Hibernate's built-in "current session" context management:正如它在文档中所写:一个更灵活的解决方案是 Hibernate 的内置“当前会话”上下文管理:

// Non-managed environment idiom with getCurrentSession()
try {
    factory.getCurrentSession().beginTransaction();

    // do some work
    ...

    factory.getCurrentSession().getTransaction().commit();
}
catch (RuntimeException e) {
    factory.getCurrentSession().getTransaction().rollback();
    throw e; // or display error message
}

Whenever you carry out any unit of work on the database objects, Transactions have to be used.每当您对数据库对象执行任何工作单元时,都必须使用事务。 http://docs.jboss.org/hibernate/orm/4.0/hem/en-US/html/transactions.html shows why they are used and how they can be used. http://docs.jboss.org/hibernate/orm/4.0/hem/en-US/html/transactions.html展示了为什么使用它们以及如何使用它们。 But, the key is to use a transaction object by calling session.beginTransaction() which returns a Transaction object.但是,关键是通过调用session.beginTransaction()来使用一个transaction对象,它返回一个Transaction对象。 This will represent the unit of work carried out to the database.这将代表对数据库执行的工作单元。

I found I had to flush and then refresh the DAO, and then it worked.我发现我必须刷新然后刷新 DAO,然后它才起作用。

session.flush()
session.refresh(entityDAO)

You forgot to commit .You should to commit.你忘了提交。你应该提交。

session.save(ac);
// you should to add this bottom line  
session.getTransaction().commit();

Whenever Your are doing save(), Always commit the transaction once done.每当你在做 save() 时,一旦完成,总是提交事务。

Account ac =new account()
ac.insert(123);
----------
----------
----------
----------
session.save(ac);

// Add this bottom line  
session.getTransaction().commit();
if there is DB manipulation operation then it should be in transaction
if you call session.save(object)
hibernate create bellow query only .
select hibernate_sequence.nextval from dual
so right code is here
Transaction  t=session.beginTransaction();
session.save(obect);
t.commit(); should not forgot to commit.
session.close();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM