Friday, March 12, 2010

hibernate tip: smart id generator

Tagged with: , ,
Tuesday, March 3, 2009, 8:00
This news item was posted in HowTo & Tutorial category and has 1 Comment so far.

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

Use smart and meaningful ids

Use smart and meaningful ids

Simple implementations:

  • SmartIdTableGenerator
  • SmartIdSequenceGenerator

SmartIdTableGenerator

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;

SmartIdSequenceGenerator

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:

  • LD200902000006
  • LDServerTitan00006

Sample Code
have fun :)

Share and Enjoy:
  • Digg
  • del.icio.us
  • Netvouz
  • DZone
  • Wists
  • LinkedIn
  • Slashdot
  • Google Bookmarks
  • Reddit
  • StumbleUpon
  • email
  • Twitter

Related posts:

  1. hibernate native Id generator tip
  2. DistributableEventPublisher for Spring via JGroups
  3. hibernate native id generator bug 3.2.4-ga
  4. who needs implementations?
  5. JGroups-Spring In Action

You can leave a response, or trackback from your own site.

One Response to “hibernate tip: smart id generator”

  1. 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!!!

Leave a Reply