hibernate custom generator

public class CustomIdSequenceGenerator 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);
    }
    public synchronized Serializable generate(SessionImplementor session, Object object)
		throws HibernateException {
        Serializable generated = super.generate(session, object);
        String v=null;
        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;
	}

}
自定义 TableGenerator

 

public class CustomIdTableGenerator extends org.hibernate.id.TableGenerator{
  
    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);
    }
    public synchronized Serializable generate(SessionImplementor session, Object object)
		throws HibernateException {
        Serializable generated = super.generate(session, object);
        String v=null;
        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;
	}

}
hibernate xml
  parameters = {
            @Parameter(name = "format", value = "CK0000000")
        }

  

你可能感兴趣的:(Hibernate,xml)