-->
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.  [ 3 posts ] 
Author Message
 Post subject: Entity intercepted when a collection element updated
PostPosted: Wed Oct 14, 2009 1:25 pm 
Regular
Regular

Joined: Tue Feb 17, 2009 5:13 am
Posts: 59
Hi all.

I need to know how can I set Hibernate (...) to achieve the following issue:

I have a bidirectional (composition) one-to-many association (a.bs is a Set<B> object and b.a is a A object). When I load "a" from DB and I update one of its "bs" I need Hiernate intercept A entity when saveOrUpdate A.

Code:
Code:
public class A {
   Set<B> bs = new HashSet<B>();
   // ... other fields and setters/getters
}
public class B {
   A a = null;
   // ... other fields and setters/getters
}


Use case:
Code:
   A a = load("idA"); // load A from DB
   B b = s.getBById("idB"); // get a B element of A
   b.setName("blablabla"); // update a field of B
   saveOrUpdate(a); // persist A entity with its Bs (including modified B)


The change is performed because the (mini)model has been annotated properly.

The problem is that my interceptor only detects the change in B entity, but not A. I need to detect A change because I need to update audit info.

Other point of view is: I need to get A entity via B and update it. In fact, I can get A from B, but the change is not persisted...

Do you know the problem?

Any idea?

Thanks!

_________________
Born to lose... live to win!


Top
 Profile  
 
 Post subject: Re: Entity intercepted when a collection element updated
PostPosted: Thu Oct 15, 2009 2:11 pm 
Regular
Regular

Joined: Mon Jan 05, 2009 6:42 pm
Posts: 99
Location: IL
Hi,
I don't know if this helps or not. I kind of lost in your example but in general, in an bi-directional association, only one end of the change is persisted. And usually you would retrieve children(in your Case B) from the Parent(in your Case A) and modify the Children and this would cause children changes saved and also Parent gets saved as the children in it are modified. The Hibernate Attribute that controls this the inverse attribute.
<set
name="XX"
table="XX"
lazy="true"
inverse="false"
cascade="all"
sort="unsorted"
>

-Srilatha.


Top
 Profile  
 
 Post subject: Re: Entity intercepted when a collection element updated
PostPosted: Thu Oct 15, 2009 2:36 pm 
Regular
Regular

Joined: Tue Feb 17, 2009 5:13 am
Posts: 59
That is solved with org.hibernate.annotations.Cascade.
In many cases I can update a A object from any of its B related objects. But not in every cases.

The conflictive scenario is when I load an A object from DB which has B elements. When I update any B all is right. But in the interceptor, with both objects (A and B) attached to the session, if I try to update a A field accessing via B, that change will be lost on flush.

I need a way to do this in this scenario.

_________________
Born to lose... live to win!


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 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.