-->
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: Dynamically determine generator-class of id-field?
PostPosted: Fri Jul 21, 2006 7:33 am 
Newbie

Joined: Fri Jul 21, 2006 7:20 am
Posts: 15
Hibernate version: 3.1.2
Name and version of the database you are using: MySQL 4.1.12

Ok; here's the problem. I'm creating a sort of 'hibernate-my-admin' application. So I have an insert page that determines the needed fields for a given classname.

Before I can show the form, I need to know if the identifier field of the object is 'assigned' (So I need to display an input field to let the user insert an ID) or if the identifier field is 'native' or at least managed by hibernate, so I can exclude it from the form.

Is there any way I can get this behaviour information (My first guess would be to look it up in the ClassMetadata, but I cannot find it there, also the getIdentifierType() seems to return insufficient information for my decision).

Thanks in advance,


Top
 Profile  
 
 Post subject: Solution
PostPosted: Fri Jul 21, 2006 10:01 am 
Newbie

Joined: Fri Jul 21, 2006 7:20 am
Posts: 15
Ok, I found out how to do this. Hopefully this will help other users as well.

If you want to know if an identifierField is 'assigned' or 'native', you can cast the classMetadata object to an EntityPersister. When you have an entity persitor you can ask for the identifierGenerator and check if it is an instance of org.hibernate.id.Assigned or org.hibernate.id.IdentityGenerator

Code:
EntityPersister ep = (EntityPersister) classMetadata;
Object identifierType = ep.getIdentifierGenerator();

System.out.println( "Class: " + classMetadata.getEntityName() );
System.out.println( "Has identity generator: + ep.getIdentifierGenerator().getClass() );


(Post a reply if this helped you :) )


Top
 Profile  
 
 Post subject: Re: Solution
PostPosted: Tue Nov 21, 2006 3:11 pm 
Newbie

Joined: Tue Aug 29, 2006 2:10 pm
Posts: 11
This is exactly what I was looking for. Perhaps ClassMetadata could have a getIdentifierGenerator method added so this hackery could be avoided.


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.