How to auto increment data on new record insertion
I would like to add a user defined column which will auto increment on new record insertion, how should I do this? so that it runs on h2, mysql
@Entity public class Parent {
@OneToMany List<Child> children;
}
@Entity public class Child {
int sortOrder;
}
e.g. Parent1 has Child{1, 2, 3}
Parent2 has {1, 2}
Note: Values within each Parent needs to be auto incre开发者_如何学JAVAmented not outside.
Set the Id type as identity, and the DB tbale should handle the Identity insertion.
sample,
<id
name="dayTypeId"
column="Day_Type_ID"
type="int">
<generator class="identity">
</generator>
</id>
The database handles this, not Hibernate. You need auto_increment surrogate primary keys in your tables.
精彩评论