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>
If you want to use this strategy, you have to annotate the primary key attribute @Id and with the @GeneratedValue annotation and set the strategy attribute to GenerationType. IDENTITY. If you now persist a new Author entity, Hibernate will use the auto-incremented database column to generate the primary key value.
Auto-increment allows a unique number to be generated automatically when a new record is inserted into a table. Often this is the primary key field that we would like to be created automatically every time a new record is inserted.
AUTO_INCREMENT columns start from 1 by default. The automatically generated value can never be lower than 0. Each table can have only one AUTO_INCREMENT column. It must defined as a key (not necessarily the PRIMARY KEY or UNIQUE key).
The default value is Yes. If you want to manually assign a value to a field that has the AutoIncrement property set to Yes, you must be member of the SQL Server db_owner database permission set.
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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With