About Question enthuware.oce-jpad.v6.2.602 :

Moderator: admin

Post Reply
himaiMinh
Posts: 358
Joined: Fri Nov 29, 2013 8:26 pm
Contact:

Re: About Question enthuware.oce-jpad.v6.2.602 :

Post by himaiMinh »

I tried this in the ToyAppForJPA:

Code: Select all

 A a = em.find(A.class, (long)113);
List bs = a.getBs();
B b = em.find(B.class, (long)112);
bs.remove(b);
em.persist(a);
em.remove(b);
I am curious why it works without throwing EntityExistException ?
a with id 113 has already existed in the table A. We modify a by removing b in its list, I expect to see EntityExistException. But I don't.

himaiMinh
Posts: 358
Joined: Fri Nov 29, 2013 8:26 pm
Contact:

Re: About Question enthuware.oce-jpad.v6.2.602 :

Post by himaiMinh »

One more try I did :

Code: Select all

//I have persisted A with id = 116 and it has a B.
A a = em.find(A.class, (long)116);
em.remove(a);
After a is removed, its B and its reference in A_B table are removed as well.
So, removing a is good enough. I don't need to explicitly remove its b.

himaiMinh
Posts: 358
Joined: Fri Nov 29, 2013 8:26 pm
Contact:

Re: About Question enthuware.oce-jpad.v6.2.602 :

Post by himaiMinh »

Regarding to my previous post:
Code:
A a = em.find(A.class, (long)113);
List bs = a.getBs();
B b = em.find(B.class, (long)112);
bs.remove(b);
em.persist(a);
em.remove(b);
I am curious why it works without throwing EntityExistException ?
The spec 3.2.2 says "If X is a preexisting managed entity, it is ignored by the persist operation".
So, I understand why EntityExistsException is not thrown because it is not a new entity or detached entity.
But I assume the persist (a) is ignored.
But in this example, persist (a) is not ignored. It persists the a with the b removed.

Instead, I merge a instead of persist a :

Code: Select all

A a = em.find(A.class, (long)108);
List bs = a.getBs(); 
B b = em.find(B.class, (long)109);
bs.remove(b);
em.merge(a);
em.remove(b);
It also works.

admin
Site Admin
Posts: 10046
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.oce-jpad.v6.2.602 :

Post by admin »

himaiMinh wrote:Ok. I see the difference between the two solutions now:
1. Since a has @OneToMany relationship with b, removing a will cause b to be removed successfully.
But in this question, I don't see any orphanRemoval=true specified.
No, b will not be removed automatically. The row in the join table that links a with b will be removed if you remove a.
2. Since b has @ManyToOne relationship with a, we have to break the relationship between a and b to avoid foreign key violation in the A_B join table.
Correct.
If you like our products and services, please help us by posting your review here.

himaiMinh
Posts: 358
Joined: Fri Nov 29, 2013 8:26 pm
Contact:

Re: About Question enthuware.oce-jpad.v6.2.602 :

Post by himaiMinh »

Hi,
I tried this :

Code: Select all

A a = em.find(A.class, (long)108);
em.remove(a);
I expect a is removed and the a in the A_B table is removed, but b should be there.
But Hibernate remove a, b and the relationship in the A_B table.

Post Reply

Who is online

Users browsing this forum: No registered users and 68 guests