Java – JPA hibernate – loop in entity relationship – cascade strategy
I have a group of entities connected to each other to form a cycle, that is, the parent entity P has two one to many relationships with two child entities C1 and C2, and each has one to many relationships with another entity. A. entity a implements the association of these entities (C1, C2) and defines the attributes of the relationship (not just the connection table) All relationships can navigate in both directions
The following question comes from this design: since you always call the entity manager operation on the root entity P, how should the cascade policy be persisted / merged? Should we take two routes to the cascade?
Note: if the application chooses to provide only one cascading path, a transient objectexception may be thrown If it provides two paths, these paths must go through a complete cycle, because you can try to save example C1 through a
Version: JPA 2.0, hibernate kernel 4.1 7,hibernate-jpa-2.0-api 1.0. one
Solution
I can give you my two cents. I'm sorry if my answer is a little long
If you have such a cascading conflict, it may be because your cascading method or domain model is not clearly defined I will carefully extend a cascading strategy to a whole graph, or a set of unrelated elements
My suggestion is that the cascading strategy should be used only for strongly bound data sets, and similar to the same types of classes and their (private) internal classes in the Java world The relationship between mother class and children should also be exclusive (in UML, it is called non shared Association)
Of course, you can do other things (we can all be lazy), but eventually you can create a coupling network between a single persistent flow (or persistence configuration) and your business flow You will have to manage a large number of exceptions and perform a large number of configuration logic in the cascading policies you previously placed (save, update, delete)
At the extreme, some people may want to save only one large root object Why not? The rest "should stick to it" But in fact, this may seriously limit the maintainability of the system In addition, you may need to manage the state of large graphics in memory when loading, saving and merging
If you execute a web application or any client server application, the web workflow should be able to save a limited set of objects in each request without saving everything from the root element I know I didn't answer your question directly So let's go back to your example:
P is a bank, C1 and C2 are two customers, and a is a product
I have two simple answers: 1) each layer can be saved separately without any cascade But it can be done in the same transaction, or in the same Dao if you want
2) P and C can be cascaded But a must be saved in a different workflow
This reminds me of Peter coad's chapter on "domain driven analysis": http://www.petercoad.com/download/bookpdfs/jmcuch01.pdf
This chapter describes how to separate different objects in a drawing in different prototypes The persistent workflow between transaction data and description should not be the same, but can also be "things" This helps to establish a better cascading strategy:
The four archetypes of Peter Coad are: - Is it a moment or interval? - Is it a role played? - Is it a catalog-entry-like description? - Otherwise,it's a party,place,or thing.
I hope it helps