-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: After persist(A), A is in persisted but A.getB() is not
PostPosted: Wed Jun 02, 2010 4:51 am 
Regular
Regular

Joined: Thu Oct 13, 2005 4:19 am
Posts: 98
I have A object which has a @ManyToOne to a B object.
From a different JVM I receive a transient A which references a detached B.
If I now persist A:
Code:
entityManager.persist(a);

then a is in the persistence context, but a.getB() is not.
This leads to problems. Does the spec allow to have a persistent object refer to a detached object?
Is there a specific reason why hibernate doesn't simply change the a.getB() with a proxy of B (or a persistent instance of B)?
Note that I don't want PERSIST/MERGE cascading from A to B.

If I now query that A (and it's B) from the database, like this:
Code:
A refreshedA = entityManager.createQuery("from A a left join fetch a.b left join fetch b.c where a.id = " + a.getId()).getSingleResult();

Then a.getB() is still the detached object (it's not in the persistent context) and even worse, the a.getB().getC() isn't loaded (even though I explicitly asked to fetch it in the query).

Thanks for any and all help.

_________________
http://www.ohloh.net/accounts/ge0ffrey


Top
 Profile  
 
 Post subject: Re: After persist(A), A is in persisted but A.getB() is not
PostPosted: Wed Jun 02, 2010 5:58 am 
Hibernate Team
Hibernate Team

Joined: Fri Oct 05, 2007 4:47 pm
Posts: 2536
Location: Third rock from the Sun
that's natural you asked for that.
When you have A in the persistence context and then query for A, you will get the same reference back (A==A) because it was already in the persistence context. As you don't want it to cascade persist/merge B is still detached.

If you want these things to "just work" you should use cascade persist/merge. A hack for you could be to NOT attach A to the persistence context, just perform the query from a.getId().

_________________
Sanne
http://in.relation.to/


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.