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