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.  [ 5 posts ] 
Author Message
 Post subject: Lookup Hibernate class mapping metadata at runtime.
PostPosted: Tue Jun 27, 2006 7:06 am 
Beginner
Beginner

Joined: Tue Oct 07, 2003 4:41 am
Posts: 21
Hibernate version:
3.1

Hi,

Hoping someone can point me in the right direction for how to look up mapping metadata at runtime in an Interceptor.

I am using Hibernate Interception in our application to perform a number of key tasks. One of those is audit of data changes.

The auditing of changes is handled in the onFlushDirty method of the Interceptor, in which I get the list of fields, their pre and post states, and types.

We basically generate a difference string of how the object has changed based on the pre/post values of all the fields.

My problem is with Collection fields. In the diff string, we need to indicate that the content of a collection field has changed in some way. We have devised a way which works for collections mapped with shared aggregation (i.e. cascade="all" ) and for those with cascade = "none".
But we can't use the same mechanism for collections mapped with cascade="all-delete-orphan", becuase we are not able to replace the field value with a new collection.

Is there some way in the Hibernate API, whereby based on the entity passed into onFlushDirty, I could lookup it's mapping metadata, and figure out for each of it's mapped collection fields, which are mapped with
cascade="all-delete-orphan" and which aren't.

Is such information available at run time to application code at all?

thanks in advance

John


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 27, 2006 10:54 am 
Expert
Expert

Joined: Fri Aug 19, 2005 2:11 pm
Posts: 628
Location: Cincinnati
I think you can get it out of your Configuration class

Another way is you can add some code to the hibernate tools (if you're using them) to have it add some helper methods to the POJO that you can use at runtime

_________________
Chris

If you were at work doing this voluntarily, imagine what you'd want to see to answer a question.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 27, 2006 11:20 am 
Beginner
Beginner

Joined: Tue Oct 07, 2003 4:41 am
Posts: 21
Having searched through the source, I found what I was looking for but it wasn't available through any public api calls.
So I have to come clean, and admit I changed the source to get what I wanted.

I modified
Code:
org.hibernate.metadata.ClassMetadata


by adding
Code:
public EntityMetamodel getEntityMetamodel();


and then modified
Code:
org.hibernate.persister.entity.AbstractEntityPersister


by making the following method public
Code:
public EntityMetamodel getEntityMetamodel()


So now I can do the following

Code:
EntityMetamodel meta = getSessionFactory().getClassMetadata(entity.getClass()).getEntityMetamodel();


and use it to get the mapping meta properties by

Code:
StandardProperty[] props = meta.getProperties();


which in turn gives me the CascadeStyle for each StandardProperty which has a method
Code:
hasOrphanDelete()
which tells me what I needed to know.

Not ideal I admit, but I'm not sure what other way I could find it out.

John


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 27, 2006 5:27 pm 
Expert
Expert

Joined: Tue Apr 25, 2006 12:04 pm
Posts: 260
Does this code help you?

Code:
SessionFactoryImpl impl = (SessionFactoryImpl) factory;
EntityPersister p = impl.getEntityPersister( MyPersistentClass.class.getName() );
CascadeStyle[] styles = p.getPropertyCascadeStyles();


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 28, 2006 9:42 am 
Beginner
Beginner

Joined: Tue Oct 07, 2003 4:41 am
Posts: 21
Hi,

thanks for the response. You're right, this will save me making source changes.

The only change I'd make to what you've shown is to use the Interface SessionFactoryImplementor rather than the Impl class.
e.g.

Code:
SessionFactoryImplementor implr = (SessionFactoryImplementor)getSessionFactory();

EntityPersister ps = implr.getEntityPersister(entity.getClass().getName());

CascadeStyle[] styles = ps.getPropertyCascadeStyles();


I tested this and got back the CascadeStyles I was looking for.

thanks

John


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