Spring Boot Hibernate子表PK错误地链接到父表PK

时间:2018-05-31 09:03:04

标签: spring hibernate jpa hibernate-mapping

在父表中

@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE)
@SequenceGenerator(name = "item")
private Long id;

@OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL,mappedBy = 
"item")
List<ItemTxn> itemTxns;

在儿童表中

@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE)
@SequenceGenerator(name = "itemTxn")
private Long id;

@ManyToOne(fetch = FetchType.LAZY)
Item item;

假设我的父表是一个带有pkid 1的行; 在儿童表中 我有一排 pkid -2 parent_table-id - 1;

将正确插入数据 parent_table-id正确引用父表 但子表中的pkid列也创建了一个到父表id列的FK链接。

我该如何防止这种情况?

编辑:

The Item_ID column in item_txn table correctly has a fk to the parent table - item on the ID column But the id column on the item_txn table is incorrectly referencing back to the item table id column as well

0 个答案:

没有答案