简体   繁体   中英

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. but the record is not showing inside database. how to force write to database after each save() rather than wait for commit like above ?

getHibernateTemplate().flush() is the method to force hibernate to write to the database (send insert & update queries). This is done within a transaction so it is not visible to other transactions (querying from a SQL client) till the transaction is committed.

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). Or the other approach to test is to use the jdbcTemplate (with the same database) to query and check.

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.

Does getHibernateTemplate() return the same template each time? 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.

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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