-->
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: Which Generator Class to Use?
PostPosted: Mon Apr 23, 2007 6:46 am 
Newbie

Joined: Mon Apr 23, 2007 6:41 am
Posts: 1
In my Db , i have got a field which is of varchar type and is a primary key for the table. Coressponding to this , I have an attribute defined in Java class of type String. Which generator class should I use for this?

Thanks in Advance.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 23, 2007 6:55 am 
Regular
Regular

Joined: Mon Mar 26, 2007 12:38 am
Posts: 119
Hi,
If possible, use a number primary key. ( performance gains )
If not, the generator to use depends on what you want in the key ? ( the reason why string is chosen, may be some prefix or whatever )
Anyway, if you decide to go with it, here is one way.

Define a custom id generator,

public class MyIdGenerator implements IdentifierGenerator {
public Serializable generate(SessionImplementor session, Object object) throws HibernateException {
// Use your logic here. For example, I am using a sequence
SessionImpl sessionImpl = (SessionImpl) session ;
BigDecimal id = (BigDecimal) sessionImpl.createSQLQuery("select my_sequence.nextval from dual").list().get(0) ;
return "P" + id.toString() ; // format it as required
}
}


And then, use that generator in id mapping.

<id name="id" column="id" type="string">
<generator class="MyIdGenerator"></generator>
</id>



( similar issue )
http://forum.hibernate.org/viewtopic.php?t=973551

------------------------------------------------------------------
Rate the reply if you find it helpful


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 23, 2007 8:15 am 
Regular
Regular

Joined: Mon Jun 12, 2006 5:33 am
Posts: 63
Hi Mav_Rahul,

if you want hibernate to generate it for you, use UUID:

Code:
    <id name="Key" column="pid" type="string">
        <generator class="uuid"/>
    </id>


If you generate it yourself, use assigned:
Code:
       <id name="id" column="id">
            <generator class="assigned"/>
        </id>


chucky

Don't forget to rate if this helps
-------------------------------------------


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.