如何在新记录插入时自动增加数据

时间:2010-11-16 10:22:56

标签: hibernate jpa

我想添加一个用户自定义列,它会在新记录插入时自动递增,我该怎么做?所以它运行在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}

注意:每个Parent中的值需要自动递增而不是在外面。

2 个答案:

答案 0 :(得分:0)

数据库处理这个,而不是Hibernate。您需要在表格中使用auto_increment代理主键。

答案 1 :(得分:0)

将Id类型设置为identity,DB表应该处理Identity插入。 sample,

<id
            name="dayTypeId"
            column="Day_Type_ID"
            type="int">

            <generator class="identity">
            </generator>
        </id>