问题
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="increment"/>
</property>
回答1:
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
回答2:
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
来源:https://stackoverflow.com/questions/4050979/hibernate-auto-increment-property