开发者

Hibernate auto-increment property

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

<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="incr开发者_JAVA技巧ement"/>
</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).

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

References

  • 5.1.11. Property
  • 5.6. Generated properties


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜