我以這種方式有一個一對多的物體關聯模型:
@Entity
@Table(name = "father")
public class Father {
@Id
@GeneratedValue
@Column(columnDefinition = "BINARY(16)", updatable = false)
@Type(type="org.hibernate.type.UUIDBinaryType")
private UUID id;
...
@OneToMany(cascade = CascadeType.ALL, mappedBy = "father", orphanRemoval = true)
private List<AbstractChild> children = new ArrayList<>();
...
}
@Entity
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(
name="type",
discriminatorType = DiscriminatorType.INTEGER,
columnDefinition = "TINYINT(1) NOT NULL")
@Table(
name = "child",
uniqueConstraints = @UniqueConstraint(columnNames = {"type", "name", "father_id"})
)
@IdClass(Child.class)
public abstract class AbstractChild implements Serializable {
@Id
@Column(nullable = false)
protected Locale locale;
@Id
@ManyToOne(fetch = FetchType.LAZY)
protected Father father;
@Id
@Column(name = "type", insertable = false, updatable = false)
protected short mediaType;
...
}
@Entity
@DiscriminatorValue("1")
public class ConcreteChildOne extends AbstractChild {
@Lob
private String content;
...
}
@Entity
@DiscriminatorValue("2")
public class ConcreteChildTwo extends AbstractChild {
@Column(name = "path")
private String path;
...
}
一切看起來都很好,但是當我用孩子保存父物體時,生成的 sql 中的值順序是錯誤的。
父親的插入查詢沒問題
insert
into
father
(description, name, id)
values
(?, ?, ?)
有那些系結引數
- 系結引數 [1] 作為 [VARCHAR] - [說明]
- 系結引數 [2] 作為 [VARCHAR] - [名稱]
- 系結引數 [3] 作為 [BINARY] - [3bfac6e0-08ec-4f6b-a62a-5626ee6ab0e5]
但是對孩子的插入查詢是錯誤的
insert
into
tourist_informations
(content, type, father_id, locale)
values
(?, ?, ?, ?)
- 系結引數 [1] 為 [CLOB] - [這是我的內容]
- 系結引數 [2] 作為 [BINARY] - [3bfac6e0-08ec-4f6b-a62a-5626ee6ab0e5]
- 將引數 [3] 系結為 [SMALLINT] - [1]
- 系結引數 [4] 作為 [VARCHAR] - [en_EN]
創建資料庫的sql是
create table father (
id BINARY(16) not null,
description varchar(255),
name varchar(150) not null,
primary key (id)
) engine=InnoDB
create table child (
type TINYINT(1) NOT NULL ,
locale varchar(255) not null,
path varchar(255),
content longtext,
father_id BINARY(16) not null,
primary key (father_id, type, locale)
) engine=InnoDB
alter table tourist_informations
add constraint FKpem984v0clq1bsvubkkynoq7x
foreign key (father_id)
references father (id)
有人認為為什么順序是錯誤的?謝謝指教。
uj5u.com熱心網友回復:
我認為你的映射是錯誤的。它應該是:
@OneToMany(cascade = CascadeType.ALL, mappedBy = "father", orphanRemoval = true)
private List<AbstractChild> children = new ArrayList<>();
uj5u.com熱心網友回復:
我找到了一張有效的地圖:
@Entity
@Table(name = "father")
public class Father {
@Id
@GeneratedValue
@Column(columnDefinition = "BINARY(16)", updatable = false)
@Type(type="org.hibernate.type.UUIDBinaryType")
private UUID id;
...
@OneToMany(cascade = CascadeType.ALL, mappedBy = "father", orphanRemoval = true)
private List<AbstractChild> children = new ArrayList<>();
...
}
@Embeddable
public class ChildID implements Serializable {
@Column(name = "type")
protected short type;
@Column(nullable = false)
protected String name;
@Column(columnDefinition = "BINARY(16)", name = "father_id")
@Type(type="org.hibernate.type.UUIDBinaryType")
private UUID fatherId;
...
}
@Entity
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(
name="type",
discriminatorType = DiscriminatorType.INTEGER,
columnDefinition = "TINYINT(1) NOT NULL")
@DiscriminatorOptions(force = true, insert = false)
@Table(
name = "child",
uniqueConstraints = @UniqueConstraint(columnNames = {"type", "name", "father_id"})
)
@IdClass(Child.class)
public abstract class AbstractChild implements Serializable {
@EmbeddedId
private ChildID id;
public AbstractChild() {
this.id = new ChildID();
}
@ManyToOne(fetch = FetchType.LAZY)
@MapsId("fatherId")
protected Father father;
...
}
@Entity
@DiscriminatorValue("1")
public class ConcreteChildOne extends AbstractChild {
@Lob
private String content;
public ConcreteChildOne() {
super();
this.getId().type = 1;
}
...
}
@Entity
@DiscriminatorValue("2")
public class ConcreteChildTwo extends AbstractChild {
@Column(name = "path")
private String path;
public ConcreteChildTwo() {
super();
this.getId().type = 2;
}
...
}
當然,您可以使用委托方法改進代碼以在 embedde d id 上設定型別或將其與 super() 傳遞給建構式,但這些都是細節。
希望回答能幫到大家謝謝大家的回答
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/318580.html
