-->
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: Updating collections in a 3-tier application
PostPosted: Wed Jan 12, 2005 6:35 am 
Hibernate version: 2.1.3


Hi,
I am having a scenario where i retrieve a persistent object in a stateless session bean. I convert this persistent object to a domain element and pass it over to the web-tier.
The persistent object has a one-to-many relationship with one more persistent object, as below:

class A {
Object id;
....
java.util.Set childObjects;

}

class B {

Object id;
....
A parentObj;

}

The hbms have the appropriate relationship mappings. I am having a cascade all-delete-orphan strategy. So whenever i remove a entry from the set of childObjects the corresponding entry in the database is deleted.

The problem i am facing is:
- I convert the persistent object into a domain element
- The original Set in the parent persistent object is converted to a set of domain elements. This implies that i lose the reference to the original set.
- At the web-tier i remove one(or more) elements from the set of child objects.
- Then i pass this object to the session bean for persisting it back
- I then convert this domain element back into a persistent object.
- I then call a update on the persistent object(Note: I am using <timestamp> tag in my hbms>


- Ideally, hibernate should delete those entries from database which were removed from the set. However, hibernate is not firing any delete query.

I found the following piece of information on some site:
#########################################################
Unlike other Hibernate value types, Hibernate tracks actual collection instances using Java identity, ==. Your getter method should return the same collection instance as was assigned by Hibernate to the setter method
#########################################################

So, i maintained the reference of the set of childObjects in my domain elements. I then removed an element from this set at the web-tier and passed the same set back to the bean as before. The bean then called a update on the parent object.

In this case the appropriate entries WERE DELETED from database.
(Note: I did not do any changes to hbm files )

I would not like to use this as a solution, since i have a very complex object graph. I went through the following link which talks about similar thing by using select-before-update:

http://www.hibernate.org/161.html

I followed this approach too but without success.


Can anyone suggest a solution to this problem?


Top
  
 
 Post subject:
PostPosted: Wed Jan 12, 2005 6:23 pm 
Regular
Regular

Joined: Tue Jun 22, 2004 8:01 pm
Posts: 106
Location: PowderTown, Utah, USA
Well, for starts, since you use the term "Domain Element" it sounds like you're using the Data Transfer Object (DTO) Pattern for EJB (a.k.a the infamouse Value Object). If this is the case, you may want to re-evaluate that pattern.

Hibernate makes it possible to stop using DTO's ("domain elements" in your parlance) and simply pass the persistent objects themselves across the wire. You can control large graph serialization by using lazy collections and proxied relationships. When those objects are sent over the wire, they can be operated on directly and when you send them back to your Session Bean, they can be reattached to Hibernate where Hibernate will detect the changes to the collections and make the appropriate inserts, updates, and deletions. (To re-attach a detached hibernate object use "update()" or "lock()".) In point of fact, this ability to work on detached objects and get rid of DTO's forever is one of Hibernate's greatest strengths.

If you translate the Hibernate objects into DTO's (Domain Elements) for serialization, and then make changes to the elements on the web tier, how is Hibernate supposed to know about the changes? You'd have to re-load the hibernate objects and synchronize them yourself with your returned Domain Elements. Yuck. Kinda pointless when Hibernate already does all of that for you if you just use your hibernate objects directly.

At the root of this discussion is understanding how Hibernate does its work. The reason Hibernate knows about changes to collections is that it uses special implementations of the collections that translate add/remove operations into SQL. If you disect your hibernate objects and transform them into something else, there's no way hibernate will know about your remote changes.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 14, 2005 2:22 am 
Hi,

The main purpose of converting the persistent object into a domain element is to make the object suitable for the UI. My domain element might contain information from more than one persistent object. Sending back the entire persistent object is not appropriate.
Does this mean that i MUST MAINTAIN THE REFERENCE of collections of persistent objects in my domain element so that the updates work properly. I tried this option and it works. But i would not like to go for this approach. Is there any other way i can tackle this?

Thank you.

-Jaikiran


Top
  
 
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.