Hibernate:@UniqueConstraint在映射到子类的超类中
Java EE 7, 冬眠5.4.21.1决赛 为什么Hibernate:@UniqueConstraint在映射到子类的超类中,hibernate,jpa,hibernate-mapping,unique-constraint,javax.persistence,Hibernate,Jpa,Hibernate Mapping,Unique Constraint,Javax.persistence,Java EE 7, 冬眠5.4.21.1决赛 为什么子类继承超类
子类
继承超类
子类
表映射期间使用超类
注释
如何在子类中重写@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\u multi\u col”
不会在子类
中重写,并在子类
表中生成两个唯一键。一个唯一键来自超类
,另一个来自子类
,其中应该只有一个(不包括主键)
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)
alter table test_supTable
drop index UK_multi_col
alter table test_supTable
add constraint UK_multi_col unique (colOne, colTwo)
接下来的四行是由超类
注释在子类
映射期间生成的代码:
alter table test_subTable
drop index UK_a5tjgjgpmww7otw30iyvmym1m
alter table test_subTable
add constraint UK_a5tjgjgpmww7otw30iyvmym1m unique (colOne, colTwo)
继续休眠生成的代码:
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)
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 bug吗???经过一天的搜索和测试,它看起来像是一个hibernate bug,使用EclipseLink进行测试会生成正确的db映射。我已经向hibernate提交了错误报告 请参阅:有关测试用例、EclipseLink项目文件和问题状态 如果有人遇到一个很好的解决方案,请张贴 更新: 该错误似乎已修复,请参阅: 更新: 此问题将在Hibernate 5.5.0版中修复