Maintaining relationships in JPA 2.0 - java

I've been using JPA 2.0 for a while but, sad to admit, I haven't had enough time to learn it properly. It seems like I lack the basics of how to work with Entity Manager.
Moving one step at a time, I'd like to first ask you about maintaining relationships between mapped entities. Of course I know how to create mappings between entities, different types of available associations (OneToOne, etc.) and how databases work in general. I'm purely focused on maintaining it via Entity Manager, so please do not send me to any kind of general knowledge tutorial :-).
The questions are:
Am I right that as a programmer I'm responsible for maintaining (creating/updating/removing) relationships between instances of entities?
Do I have to always update (set to null, remove from collection, etc.) instances by hand?
Plain SQL can set entities to NULL on deleting, but it seems like JPA can't do such a simple thing. It also seems like a burden to do it manually. Is there a way to achieve that with JPA?
If I have OneToMany relationship and set to NULL the entity on the Many side of the relationship. Then I persist the changes in a Set by saving the entity on the One side. Do I then have to update the entities in the Many side and set association to NULL in each instance? Seems pure silliness for one-directional bindings!
Thanks in advance!

The main thing you need to investigate is the different options you have when mapping on entity. For example in the next piece of code the cascade all option will instruct jpa to delete the child list when the parent is deleted.
#OneToMany(fetch = FetchType.LAZY, cascade = { CascadeType.ALL }, mappedBy = "parent")
private Set<Child> events = new HashSet<Child>();

Yes. You maintain the object tree and modify it to look like what
you want.
Yes and no. If you want the entity to reference null, then yes.
For instance, if you are removing one Entity, then you should clean
up any references to it held by other entities that you are not
removing. A practical example: its good practice to let an Employee
know his/her Manager has been let go. If the Employee is going to
stay, it should either have its manager reference nulled out or set
to a different manager, before the current manager can be removed.
If the employee is going to be removed as well, then cascade remove
can cascade to all the Manager's subordinates, in which case you do
not need to clean up their references to the manager - as they are
going away too.
I don't quite understand what SQL is setting to null. Deleting
removes the row in the database, so there isn't anything to set to
null. Cleaning up a reference shouldn't be that difficult in the
object model, as JPA has a number of events to help such as
preremove preupdate etc. In the end though, the problem is with
your java objects. They are just java objects, so if you want
something done, your application will need to do it for the most
part. JPA handles building them and pushing them to the database,
not changing the state for you.
Yes and no. If you set up a bidirectional relationship, you must
maintain both sides as mentioned above. If you set the child's
parent reference to null, you should let the parent know it no
longer has a child, wouldn't you? Your parent will continue to
reference its child for as long as that Parent instance exists. So
even though the database is updated/controlled through the side that
owns a relationship, the object model will be out of synch with the
database until it is refreshed or somehow reloaded. JPA allows for
multiple levels of caching, so it all depends on your provider setup
how long that Parent instance will exist referencing a Child that no
longer exists in the database.

Related

What is the significance of being the realtionship owner and how does CASCADE work with the two sides in JPA?

I have two tables. Transactions and Errors. There exists a One-To-Many relationship between Transactions and Errors. This is a bi-directional relationship and Errors is the owning side as #JoinColumn is specified in the Errors class. I want to understand what exactly does it mean to "OWN" the relationship. Say at the moment I have,
PROCESSED (column in TRANSACTIONS) set to N
ACTIVE (column in ERRORS) set to 1
Scenario 1:
Now lets suppose we execute the below code.
transactions.setProcessed("Y");
errors.setActive(0);
transactions.setErrors(errors);
entityManager.merge(transactions);
I understand that the PROCESSED field will get set to "Y" in TRANSACTIONS but will the ACTIVE field in ERRORS also get set to 0 or not given that transactions IS NOT the OWNING side of the relationship?
Scenario 2:
On the other hand, if we execute the below:
errors.setActive(0);
transactions.setProcessed("Y");
errors.setTransactions(transactions);
entityManager.merge(errors);
I understand that the ACTIVE filed in ERRORS will be set to 0 but will the PROCESSED field in TRANSACTIONS also be set to "Y" given that ERRORS IS the OWNING side of the relationship?
How do JPA cascade types tie into scenarios like this?
In a non-bidirectional relationship, you define a mapping. When you make a change to that mapping, it is obvious what will happen - a foreign key will get updated. Because there is only one mapping, there can be no conflict (many JPA providers will throw errors if they detect you have more then one writable mapping for a field).
With a bidirectional relationship, this control is less obvious. In your transaction-Error bidirectional relationship, assume it is a OneToOne bidirectional mapping, and Transaction1 is set to point to Error1 and vis versa. Lets say your application determines that Transaction1 should be pointed to Error2 instead, and changes the reference. If Error1's reference to Transaction1 isn't corrected to reflect this situation, there is a problem for JPA in determining what value to put into the foreign key. This is where ownership comes into play. The owning side is considered the writeable mapping, and changes to it control the foreign key fields. In a OneToMany, the owning side is usually the ManyToOne back reference because it is more natural since the foreign key is in the table holding the ManyToOne anyway. If you make a change to add an Error to a Transaction but do not change the Error to also reference that Transaction, your object model will be out of sync with what goes into the database - The foreign key in Error will not be changed, but the transaction object will show an error in its list until it is refreshed or reloaded from the database.
Cascading is something unrelated to ownership. It just means the operation (persist, merge, delete, refresh) applies to the entity referenced by the relationship. If using cascade.all an call em.refresh(transaction), the transaction and all referenced Errors will be refreshed from the database. Any relationships Error then has that have a cascade setting of ALL or REFRESH will also get refreshed and so on. JPA should detected that it has already refreshed the referenced Transaction instance if you place it on the back references, but why risk it. Generally, cascade options should only be placed on mappings where they are required to avoid unintended consequences. If you aren't sure if it is needed, leave it off until you are sure. Things like lazy fetching and other optimizations can cause all sorts of strange and hard to find bugs when someone goes and puts a cascade refresh everywhere.
In your example, you might put a cascade merge on the root entity that your application will be passing around. Any changes made to that graph then are easily picked up with a single merge call without having to call merge on each individual leaf. How your model is built and serialized though will affect the merge, so generally cascade options are put only on the root->Leaf relationships to avoid issues where the root -> leaf -> root' where root != root'. If you have cascade merge on both sides, the state of root' might overwrite your changes in root.
When we say that Errors is the owning side, that means foreign key of the relationship lies within the Errors table(which you are doing via #JoinColumn). Thus, the owning side of the relationship is the one in which reference column of the other entity will be present.
You can define the inverse side of relationship by specifying #OneToMany in the Transactions entity.
Now comes the second part of your question regarding the update of transactions and errors. In my view you can update List associated with a transaction by applying appropriate mode of cascade(persist,delete etc) which means you can specify in your Transaction entity #OneToMany(cascade=CASCADETYPE.MERGE) while specifying the inverse relationship. In this way, if whenever you will update a transaction row, corresponding error rows can also be updated.
However, I don't think it is a good practice to cascade in the other way ie if you update child entity the parent entity should also get updated as it may lead to many data inconsistencies

JPA: Event notification during persisting/updating a child?

I have two entities A and B. A has a many-to-one association towards B (A.listOfBs::List). I want to validate before B is persisted if a B was added to an already existing A.
Debugging into our JPA implementation (Hibernate), I learned that it basically cascades the association and while knowing its a parent<->child association only invokes the PrePersist-handler on B and never the BeforeUpdate on A.
Since I do not want to introduce a bi-directional mapping, I would like to know how I can validate A's children during B's persisting phase as A has special requirements (no two childs sharing the same type).
Is there any mechanism I can learn about parent<->child related events revolving around persisting or deleting children of a particular parent?
From debugging into Hibernate (another framework might be different), the persist event is handled solely on a per entity level. There is a mapping between child and parent in the persistence context that is set prior to persisting the entity (see handling/persisting an association). If one is able to access it, it would be possible for the child to notify its parent event even without a bi-directional connection. All this will continue to feel like a hack.
The best solution seems to be to listen to session level events and iterate through the list of managed entities an let those be validated regardless whether those have changed or not.

JPA (EclipseLink) returning null in list

I didn't know if it was appropriate to delete the original question but have added a more concise explanation at the bottom hopefully eliminating the need to review all of the details.
I am receiving inconsistent results and can't figure out what I am doing wrong. It relates to an EE7 web app with container managed Entity Manager, Java 8, EclipseLink 2.5.2, netbeans 8.0.2 IDE.
There is a parent child relationship mapped as a bi-directional relationship:
Event Table is the parent which maps to Games
#OneToMany(fetch=FetchType.LAZY, cascade = CascadeType.ALL, mappedBy = "event")
#OrderBy("gameType, round")
private List<Game> gameList;
Games maps the Event as
#JoinColumn(name = "EVENT", referencedColumnName = "ID")
#ManyToOne(fetch = FetchType.EAGER, optional = false)
private Event event;
Step 1:
Using a web interface I create an event and one game. The game is added to the event list and event to the game. em.persist(event) is called to save all of it.
-> works great
Step 2:
Edit the event
create a new game - Add the event to the game, em.persist(game) with em.flush() to get the id and add it to the game list in events
After all editing is done em.update(event)
-> database updates exactly as expected
-> BTW I understand that it is a better user experience to not persist game until the event is ready to be saved to allow option of 'quit'. It is something I plan to do but don't yet have a good handle on best practices for doing so and my experience with cascade is mixed.
Finally the issue:
- 100% of the time 'step two' works correctly with the database but when I retrieve the event for further edits the event has an additional entry in the game list with value of null.
- if I shut down and restart the app over and over sometimes it happens and sometimes it doesn't. The database always remain in tact and exactly as expected
- simultaneously using a different browser from the same machine or others has the same result
- obviously the persistence cache CAN get in an inconsistent state but I have not been able to determine if I am doing something wrong or experiencing an EclipseLink bug
Earlier I reported a problem that turned out to be an EclipseLink bug related to the IndirectList class and Java 8 so I am not sure if I am doing something wrong or if this is similar.
Thanks in advance!
After some reading through the JPA JSR I am wondering if my understanding is correct. Using the EE container managed transaction scoped persistent context in a web application essentially means a transaction begins with a method call to the EJB and end with the return. Therefore getting info to put something on a screen is one transaction and updating after the users changes is another. Or more clearly, I am almost always working with detached entities.
My approach has been to get the entity tree, let the user/administrator update whatever is necessary and then persist, merge or delete as appropriate. Since it is detached orphan removal doesn't work so I do that directly but maintain the relationships. I also persist the new children directly and presume I can em.merge the parent as much as I want because it becomes attached, applies the changes and my entity is in tact with the DB.
Is that a fair approach or is it it flawed somehow?
I am still having this issue and have possibly tracked it a little further. I have a number of related records with bi-directional links via foreign keys #ManyToOne and #OneToMany.
It is essentially a simple chain with backward links:
Entity A contains a List of B, and B a foreign key to A
Entity B contains a List of C, and C a foreign key to B
Steps to reproduce:
new C(B, and other constructor parameters)
B.add(C)
Set other properties in C
em.persist(C) to ensure an ID
em.update(B) to register updated relationship
After every step including 4, everything looks correct. I thought perhaps step 2 should follow 4 but am not sure it would make a difference.
After step 5,
B has the same memory location and pointer to C.
C has been updated with NEW memory locations for the references to B and A.
i.e. the pointer back to B and A in C is a different memory location than the source
Subsequently when I refresh the entity with a em.find(), entity B has NULL entries in List C although the MySQL database reflects what I expect.
I have obviously done something wrong which has corrupted the EclipseLink persistence cache. The only thing I have found to fix this is restarting the app to delete the cache.
I have not been able to resolve this for months and would appreciate any advice.
The problem was ultimately tracked to a coding error. Specifically the reference pointer returned from em.merge() was not updated in the related entities resulting in multiple copies and corrupting persistence.
Something in the startup sequence affected whether this corruption became evident with the symptom of null's in a list or everything working fine. It appeared sporadic but in fact was the result of the earlier corruption.

JPA - delete a child from OneToMany relationship

In a #OneToMany relationship if I want to remove a child, do I need to explicitly delete that child from parent's collection as well or just deleting the child will suffice?
For instance, Person and Phone. Each person has many phone numbers. If I want to delete one phone number from a person is this enough:
EntityManager.remove(phone);
Or I need to to this beforehand:
Person.getPhone().remove(phone);
Not to mention, the CascadeType is set to MERGE.
You need to remove the Phone explicitly from the phones collection, it's not enough to remove it with the EntityManager.
From the other side, it might be sufficient to use orphanRemoval, so if you remove an entity from a collection, it gets automatically deleted. Something like:
#OneToMany(mappedBy="person", orphanRemoval="true")
private List<Phone> phones;
See also: http://docs.oracle.com/cd/E19798-01/821-1841/giqxy/index.html
Cascade.REMOVE only removes the child entity if the parent entity is removed as well. Cascase.MERGE has nothing to do with this problem.
Not sure if MERGE is enough to get entities deleted cascaded, you will probably have to also define DELETE cascading and depending on how the data is mapped (with or without a secondary table in between) it might even be necessary to apply orphan removal too.
If you don't apply cascading for deletion but rather use a JPA query or an entityManager.remove(), then it is certainly a good idea to manually evict it from the oneToMany collection as well. The reason is simple: you may manually remove it from the database, but that doesn't mean it automagically gets removed from the collection too so for the lifetime of the parent entity, it will still be referencing an entity which is not supposed to exist anymore. Things get weird when you then also accidentally change the state of said entity.

JPA cascade options at runtime

I'm trying to make an application that keeps an object model in sync with a database by observing all changes and then immediately persisting the objects in questions. Many of the object in the model have children in large lists or trees.
When I load an object from the database, I rely on a one-way cascading relationship to also retrieve all of its children and include them in the application.
However, it is possible to alter a field in the parent object which requires persistence and I can determine that none of the children are affected. So I would like to persist the parent, without hitting the database with all the cascaded child persists.
eg
#Entity
public class Parent {
#OneToMany(cascade=CascadeType.ALL)
public List children;
}
How can I override the cascade option when I persist a Parent object? Or should I just set it to REFRESH and make sure I never need a cascading persist?
Reading the objects from the database and persisting them rely upon two different annotations.
When you load an object, it will also get the other end of any eager (FetchType.EAGER) relationships, as defined by the fetch property on the relationship.
Depending on your JPA provider, you may have options to override this behaviour. EclipseLink, via the incredibly useful QueryHint.BATCH, certainly does.
When you persist, delete or refresh, the cascade type is what's relevant.
So, lose the cascade, keep the fetch and problem solved.
Personally I think cascade all is asking for trouble but opinions will vary.
A decent JPA provider will have a pretty sophisticated (configurable) caching scheme already. Perhaps you should be asking why you're reinventing that particular wheel?
Is it an issue of asynchronous updates purely for performance? Or is something else the reason?

Categories