我正在使用 @GeneratedValue(strategy = GenerationType.AUTO) 生成实体的 ID。
我不知道它是如何工作的,但在我的子表中,它会生成遵循父序列的 ID 值。
//parent table
@Entity
@Table (name = "parent")
public class Parent {
@Id
@GeneratedValue (strategy = GenerationType.AUTO)
@Column (name = "id")
private long id;
@OneToMany (cascade = {CascadeType.ALL}, fetch = FetchType.LAZY)
@JoinColumn (name = "parentId")
@ForeignKey (name = "FKparent")
private List<child> child;
}
//child table
@Entity
@Table (name = "child")
public class Child {
@Id
@GeneratedValue (strategy = GenerationType.AUTO)
@Column (name = "id")
private long id;
}
父级插入的ID值更新了序列,子级插入的ID值也更新了序列。
在下一次父级插入时,序列使用子级插入更新的值......
这些注释并没有创建两个序列,只有一个。这是正确/预期的吗?
我只使用entityManager.persist(parent);
通过我的DAO服务插入了实体。