问题描述
我正在尝试创建一种自定义方式来计算并传递遵循我自己的模式的唯一ID.
I'm trying to create a custom way of computing and passing unique id's that follow my own pattern.
Hibernate具有@GenericGenerator批注,可用于映射自定义类以计算唯一ID并将其分配回@Id列.
Hibernate has the @GenericGenerator annotation that lets you map a custom class for computing a unique id and assigning it back to the @Id column.
示例
@Id
@GeneratedValue(generator="MyIdGenerator")
@GenericGenerator(name="MyIdGenerator", strategy="com.test.MyIdGenerator")
问题是我不想在程序包级别使用(休眠)@GenericGenerator.这可以在纯" JPA/2中吗?
The thing is that i don't want to use (hibernates) @GenericGenerator at package level.Can this be in "pure" JPA / 2 ?
感谢您的时间.
推荐答案
不,它没有.没有第三者的唯一可能性就是自己分配价值.如果您想避免调用设置id的方法,则可以使用Prepersist回调.
No, it doesn't have. Only possibility without 3rd party is to assign value by yourself. If you want to save yourself from calling method that sets id, then for example Prepersist callback can be used.
@PrePersist
public void ensureId() {
id = ...
}
这篇关于JPA是否具有类似休眠'@GenericGenerator'之类的东西来生成自定义ID?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!