简体   繁体   English

休眠保存并使用相同的会话,相同的事务

[英]hibernate save and get using same session ,same transaction

I have a scenario service layer is transactional, where i can only commit after done trandaction. 我有一个场景服务层是事务性的,在其中我只能在完成事务之后提交。 i simplified it into like below. 我将其简化为如下所示。

begin transaction

for(loop){

getHibernateTemplate().save(object);


getHibernateTemplate().get(object_by_key); //cannot get object by object_by_key because "object" is not commit into database until entire for(loop) completed. 

}

end transaction. commit();

i try to put getHibernateTemplate().flush(), after save() and able to see "insert" in show_sql. 我尝试将getHibernateTemplate()。flush()放在save()之后,并能够在show_sql中看到“插入”。 but the record is not showing inside database. 但记录未显示在数据库内部。 how to force write to database after each save() rather than wait for commit like above ? 如何在每个save()之后强制写入数据库,而不是像上面那样等待提交?

getHibernateTemplate().flush() is the method to force hibernate to write to the database (send insert & update queries). getHibernateTemplate()。flush()是强制休眠模式写入数据库(发送插入和更新查询)的方法。 This is done within a transaction so it is not visible to other transactions (querying from a SQL client) till the transaction is committed. 这是在事务中完成的,因此在提交事务之前,其他事务(从SQL客户端查询)不可见。

If the insert query is showing up in the log then it has been sent to the database. 如果插入查询显示在日志中,则它已发送到数据库。 If you want to test that the record was inserted correctly - you can either do a getHibernateTemplate().clear() (which will remove all the cached data) and then do a getHibernateTemplate.get() (which will query from the dataSource). 如果要测试是否正确插入了记录,则可以执行getHibernateTemplate()。clear()(将删除所有缓存的数据),然后执行getHibernateTemplate.get()(将从数据源查询) 。 Or the other approach to test is to use the jdbcTemplate (with the same database) to query and check. 或者另一种测试方法是使用jdbcTemplate(具有相同的数据库)进行查询和检查。

If the SQL client tool you are using allows you to specify the Isolation level - starting the SQL client session in isolation read_uncommited - would allow you to see the changes done even before the transaction is commited. 如果您使用的SQL客户端工具允许您指定隔离级别-以read_uncommited隔离启动SQL客户端会话-甚至可以在提交事务之前查看已完成的更改。

Does getHibernateTemplate() return the same template each time? 每次getHibernateTemplate()返回相同的模板吗? Or if not a template based on a common session. 或者,如果不是基于公共会话的模板。 You might be better off saving the template to a local variable and reusing it, rather than calling getHibernateTemplate() each time. 您最好将模板保存到局部变量中并重用它,而不是每次都调用getHibernateTemplate()。

begin transaction

template = getHibernateTemplate();

for(loop){

template.save(object);


template.get(object_by_key); //cannot get object by object_by_key because "object" is not commit into database until entire for(loop) completed. 

}

end transaction. commit();

I think your problem might be because you are using a different session for the save and get calls. 我认为您的问题可能是因为您使用不同的会话进行保存和获取电话。 If they are made to the same session you should see the behaviour you want. 如果它们是在同一会话中进行的,则应该看到所需的行为。

As I know you can't do it at all. 据我了解,您根本无法做到。 It doesn't work. 没用 It is not possible to retrieve saved object in the same transaction. 无法在同一事务中检索保存的对象。 I think it is because the session is the first-level cache and Hibernate can obtain a new object only when the another transaction that creates that object commited. 我认为这是因为会话是一级缓存,并且Hibernate仅在提交另一个创建该对象的事务时才可以获取新对象。

Why do you need to get the object by key? 为什么需要按键获取对象? You already have the object! 您已经有了对象! If really you need the object key (for print it?) you can use a new transaction for each save operation, putting the transaction begin and commit in the loop. 如果确实需要对象键(用于打印吗?),则可以对每个保存操作使用一个新事务,将transaction begincommit到循环中。

暂无
暂无

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

相关问题 Java Hibernate save an object and update a different object using the same session in the same transaction only update and not save - Java Hibernate save an object and update a different object using the same session in the same transaction only update and not save 休眠:在同一会话中使用load()和get()选择相同的记录 - Hibernate: Selecting same record using load() and get() in the same Session 休眠-同一会话中的第二个事务不会保存修改的对象 - Hibernate - 2nd Transaction in same Session doesn't save modified object 在同一表的同一事务中绑定springjdbc和hibernate会话工厂 - binding springjdbc and hibernate session factory in same transaction on same table 使用 hibernate session 无事务获取 object - Get object using hibernate session without transaction Hibernate在同一事务中新创建的记录上的save()之后调用get() - Hibernate calling get() after save() on the newly created record within the same transaction 如何在同一休眠会话中从数据库表中获取更新的值 - How to get updated values from db table in the same hibernate session 两个EAR文件,相同的JPA实体管理器,同一个事务=>相同的会话? - Two EAR files, same JPA entitymanager, same transaction => same session? Hibernate JPA,在同一事务中删除和添加 - Hibernate JPA, deleting and adding in the same transaction 休眠查询未在同一事务中找到“待定”实体 - Hibernate query not finding “pending” entites in same transaction
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM