Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2008/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Hibernate:@UniqueConstraint在映射到子类的超类中_Hibernate_Jpa_Hibernate Mapping_Unique Constraint_Javax.persistence - Fatal编程技术网

Hibernate:@UniqueConstraint在映射到子类的超类中

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决赛 为什么子类继承超类

Java EE 7, 冬眠5.4.21.1决赛

为什么
子类
继承
超类
注释,或者更具体地说,为什么Hibernate在
子类
表映射期间使用
超类
注释

如何在子类中重写
@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版中修复