-->
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.  [ 4 posts ] 
Author Message
 Post subject: Getting ClassMetadata by entity name.
PostPosted: Fri Oct 29, 2010 6:53 am 
Newbie

Joined: Mon Jul 31, 2006 8:32 am
Posts: 7
Hi,

I'd like to get the instance of ClassMetadata for one of the entities. I'm using the @Entity JPA annotation that specifies the name of the entity as "customer". So what I intuitively put in my code is
Code:
session.getSessionFactory().getClassMetaData("customer")
. It didn't work and instead of "customer" (the entity name as specified in annotation) I have to use FQN of the class "com.gorilla.model.Customer".

Is there any way I could use the short entity name as specified in the annotation to get the ClassMetadata?

Cheers,
Tom


Top
 Profile  
 
 Post subject: Re: Getting ClassMetadata by entity name.
PostPosted: Sat Oct 30, 2010 1:24 am 
Beginner
Beginner

Joined: Tue Oct 26, 2010 6:12 pm
Posts: 29
Quote:
Is there any way I could use the short entity name as specified in the annotation to get the ClassMetadata?

May be not in a simple way. For some reason (bug?), the FQ name of the class (and not the entity name attribute you specify) is mapped to the corresponding class's ClassMetadata, resulting in the behavior you observe. The name you specify in the annotation is available in a property called nodeName. You can, of course, see the mappings that Hibernate has knowledge of and write a wrapper to get the behavior you desire.

Code:
            Configuration cfg = new AnnotationConfiguration()
                            .addAnnotatedClass(Customer.class)
                            .configure();

            sessionFactory = cfg.buildSessionFactory();

            Iterator classes = cfg.getClassMappings();
            while(classes.hasNext()){
                org.hibernate.mapping.PersistentClass p = (org.hibernate.mapping.PersistentClass) classes.next();
                System.out.println("Entity Name: " + p.getEntityName());
                System.out.println("Class Name: " + p.getClassName());
                System.out.println("Annotated name: " + p.getNodeName());
            }


Top
 Profile  
 
 Post subject: Re: Getting ClassMetadata by entity name.
PostPosted: Mon Nov 01, 2010 5:01 am 
Newbie

Joined: Mon Jul 31, 2006 8:32 am
Posts: 7
Yeah, I also have worked out this way of getting the issue resolved. But this is not very clean, because in the code I don't want to deal with Configuration object, but rather with Session (and consequently SessionFactory that facilitates the metadata-related functionality). It would be nice to be able to use "customer" and/or "com.gorilla.model.Customer".

I'll have a look a the code to see if this is something that I could implement in relatively short period of time ;)


Top
 Profile  
 
 Post subject: Re: Getting ClassMetadata by entity name.
PostPosted: Mon Nov 01, 2010 10:14 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 11, 2009 2:26 am
Posts: 29
try PersistentClass.getNodeName()


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