-->
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.  [ 6 posts ] 
Author Message
 Post subject: How to discover the persistent obj id attr. name at runtime?
PostPosted: Tue Oct 26, 2010 5:09 pm 
Newbie

Joined: Fri Oct 22, 2010 11:06 pm
Posts: 11
Hi!
I'm implementing a generic DAO object to execute CRUD operations on objects whose classes have been mapped on Hibernate. I'm using JPA annotations for the class mapping.

In order to construct some filter expressions used internally, this DAO object needs to obtain the name of the attribute that has been mapped as the object's id for a given persistent object. So for example, I give an persistent instance (returned by a criteria query) to the DAO object and need to retrieve the name of its id attribute:

Code:
String idAttrName = daoObject.getIdAttrName(persistentObject);


...which in most cases will simply be "id", but might be something else.

Is this possible? I only have access to the entity manager originally used to retrieve the object. I don't have metamodel classes being generated.

Please let me know if you have any clues on how to obtain this information.

Thanks!
Eduardo Born


Top
 Profile  
 
 Post subject: Re: How to discover the persistent obj id attr. name at runtime?
PostPosted: Wed Oct 27, 2010 6:11 am 
Senior
Senior

Joined: Fri Oct 08, 2010 8:44 am
Posts: 130
Use run-time annotations?


Top
 Profile  
 
 Post subject: Re: How to discover the persistent obj id attr. name at runtime?
PostPosted: Mon Nov 01, 2010 1:02 pm 
Newbie

Joined: Fri Oct 22, 2010 11:06 pm
Posts: 11
Yeah that could be but I didn't want to introduce one more piece in the puzzle. Hibernate already detects this property on all mapped entities, so I was looking for a way to use Hibernate to obtain that information without having to write my own annotations.

Regarding scanning for Hibernate annotations, that's not too good because I'd have to look for Hibernate annotations and JPA too, and that wouldn't work for entities mapped with XML files.


Top
 Profile  
 
 Post subject: Re: How to discover the persistent obj id attr. name at runtime?
PostPosted: Mon Nov 01, 2010 2:47 pm 
Beginner
Beginner

Joined: Tue Oct 26, 2010 6:12 pm
Posts: 29
Quote:
I give an persistent instance (returned by a criteria query) to the DAO object and need to retrieve the name of its id attribute


I don't think JPA provides any mechanism to gather such meta information. However you could use Hibernate's ClassMetaData to fetch this information.

http://docs.jboss.org/hibernate/core/3. ... adata.html

But to get the class meta data you will need a handle to the session factory. Guess if you look in the org.hibernate.ejb package you might get some ideas on how to get that...

http://docs.jboss.org/hibernate/core/3. ... -tree.html


Top
 Profile  
 
 Post subject: Re: How to discover the persistent obj id attr. name at runtime?
PostPosted: Mon Nov 01, 2010 10:22 pm 
Newbie

Joined: Fri Oct 22, 2010 11:06 pm
Posts: 11
That's exactly what I was looking for!!

Thank you!!!!
Eduardo


Top
 Profile  
 
 Post subject: Re: How to discover the persistent obj id attr. name at runtime?
PostPosted: Wed Nov 03, 2010 12:27 pm 
Beginner
Beginner

Joined: Tue Oct 26, 2010 6:12 pm
Posts: 29
Quote:
I don't think JPA provides any mechanism to gather such meta information.


Actually, I was wrong on this one. JPA *does* provide this information using the Metamodel API (probably using the IdentifiableType interface).

http://download.oracle.com/javaee/6/api ... mmary.html


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