【问题标题】:Hibernate: @UniqueConstraint in superclass mapping to subclassHibernate:超类映射到子类中的@UniqueConstraint
【发布时间】:2020-09-25 17:13:45
【问题描述】:

Java EE 7, 休眠 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 表中生成两个唯一键。一个唯一键来自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 |

有没有人能解决这个问题?

这是一个休眠错误吗??

【问题讨论】:

    标签: hibernate jpa hibernate-mapping unique-constraint javax.persistence


    【解决方案1】:

    经过一天的搜索和测试,它看起来像是一个 Hibernate 错误,使用 EclipseLink 进行测试会生成正确的数据库映射。我已经向hibernate提交了一个错误报告

    请参阅:HHH-14234,了解测试用例、EclipseLink 项目文件和问题状态。

    如果有人遇到问题的良好解决方案,请发布。

    更新: 看起来该错误已修复,请参阅: https://github.com/hibernate/hibernate-orm/pull/3574

    更新: 这个问题将在 Hibernate 5.5.0 版本中修复

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-07-05
      • 1970-01-01
      • 2015-06-19
      • 2012-08-17
      • 2012-07-15
      • 2011-03-14
      • 2011-05-15
      • 1970-01-01
      相关资源
      最近更新 更多