gpt4 book ai didi

java - 如何将 Long id new java.util.Date().getTime() 设置为 @GenerateValue

转载 作者:行者123 更新时间:2023-12-02 03:39:52 29 4
gpt4 key购买 nike

如何将主键 Long id = new java.util.Date().getTime() 设置为 @GenerateValue?

我的一段代码:

private Long id;
@Id
@GeneratedValue(generator = "increment")
@GenericGenerator(name = "increment", strategy = "increment")
@Column(nullable = false, name = "id")
public Long getId() {
return id;
}

谢谢。

最佳答案

您尝试使用自定义生成器怎么样;

package x.y.z;
public class LongGenerator implements IdentifierGenerator {

@Override
public Serializable generate(final SessionImplementor sessionImplementor, final Object object) throws HibernateException {
return new java.util.Date().getTime();
}
}

和:

    private Long id;
@Id
@GeneratedValue(generator = "increment")
@GenericGenerator(name = "increment", strategy = "x.y.z.LongGenerator")
@Column(nullable = false, name = "id")
public Long getId() {
return id;
}

关于java - 如何将 Long id new java.util.Date().getTime() 设置为 @GenerateValue,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36958505/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com