简体   繁体   English

Hibernate自动增量属性

[英]Hibernate auto-increment property

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>

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). 不,你不能在<property>使用<generator> (或者,用简单的英文写,Hibernate只支持使用生成器来识别属性)。

Maybe have a look at generated properties if you can rely on the database to generate the value (eg using a trigger). 如果您可以依赖数据库来生成值(例如使用触发器),也许可以查看生成的属性

References 参考

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. 例如,如果您使用的是Mysql,那么在表id字段中应该为该字段设置自动增量。 Hibernate will not generate it, but rather depend on database to do it. Hibernate不会生成它,而是依靠数据库来完成它。 If you use Hibernate tools to generate tables, it would take care of generating tables appropriately. 如果使用Hibernate工具生成表,则需要适当地生成表。 You can refer to http://docs.jboss.org/hibernate/core/3.3/reference/en/html/mapping.html#mapping-declaration-id 你可以参考http://docs.jboss.org/hibernate/core/3.3/reference/en/html/mapping.html#mapping-declaration-id

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM