Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jpa removing item from list

Tags:

java

jpa

I have 1-N relationship (object has a list) and want remove one item from the list. What is the right way of removing item in the list with JPA?

Do I have to first update the list and merge the owner and remove the item after (if I do not remove the item and just merge the owner, orphan will remain in DB, right)? In what sequence should be merge() and remove() called, does the order of remove() and merge() matter?

Code sample:

List<Item> items = owner.getItems();
Item itemToRemove = items.get(index);
owner.remove(itemToRemove);
em.merge(owner);
em.remove(itemToRemove);
like image 451
padis Avatar asked Jan 04 '11 11:01

padis


3 Answers

This should work in case you have orphanRemoval=true (JPA 2.0).

For JPA 1 there is no standard way to handle this. Hibernate has @Cascade where you can specify DELETE_ORPHAN

like image 141
Bozho Avatar answered Oct 12 '22 22:10

Bozho


This is sufficient:

List<Item> items = owner.getItems(); Item itemToRemove = items.get(index); items.remove(itemToRemove); 

When the EntityManager is flushed, the link between owner and itemToRemove will be deleted. And if cascade is set to CascadeType.ALL, itemToRemove will also be deleted.

Actually, a shorter (and equivalent) version is this:

List<Item> items = owner.getItems(); // no need to keep a reference to the object that has to be removed items.remove(index); 

Reference:

  • Managing Entities (Java EE 5 tutorial)
    [unfortunately there is no passage where this is written out explicitly, you have to read between the lines a bit]
like image 22
Sean Patrick Floyd Avatar answered Oct 12 '22 22:10

Sean Patrick Floyd


Thank you guys for the answers. Your answers are very similar but there are some slight differences.

Just want to make sure that I understand it right:

  1. In JPA 1.0 (in general) I have to call remove() on referenced entities. They won't be removed with flush of entity manager. Otherwise orphan would remain in DB.
  2. In JPA 1.0 with Hibernate implementation when DELETE_ORPHAN set, referenced entity is removed with flush of entity manager. No need to call remove().
  3. In JPA 2.0 (in general) when orphanRemoval=true, referenced entity is removed with flush of entity manager. No need to call remove(). If orphanRemoval is not set, remove() has to be called to remove referenced entity.
like image 23
padis Avatar answered Oct 13 '22 00:10

padis