Java @Id @GeneratedValue 但设置自己的 ID 值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2108178/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
@Id @GeneratedValue but set own ID value
提问by woezelmann
I have a table with a generated id, but in some cases I would like to set it on my own. Can I, somehow, force Hibernate to ignore the @GeneratedValue?
我有一个带有生成 id 的表,但在某些情况下,我想自己设置它。我可以以某种方式强制 Hibernate 忽略 @GeneratedValue 吗?
采纳答案by Kannan Ekanath
It may be an overkill but have you thought about writing your own CustomIDGenerator which probably subclasses say the AutoGenerator of hibernate and exposes a couple of methods where you can set the id of the next class object to be generated so for example
这可能有点矫枉过正,但你有没有想过编写自己的 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
}
}
This could get complicated but at the least is possible as per hibernate doco
这可能会变得复杂,但至少根据hibernate doco是可能的
回答by Emmanuel Bernard
For you use case, you can manually add this no user. One way to do it is to put the insert operation on a file named "./import.sql" (in your classpath). Hibernate will go execute these statements when the SessionFactory is started.
对于您的用例,您可以手动添加此无用户。一种方法是将插入操作放在名为“./import.sql”的文件上(在您的类路径中)。当 SessionFactory 启动时,Hibernate 将执行这些语句。
回答by ahpoblete
Although this question was asked quite a while ago, I found the perfect answer for it in this postby @lOranger, and wanted to share it.
虽然这个问题很久以前就被问到了,但我在@lOranger 的这篇文章中找到了完美的答案,并想分享它。
This proposal checks if the object's current id is set to something other than null
, and if so, it uses it, otherwise, it generates it using the default (or configured) generation strategy.
该提议检查对象的当前 id 是否设置为 以外的其他值null
,如果是,则使用它,否则,它使用默认(或配置)生成策略生成它。
It's simple, straight forward, and addresses the issue brought up by @Jens, of one not being able to retrieve the object's current id.
这很简单,直接,并解决了@Jens 提出的问题,即无法检索对象的当前 id。
I just implemented it (by extending the UUIDGenerator), and it works like a charm :-D
我刚刚实现了它(通过扩展 UUIDGenerator),它就像一个魅力:-D
回答by rakesh
create your own identifiergenerator/sequencegenerator
创建您自己的标识符生成器/序列生成器
public class FilterIdentifierGenerator extends IdentityGenerator implements IdentifierGenerator{
@Override
public Serializable generate(SessionImplementor session, Object object)
throws HibernateException {
// TODO Auto-generated method stub
Serializable id = session.getEntityPersister(null, object)
.getClassMetadata().getIdentifier(object, session);
return id != null ? id : super.generate(session, object);
}
}
modify your entity as:
将您的实体修改为:
@Id
@GeneratedValue(generator="myGenerator")
@GenericGenerator(name="myGenerator", strategy="package.FilterIdentifierGenerator")
@Column(unique=true, nullable=false)
private int id;
...
and while saving instead of using persist()
use merge()
or update()
并在保存而不是使用persist()
usemerge()
或update()