Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I implement Unit Of Work in Spring & JPA/Hibernate?

I am trying to get my head around the concept of UOW and how I can implement it in my spring/jpa/hibernate app with DDD. I make my methods transactional, but how do I make sure that all the entities that have been changed in the transaction get persisted? Is it by using cascading collections or is there another way?

like image 839
Piotr Avatar asked Nov 11 '10 13:11

Piotr


2 Answers

Hibernate implements Unit of Work internally. That is, all changes made to persistent entities inside a transaction are persisted automatically (unless you have a transaction with readOnly = true).

Cascading options are needed to configure transition of graphs of transient or detached entities to persistent state.

See also:

  • 11.1. Hibernate object states
like image 107
axtavt Avatar answered Oct 03 '22 17:10

axtavt


how do I make sure that all the entities that have been changed in the transaction get persisted?

That's default behaviour for the JPA EntityManager.

Read this section of the Java EE 5 tutorial (all the way down to Synchronizing Entity Data to the Database)

like image 21
Sean Patrick Floyd Avatar answered Oct 03 '22 19:10

Sean Patrick Floyd