-->
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: Smart deletion of objects via metadata
PostPosted: Fri Apr 04, 2008 2:35 am 
Newbie

Joined: Fri Apr 04, 2008 1:54 am
Posts: 3
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

Hibernate version: 3.2.ga

Generally, I have an object graph generated via an ER tool. As a result, all the associations are consistent (all owning objects have a collection and the owned object has a singular value pointing to the owner. ). I would like to exploit the meta data that Hibernate _must_ be keeping to crawl the object graph and orphan off any objects referencing an object and then delete with impunity the referenced object. Basically, I never want to get a ConstraintViolation. There are no non-nullable foreign keys. I dont have the ability to inject customer cascade-delete-orphan logic (and it wouldnt be appripriate here, as I dont have a guarantee an ojbect is only refernced by one other entity).

What would you use in the to do such a thing? I get tat all objects may be cast to HibernateProxy. I get that I can do:

LazyInitializer li = proxy.getHibernateLazyInitializer();
SessionImplementor impl = li.getSession() ;
ClassMetadata meta=impl.getFactory().getClassMetadata(Foo.class);
Type[] types = meta.getPropertyTypes();

For example and that seems to get me closer, but I'm still generally unsure of if this is even possible. Is there a way to determine the 'owning' side of a relationship for example? To dynamically using reflection/meta data figure out which collection is 'amppedBy' that owning side on the other side of the association?

Thanks in advance for any help

_________________
Josh Long
Sun Certified Java Programmer
http://www.joshlong.com


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 04, 2008 3:49 am 
Expert
Expert

Joined: Thu Jul 05, 2007 9:38 am
Posts: 287
I'm not 100% sure what you are trying to do, but it sounds like the configuration object that you used to spawn your session factory might be of some use to you. It containes all the class and mapping definitions.

Configuration.getClassMapping(String) will give you a PersistentClass Object which might contain the information you are looking for.

_________________
Please rate useful posts.


Schauderhaft: Softwaredevelopment, Projectmanagement, Qualitymanagement and all things "schauderhaft"


Top
 Profile  
 
 Post subject:
PostPosted: Sat Apr 05, 2008 4:16 pm 
Newbie

Joined: Fri Apr 04, 2008 1:54 am
Posts: 3
Alright, that was definitely a nudge in the right direction. Now i need to figure out specifically how to extract (via runtime meta data) what a collection of entities is 'mappedBy' - that is, say I have a Foo and Foo has a collection of Bars. The collection of Bars are mappedBy 'foo'. I know thus, hat by iterating over each item in that collection and setting whatever property is used in mappedBy to null and also removing the object from the collection, that I will not have any foreign key constraints on that Foo.

The code to handle any one of these sorts of situations looks roughly like:
Code:
Foo f = ... ;
Collection<Bar> bars=f.getBars() ;
Collection<Bar> toRemove=new ArrayList<Bar>();
for(Bar b : bars) {

toRemove.add(b) ;
  b.setFoo( null) ;
}

for(Bar remove : toRemove)
  f.getBars().remove(remove) ;


Its boilerplate, and it needs to be done each time i want to unentangle an object so I can delete it. If we add another association with the modeling tool and it touches Foo, then we need to modify the appropriate deletion method because itll now blow chunks. I want to build a smarter mouse trap. In this case, the collection of Bars were mapped by foo. Setting bar.foo = null and then removing bar from foo's collection of bars works.

I appreciate your prompt reply, and thanks in advance for any further insight. Essentially, I want to do the above, but via reflection and via the runtime data available through Hibernate's APIs. In my case, all associations ae bidirectional, and all associations are one to many or one to one, so the above ought to work (or I just null out each objects pointer to the other for the one to one case).

Thanks again.

_________________
Josh Long
Sun Certified Java Programmer
http://www.joshlong.com


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.