简体   繁体   English

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

[英]Hibernate: @UniqueConstraint in superclass mapping to subclass

Java EE 7, Hibernate 5.4.21.Final Java EE 7,Hibernate 5.4.21.Final

Why does the SubClass inherit the SuperClass @UniqueConstraint annotations, or more specifically why does Hibernate use SuperClass annotations during SubClass table mapping?为什么SubClass继承SuperClass @UniqueConstraint注解,或者更具体地说,为什么 Hibernate 在SubClass表映射期间使用SuperClass注解?

How do I override @UniqueConstraint in subclasses?如何在子类中覆盖@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;
    ...
}

using the same name "UK_multi_col" in @UniqueConstraint does not override in SubClass and generates two UNIQUE KEYs in SubClass tables.@UniqueConstraint中使用相同的名称"UK_multi_col"不会在SubClass覆盖并在SubClass表中生成两个 UNIQUE KEY。 One unique key is from the SuperClass and one from SubClass , where there should only be one (not including the primary 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 generated code: 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)

next four lines is code generated by SuperClass annotations during SubClass mapping:接下来的四行是在SubClass映射期间由SuperClass注释生成的代码:

alter table test_subTable
    drop index UK_a5tjgjgpmww7otw30iyvmym1m    
alter table test_subTable
    add constraint UK_a5tjgjgpmww7otw30iyvmym1m unique (colOne, colTwo) 

continued hibernate generated code:继续休眠生成的代码:

alter table test_supTable
    drop index UK_multi_col
alter table test_supTable
    add constraint UK_multi_col unique (colOne, colTwo)

db tables:数据库表:

| 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 |

does anyone have a solution to this problem?有没有人有解决这个问题的方法?

Is this a hibernate bug??这是休眠错误吗??

After a day of searching and testing it looks like its a Hibernate bug, testing with EclipseLink produces the correct db mapping.经过一天的搜索和测试,它看起来像是一个 Hibernate 错误,使用 EclipseLink 进行测试会产生正确的数据库映射。 I have submitted a bug report to hibernate我已向休眠提交了错误报告

see: HHH-14234 for test cases, EclipseLink project files, and issue status.请参阅: HHH-14234以了解测试用例、EclipseLink 项目文件和问题状态。

If anyone comes across a good solution to the issue please post.如果有人遇到该问题的良好解决方案,请发布。

UPDATE: looks like the bug has been fixed, see: https://github.com/hibernate/hibernate-orm/pull/3574更新:看起来错误已修复,请参阅: https : //github.com/hibernate/hibernate-orm/pull/3574

UPDATE: this issue will be fixed in Hibernate version 5.5.0更新:此问题将在 Hibernate 5.5.0 版中修复

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM