Java 如何在hibernate和spring中生成此格式的订单号ORD00001

Java 如何在hibernate和spring中生成此格式的订单号ORD00001,java,spring,hibernate,Java,Spring,Hibernate,如何在hibernate和spring中生成此格式的订单号ORD00001 请帮助我生成上述编号。我尝试了各种方法生成此序列号,但没有有效的解决方案。试试这个 import org.hibernate.id.IdentifierGenerator; import org.hibernate.engine.SessionImplementor; import org.hibernate.HibernateException; import java.io.Serializable; import

如何在hibernate和spring中生成此格式的订单号ORD00001

请帮助我生成上述编号。我尝试了各种方法生成此序列号,但没有有效的解决方案。

试试这个

import org.hibernate.id.IdentifierGenerator;
import org.hibernate.engine.SessionImplementor;
import org.hibernate.HibernateException;

import java.io.Serializable;
import java.security.SecureRandom;
import java.util.UUID;

public class RandomIdentifierGenerator implements IdentifierGenerator {

  private final static String label = "ORD";
  private final static SecureRandom sr = new SecureRandom();

  public Serializable generate(SessionImplementor sessionImplementor, Object o) throws HibernateException {
    long val = sr.nextLong();
    return label + Long.toString(Math.abs(val), Character.MAX_RADIX);
  }
}

我想你真正需要的是用更实用的方式解析生成的订单,然后我不会使用生成随机ID的方法,我会使用订单的真实ID,它将是顺序的,并按照你的意愿对其进行格式化以得到结果。在代码中是这样的:

Class Order

@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private int id;

@Transient
public String getOrderIdBusiness(){
    DecimalFormat myFormatter = new DecimalFormat("ORD000000");
    return myFormatter.format(id);
}

我认为使用sequencethanx在数据库中处理它会更容易,但是如何在hibernate注释中实现呢?将这个类作为一个组件,并在DAO.thanx中自动连接它。好的,它工作得很好。还有一件事我想生成TRP001(系统日期)这样的行程顺序001.eg TRP001 2016-05-16 001.是否有任何最简单的方法生成此格式我得到了IlligalArgumentException:当我使用时无法将给定对象格式化为数字–Anthony Raymond Code我愿意这样做too@AnthonyRaymond很好,我认为这是一个简单易读的解决方案:)我想从web门户发送消息,但我的api无法发送字符串有空格。我怎么能在没有空格的情况下打印字符串?你能再详细一点你的问题吗?还是提出一个新问题?我不明白你的意思,空间在你的绳子的中间?可以使用regexp将其删除