-->
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.  [ 2 posts ] 
Author Message
 Post subject: I am looking for help about hibernate ID Generator.
PostPosted: Fri Apr 20, 2007 2:31 am 
Newbie

Joined: Sun Sep 19, 2004 9:23 pm
Posts: 7
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

Hibernate version:3.1

I am looking for help about hibernate ID Generator.
I have a table about person,it's primary key is char type.
the key is consisted of 10 digit of charactor string, which is lead by P,eg.P000000001,P000000002.

Somebody pls help me.
Thanks and regards,
Sunshine


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 20, 2007 3:35 am 
Regular
Regular

Joined: Mon Mar 26, 2007 12:38 am
Posts: 119
Hi,

Having a char primary key is not a great idea. ( affects performance )
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>



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


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