How to orphan delete/update JPA entities?

Go To StackoverFlow.com

0

public class Person{

@OneToMany(orphanRemoval = true)
List<Cars> myCars;
  //Get and Set
}

public class Car{
  Here Attribs    
}

With this code, if I delete/update one car instead of the element of the list. Does it update/delete?

Example:

Person me = DAO.GetPerson(23);
Car oneCar = me.getCars().get(0); ///Lets say it exits
//then i update
oneCar.setThis(4);
oneCar.setThat(5);
DAO.UpdateCar(oneCar); //This is just EntityManager.merge
DAO.DeleteCar(oneCar); //This is just EntityManager.remove 

How can i guarantee that the orphan chages will be cascade to the list owner(PErson in this case)? So I can update the persons cars via one car instead of the list of cars.

2012-04-04 04:27
by NoName


0

orphanRemoval means that you just have to remove the object from the colleciton, and not call em.remove() on it. It seems you are doing the opposite, so this will not help.

You need to remove the car from the person's list of cars, and merge the Person. If you cascade the merge, then you don't need to merge the car separately at all, or call remove on it.

merge is only required if your objects are detached. If you make your changing within the same EntityManager/transaction, then you don't need to call merge at all.

2012-04-04 14:13
by James
I knew i was doing something wrong. Thanx man - NoName 2012-04-04 15:10
Ads