last day (15 days later) » 

10:29 PM
0
A: JPA lazy-loaded one-to-one reference not updated

Rat2000Try this: @OneToOne(fetch = FetchType.LAZY, cascade = CascadeType.DETACH) @JoinColumn(name = "PARTNER_ID") private Contact partner; In the example above, the Contact entity class contains an address field that references an instance of another Contact, which is another entity class. Due to the...

 
Thanks for the suggestion, I have tried it and the results are the same. Committing the transaction automatically flushes the EntityManager, so this should make no difference. Also, changes to contact1 are flushed to the database, but not changes to contact2. That's what I don't understand.
 
I think you have confused me... So when you save to the database, the changes apear in the database for contact 1, but if you retreve the object contact 2(witch should have a reference to contact1) from the database, Contact partner is null?
 
Contact.unassignPartner() unassigns the partner for the current instance (this) but also for the referenced instance (partner). So after calling contact1.unassignPartner() and committing the transaction, I'm expecting to have contact1.partner = null and contact2.partner = null, both in memory and in the database. I'm observing contact1.partner = null and contact2.partner = null in memory, but contact1.partner = null and contact2.partner = contact1 in database.
 
I have edited my answer
 
I'm afraid the result is the same. The problem isn't that contact2 isn't detached from the EntityManager, it's that it's just not updated...
 
10:29 PM
Not even when you close the entitymanager?
 
Hi
 
hey
 
Even when I close the EntityManager
Results are the same
It's really weird
 
ok, leave the code with the detach operation as it is
that should be there
 
Changing to @OneToOne(fetch = EAGER) works fine
 
10:30 PM
and in the method you unnasign your partner do this
 
So it's something to do with how Hibernate deals with the proxy
 
let's try this way
leave @OneToOne(fetch = FetchType.LAZY, cascade = CascadeType.DETACH) as it is
that is good
and change
private static void unassignPartner(final EntityManager entityManager) {
entityManager.getTransaction().begin();

final Contact contact1 = entityManager.find(Contact.class, CONTACT1_ID);
contact1.unassignPartner();

entityManager.getTransaction().commit();
}
to this:
private static void unassignPartner(final EntityManager entityManager) {
entityManager.getTransaction().begin();

final Contact contact1 = entityManager.find(Contact.class, CONTACT1_ID);
contact1.unassignPartner();

entityManager.getTransaction().merge(contact1);

entityManager.getTransaction().commit();
}
you here?
 
Just got the message
 
private static void unassignPartner(final EntityManager entityManager) {
entityManager.getTransaction().begin();

final Contact contact1 = entityManager.find(Contact.class, CONTACT1_ID);
contact1.unassignPartner();

entityManager.merge(contact1);

entityManager.getTransaction().commit();
}
this is the correct form
and like i said you have to have the @OneToOne(fetch = FetchType.LAZY, cascade = CascadeType.DETACH)
 
No this doesn't work either
 
10:38 PM
you tried?
 
Just did
contact1 is already managed by the EntityManager, so there's no need to merge it again
 
did you really try or you just think it won't work and you didn't try?
 
I really tried it
 
I think I saw your error
in public void unassignPartner()
you are make nul this partner.partner = null;
it should be partner=null
 
I'm doing both
partner.partner = null;
this.partner = null;
I'm resetting both references
 
10:42 PM
yea sorry didn't look down
 
Sure no worries
Thanks for looking at this btw
I really appreciate it
 
try with making only partner=null
mayby jpa gets somehow confused?
i think it is worth a try, but then you really need the detach
 
Detaching is when you ask the EntityManager to stop tracking an entity
I actually want it to track the entity - changes to contact2 are not persisted
 
no!!!!
 
And it is tracked, just changes are not detected and/or persisted
 
10:45 PM
read Cascading Detach
so you didn't try the detach thing?
 
Yes I did try it
/**
 * Remove the given entity from the persistence context, causing
 * a managed entity to become detached.  Unflushed changes made
 * to the entity if any (including removal of the entity),
 * will not be synchronized to the database.  Entities which
 * previously referenced the detached entity will continue to
 * reference it.
 * @param entity  entity instance
 * @throws IllegalArgumentException if the instance is not an
 *         entity
 * @since Java Persistence 2.0
 */
public void detach(Object entity);
The ObjectDB link is consistent with that
 
you are not reading the corect thing man
 
If you detach contact1, then contact1.partner becomes detached as well
 
Marking a reference field with CascadeType.DETACH (or CascadeType.ALL, which includes DETACH) indicates that detach operations should be cascaded automatically to entity objects that are referenced by that field (multiple entity objects can be referenced by a collection field):

@Entity
class Employee {
:
@OneToOne(cascade=CascadeType.DETACH)
private Address address;
:
}
In the example above, the Employee entity class contains an address field that references an instance of Address, which is another entity class. Due to the CascadeType.DETACH setting, when an Employee instance is detached t
 
Yes
If you detach employee, then employee.address becomes detached as well
But detaching doesn't mean that the entity/value is deleted from the database
 
10:49 PM
darn your right
 
It just means that it's not tracked by the EntityManager anymore
:)
 
i think you should use CascadeType.Update
since you are updateing right?
 
I tried that as well, doesn't work either
 
then it is very odd
 
Yes :)
OneToOne w/ fetch=LAZY is not something I would use anyway
I'm just dumbfounded by the behavior I see
It's clearly got something to do with the proxy Hibernate injected in the partner reference
But as I wrote, changes to fields other than partner are properly persisted
 
10:52 PM
I really don't know what to say for user, I am sorry
 
partner.setFirstName("DUMPED") is persisted
It's allright
Thanks for taking the time to look into this
 
no problem, wish I could help more
So your problem is only when you want to break the references
right?
 
Yes
Hibernate probably thinks that what's going is weird
 
there is a more unorthodox method...
just manually seth them both to null...
 
That's exactly what I do :)
 
10:54 PM
it's not ideal, but...
 
Oh from outside the Contact.unassignPartner() method you mean
Let's try this
 
yes
you have contact 1 set to null, update database
concatc 2 set to null and update database
 
It works
contact1.unassignPartner();
contact2.unassignPartner();
Everything is persisted to the database properly
 
but it is not ideal.. like I said
 
Hibernate is probably lost because of the proxies
Because when you think of it
contact1.partner is a proxy
contact2.partner is a proxy as well
Cutting the proxy references probably mixes Hibernate up
 
10:57 PM
I use this code in a lot of many to many relationships and it works fine...
I don't know why your entotymanager is confused
it should work
 
Yeah it's just a shame that I need to bring that behavior out of the domain model
Then again, it's debatable - maybe breaking the relationship one way doesn't necessarily imply breaking the reverse relationship
Oh well
Interesting problem :)
 
it doesn't!!!
contact 1 can reference contact 2
but contact 2 doesn't need to refrence contact 1
unless you explicityly create the databse like that
 
Yes I agree
I was trying to model a husband-wife relationship actually
Person would've been a better term than Contact
So if the husband divorces from the wife, necessarily the wife divorces from the husband
Something like that
 
here
Example 2: One-to-one association that assumes both the source and target share the same primary key values.
I think this is what you need
 
Hmmm both Contacts won't share the same primary key...
Anyway I think I'll stick with managing the reverse relationship logic outside the entity
Thanks a lot for your time it's very kind of you
 
11:05 PM
well you know better what works for you and what doesn't and how you want it to be
I say it is ok to manage them outside since it is juat one to one relationship and you won't feel much diference on the cpu and ram with this
 
Yeah I know it's a design issue more than anything else
 
but if you had a one to many or many to many with a lot of objects to manage, you do realize this is far from ideal
 
I'm picky about these things
:)
Yes obviously
 
I'm picky to
and you should be
a mistake here at the db level can make a hell do develop on the rest off the application
anyway
 
Yup
 
11:07 PM
good luck with your husband and wife :)
 
Hehe
Thanks
 

last day (15 days later) »