我想为directDebit类的id创建一个序列。 为此,我希望以格式(dd_mm_yy)显示日期,后跟一个数字,表示当天创建的数字。
// Class in java
@Entity
public class DirectDebit implements Serializable {
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "directDebit_generator")initialValue=1, allocationSize=50)
@Id
private String id;
...
}
-- create sequence in mysql
CREATE SEQUENCE directDebit_generator
START WITH 1
MAXVALUE 99999999
MINVALUE 1
NOCYCLE
NOCACHE
NOORDER;
-- Format date in mysql
SELECT DATE_FORMAT(CURRENT_TIMESTAMP, '%d_%m_%Y');
//Examples of id today
10_12_2016_1
10_12_2016_2
10_12_2016_3
...
//Examples of id yesterday
09_12_2016_1
09_12_2016_2
...
你明白我想要什么吗?
答案 0 :(得分:0)
您可以自己编写org.hibernate.id.SequenceGenerator
。进入generate(...)
可以调用您的SELECT并构建标识符。代码只是一个想法,不适用于copy& paster。
public class DirectDebitSequenceGenerator
extends SequenceGenerator
{
@Override
public Serializable generate(SessionImplementor session, Object obj)
{
// Your code
}
}
实体:
public class Entity {
@Id
@GeneratedValue(strategy = "GenerationType.SEQUENCE", generator = "generator")
@GenericGenerator(name = "generator", strategy = "...DirectDebitSequenceGenerator")
private String id;
...
}