The Author Online Book Forums are Moving

The Author Online Book Forums will soon redirect to Manning's liveBook and liveVideo. All book forum content will migrate to liveBook's discussion forum and all video forum content will migrate to liveVideo. Log in to liveBook or liveVideo with your Manning credentials to join the discussion!

Thank you for your engagement in the AoF over the years! We look forward to offering you a more enhanced forum experience.

iiiles (2) [Avatar] Offline
#1
Dear Chris!
Could you explain one thing about transparent persistence I did not catch? When the PendingOrder updated how the changes are posted to DB? Whose duty to call the save() method on Hibernate? Is it Spring when it closes transaction after facade method returns success? Thank You!
iiiles (2) [Avatar] Offline
#2
Re: transparent persistence
To be more exact, after PendingOrder updated:

public int updateDeliveryInfo(
RestaurantRepository rr,
Address deliveryAddress,
Date deliveryTime, boolean force) {
..............................................

this.deliveryAddress = deliveryAddress;
this.deliveryTime = deliveryTime;
................................................
}

at what moment the changes are in DB?
simbo1905 (30) [Avatar] Offline
#3
Re: transparent persistence
With hibernate (or the hibernate JPA implimentation) when using the native hibernate transaction manager (via spring) the data goes into the db only when the transaction is committed. At which point Hibernate takes all the data changes buffered on the objects bound to the hibernate session and flushes them down jdbc inside a single phase jdbc transaction.

This means that if you are stepping through a debugger and seeing hibernate log out the sql it would write out it is not updating the database yet - you have to let the transaction commit to see the data at the database. If you want to see every detailed write when it happens you can configure a jdbc log driver which logs the low level sql work that is really happening at the driver level.

If you need to write code that also requires native jdbc or procs to work on the data during the same transaction you have to call session.flush() on the hibernate session to force it to push to the db: in which case you need to be using a JTA transaction manager and a global transaction to ensure that if there is an exception all of the work can be rolled back at the database.