@Id @GeneratedValue,但设置自己的ID值

@Id @GeneratedValue,但设置自己的ID值,第1张

@Id @GeneratedValue,但设置自己的ID值

可能是一个矫kill过正,但您是否考虑过编写自己的CustomIDGenerator,它的子类可能表示hibernate的AutoGenerator,并提供了一些方法,您可以在其中设置要生成的下一个类对象的ID,例如

class MyGenerator extends .... {public void setIdForObject(Class clazz, Long id) {    //once you use this API, the next time an object of     //type clazz is saved the id is used}public void setIdForObject(Class clazz, Long id, Matcher matcher) {    //once you use this API, the next time an object of     //type clazz is saved and the matcher matches yes the id will be     //assigned. Your matcher can match properties like name, age etc    //to say the matched object}}

这可能会变得复杂,但至少每个hibernatedoco都有可能



欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5163295.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-11-18
下一篇 2022-11-18

发表评论

登录后才能评论

评论列表(0条)

保存