Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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="increment"/>
</property>
like image 827
Thizzer Avatar asked Oct 29 '10 10:10

Thizzer


People also ask

How can create column auto increment in Hibernate?

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.

Can you set auto increment?

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.

Does auto increment start at 0 or 1?

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).

Is it possible to set an Auto_increment field value manually?

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.


1 Answers

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
like image 105
Pascal Thivent Avatar answered Oct 27 '22 00:10

Pascal Thivent