Java EE 7,休眠 5.4.21.Final
为什么SubClass
继承SuperClass
@UniqueConstraint
注解,或者更具体地说,为什么 Hibernate在表映射SuperClass
期间使用注解?SubClass
如何@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;
...
}
使用相同的名称"UK_multi_col"
in@UniqueConstraint
不会覆盖 in并在表中SubClass
生成两个 UNIQUE KEY 。SubClass
一个唯一键来自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)
接下来的四行是映射SuperClass
期间注释生成的代码:SubClass
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 |
有没有人可以解决这个问题?
这是一个休眠错误吗?