Java Hibernate 自增属性

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4050979/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 10:59:07  来源:igfitidea点击:

Hibernate auto-increment property

javahibernatehibernate-mapping

提问by Thizzer

I know you can auto-increment an id by mapping;

我知道你可以通过映射自动增加一个 id;

<id column="user_id" name="id" >
<generator class="increment"/>
</id>

But is it also possible increment a property, what I tried so far didn't work;

但是是否也有可能增加一个属性,到目前为止我尝试过的不起作用;

<property column="numeric_value" name="nr" >
<generator class="increment"/>
</property>

采纳答案by Pascal Thivent

But is it also possible increment a property, what I tried so far didn't work;

但是是否也有可能增加一个属性,到目前为止我尝试过的不起作用;

No, you can't use a <generator>inside a <property>(or, to write it in plain english, Hibernate only supports using a generator for identifier properties).

不,您不能<generator>在 a 中使用 a <property>(或者,用简单的英语编写它,Hibernate 仅支持使用生成器来生成标识符属性)。

Maybe have a look at generatedpropertiesif you can rely on the database to generate the value (e.g. using a trigger).

如果您可以依靠数据库来生成值(例如使用触发器),也许可以查看生成的属性

References

参考

回答by ch4nd4n

It would depend on database and how you have created the table. For example if you are using Mysql, then in the table id field should have auto increment set for the field. Hibernate will not generate it, but rather depend on database to do it. If you use Hibernate tools to generate tables, it would take care of generating tables appropriately. You can refer to http://docs.jboss.org/hibernate/core/3.3/reference/en/html/mapping.html#mapping-declaration-id

这将取决于数据库以及您如何创建表。例如,如果您使用的是 Mysql,那么在表 id 字段中应该为该字段设置自动增量。Hibernate 不会生成它,而是依赖于数据库来完成它。如果您使用 Hibernate 工具生成表,它将负责适当地生成表。可以参考http://docs.jboss.org/hibernate/core/3.3/reference/en/html/mapping.html#mapping-declaration-id