0
我很清楚地知道@SecondaryTable註釋是單個實體的字段映射到多張表,就好像這些表合併爲一單身。
我在實現這個概念時遇到問題。 有3類需要被持續下去,@SecondaryTables:映射一個實體到多張表
的Class1其2個字段出4要被存儲在2個其他不同的表
@Entity
@Table(name = "MAIN_CAT")
@SecondaryTables({
@SecondaryTable(name = "CAT_1", pkJoinColumns = {
@PrimaryKeyJoinColumn(name = "CAT1_PK", referencedColumnName = "id")
}),
@SecondaryTable(name = "CAT_2", uniqueConstraints = {
@UniqueConstraint(columnNames = {"storyPart2"})
})
})
public class Cat {
@[email protected]
private Long id;
private String catName;
@Column(table = "CAT_1")
private String storyPart1; // this field is going to be stored in CAT_1
@Column(table = "CAT_2")
private String storyPart2; // and so on
// getters and setters
}
2類:此類當持續存儲時,將storyPart1字段存儲在上述持久類中
@Entity
@Table(name = "CAT_1")
public class CatOne {
@Id
@GeneratedValue
@Column(name = "CAT1_PK")
private Long CAT1_PK;
@Column(name = "CATNAME_1")
private String catName1;
@Column(name = "CATAGE_1")
private String catAge1;
// getters and setters
}
3類:當持續將存儲持久類storyPart2字段1
@Entity
@Table(name = "CAT_2")
public class CatTwo {
@Id
@GeneratedValue
private Long id;
@Column(name = "CATNAME_2")
private String catName2;
@Column(name = "CATAGE_2")
private String catAge2;
// getters and setters
}
在數據庫中創建的3個表是這個類:
對於第1類:
CREATE TABLE main_cat
(
id bigint NOT NULL,
catname character varying(255),
CONSTRAINT main_cat_pkey PRIMARY KEY (id)
)
對於2類:
CREATE TABLE cat_1
(
storypart1 character varying(255),
cat1_pk bigint NOT NULL,
catage_1 character varying(255),
catname_1 character varying(255),
CONSTRAINT cat_1_pkey PRIMARY KEY (cat1_pk),
CONSTRAINT fk_kvtvo6nrv78wxkm0kv01k9kdq FOREIGN KEY (cat1_pk)
REFERENCES main_cat (id) MATCH SIMPLE
ON UPDATE NO ACTION ON DELETE NO ACTION
)
對於第3類:
CREATE TABLE cat_2
(
storypart2 character varying(255),
id bigint NOT NULL,
catage_2 character varying(255),
catname_2 character varying(255),
CONSTRAINT cat_2_pkey PRIMARY KEY (id),
CONSTRAINT fk_2duj6ebg3504x18mlw2xkpvg6 FOREIGN KEY (id)
REFERENCES main_cat (id) MATCH SIMPLE
ON UPDATE NO ACTION ON DELETE NO ACTION,
CONSTRAINT uk_5ko8732qslvvx98gy0q666qrf UNIQUE (storypart2)
)
的問題是,而不是插入在CAT_1一個記錄,它觸發2個插入用於相同。在CAT_2中插入記錄會發生同樣的情況。
Cat cat = new Cat();
cat.setCatName("CAT Name");
cat.setStoryPart1("STORYPART1");
cat.setStoryPart2("STORYPART2");
CatOne catOne = new CatOne();
catOne.setCatAge1("10");
catOne.setCatName1("catName1");
CatTwo catTwo = new CatTwo();
catTwo.setCatAge2("12");
catTwo.setCatName2("catName2");
session.save(cat);
session.save(catOne);
session.save(catTwo);
聲明:
Hibernate: insert into CAT_1 (storyPart1, CAT1_PK) values (?, ?)
Hibernate: insert into CAT_1 (CATAGE_1, CATNAME_1, CAT1_PK) values (?, ?, ?)
由於單個插入語句的分裂成2語句時,Hibernate拋出此錯誤。
Caused by: org.postgresql.util.PSQLException: ERROR: insert or update
on table "cat_1" violates foreign key constraint
"fk_kvtvo6nrv78wxkm0kv01k9kdq" Detail: Key (cat1_pk)=(2) is not
present in table "main_cat".
看起來像我已經以錯誤的方式實現了@SecondaryTable概念。如何讓Hibernate在這種情況下觸發單個插入呢?
沒有必要對這些,session.save(Catone的); session.save(catTwo); – 2015-02-06 14:26:34