您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

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

@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都有可能

其他 2022/1/1 18:18:23 有592人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶