A small tip to generate more meaningful Id. Instead of 32 for OrderID use O000000032.

Use smart and meaningful ids
Simple implementations:
simple Table Generator Based SmartID generator. it extends TableGenerator and simply returns the formatted value. you can use format parameter for format option
public class SmartIdTableGenerator
extends org.hibernate.id.TableGenerator{
private DecimalFormat format;
/**read configuration parameters*/
@Override
public void configure(
Type type, Properties params, Dialect dialect) {
super.configure( type, params, dialect);
String formatPattern = params.getProperty("format");
if(formatPattern!=null)
format=new DecimalFormat(formatPattern);
}
/**get super value and format it*/
@Override
public synchronized Serializable generate(
SessionImplementor session, Object object)
throws HibernateException {
Serializable generated = super.generate(session, object);
String v=null;
if(generated instanceof Number){
if(format!=null)
v=format.format(generated);
else
v=String.valueOf(generated);
}else
v=(String) generated;
return v;
}
}
Usage:
@Entity
public class SimpleTableWithFormatPO {
@Id
@Column(name = "id", precision = 32)
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "SmartIdTableGenerator")
@GenericGenerator(name = "SmartIdTableGenerator", strategy = "com.altuure.smartid.SmartIdTableGenerator",
parameters = {@Parameter(name = "format", value = "LD0000000")})
private String id;
simple SequenceGenerator Based SmartID generator. it extends SequenceGenerator and simply returns the formatted value
use format parameter for format option
public class SmartIdSequenceGenerator extends org.hibernate.id.SequenceGenerator{
private DecimalFormat format;
public void configure(Type type, Properties params, Dialect dialect) {
super.configure( type, params, dialect);
String formatPattern = params.getProperty("format");
if(formatPattern!=null)
format=new DecimalFormat(formatPattern);
}
/**get super value and format it*/
@Override
public synchronized Serializable generate(SessionImplementor session, Object object)
throws HibernateException {
Serializable generated = super.generate(session, object);
String v=null;
// since the type of id is String hibernate returns Long.toString() Value
if(generated instanceof String)
generated=Long.parseLong((String)generated);
if(generated instanceof Number){
if(format!=null)
v=format.format(generated);
else
v=String.valueOf(generated);
}else
v=(String) generated;
return v;
}
}
Usage:
@Entity
public class SimpleSequenceWithFormatPO {
@Id
@Column(name = "id", precision = 32)
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "SmartIdSequenceGenerator")
@GenericGenerator(name = "SmartIdSequenceGenerator", strategy = "com.altuure.smartid.SmartIdSequenceGenerator",
parameters = {@Parameter(name = "format", value = "LD0000000")})
private String id;
//...
}
You can extend this implementations and add time variables,system variables or other variables as your data structure needs
eg:
Sample Code
have fun
Related posts:
metavige said on Thursday, January 14, 2010, 8:15
if use your original code
IdentifierGeneratorFactory will throw
new IdentifierGenerationException( “this id generator generates long, integer, short” )
because in configure method, you call super.configure
and you pass your identify return class (it’s String.class)
so
you must change type when you call super.configure
you can change like LongType, IntType….
then it will be ok!!!