[英]Hibernate: @UniqueConstraint in superclass mapping to subclass
Java EE 7,Hibernate 5.4.21.Final
為什么SubClass
繼承SuperClass
@UniqueConstraint
注解,或者更具體地說,為什么 Hibernate 在SubClass
表映射期間使用SuperClass
注解?
如何在子類中覆蓋@UniqueConstraint
?
@Entity
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
@Table( name = "supTable",
uniqueConstraints = {
@UniqueConstraint( name = "UK_multi_col",
columnNames = {"colOne", "colTwo"})
}
)
public class SuperClass implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.TABLE)
@Column(name = "id", unique = true, nullable = false)
protected Long id;
@Column(name = "colOne")
protected Long colOne;
@Column(name = "colTwo")
protected Long colTwo;
...
}
在@UniqueConstraint
中使用相同的名稱"UK_multi_col"
不會在SubClass
覆蓋並在SubClass
表中生成兩個 UNIQUE KEY。 一個唯一鍵來自SuperClass
,另一個來自SubClass
,其中應該只有一個(不包括主鍵)。
@Entity
@Table( name = "subTable",
uniqueConstraints = {
@UniqueConstraint( name = "UK_multi_col",
columnNames = {"colOne", "colTwo", "colThree"})
}
)
public class SubClass extends SuperClass {
@Column(name = "colThree")
protected Long colThree;
...
}
Hibernate 生成的代碼:
create table test_subTable (
id bigint not null,
colOne bigint,
colTwo bigint,
colThree bigint,
primary key (id)
) engine=InnoDB
create table test_supTable (
id bigint not null,
colOne bigint,
colTwo bigint,
primary key (id)
) engine=InnoDB
alter table test_subTable
drop index UK_multi_col
alter table test_subTable
add constraint UK_multi_col unique (colOne, colTwo, colThree)
接下來的四行是在SubClass
映射期間由SuperClass
注釋生成的代碼:
alter table test_subTable
drop index UK_a5tjgjgpmww7otw30iyvmym1m
alter table test_subTable
add constraint UK_a5tjgjgpmww7otw30iyvmym1m unique (colOne, colTwo)
繼續休眠生成的代碼:
alter table test_supTable
drop index UK_multi_col
alter table test_supTable
add constraint UK_multi_col unique (colOne, colTwo)
數據庫表:
| test_subtable | CREATE TABLE `test_subtable` (
`id` bigint(20) NOT NULL,
`colOne` bigint(20) DEFAULT NULL,
`colTwo` bigint(20) DEFAULT NULL,
`colThree` bigint(20) DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `UK_multi_col` (`colOne`,`colTwo`,`colThree`),
UNIQUE KEY `UK_a5tjgjgpmww7otw30iyvmym1m` (`colOne`,`colTwo`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 |
| test_suptable | CREATE TABLE `test_suptable` (
`id` bigint(20) NOT NULL,
`colOne` bigint(20) DEFAULT NULL,
`colTwo` bigint(20) DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `UK_multi_col` (`colOne`,`colTwo`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 |
有沒有人有解決這個問題的方法?
這是休眠錯誤嗎??
經過一天的搜索和測試,它看起來像是一個 Hibernate 錯誤,使用 EclipseLink 進行測試會產生正確的數據庫映射。 我已向休眠提交了錯誤報告
請參閱: HHH-14234以了解測試用例、EclipseLink 項目文件和問題狀態。
如果有人遇到該問題的良好解決方案,請發布。
更新:看起來錯誤已修復,請參閱: https : //github.com/hibernate/hibernate-orm/pull/3574
更新:此問題將在 Hibernate 5.5.0 版中修復
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.