简体   繁体   中英

Hibernate: @UniqueConstraint in superclass mapping to subclass

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?

How do I override @UniqueConstraint in subclasses?

@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. One unique key is from the SuperClass and one from SubClass , where there should only be one (not including the primary key).

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

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:

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. I have submitted a bug report to hibernate

see: HHH-14234 for test cases, EclipseLink project files, and issue status.

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

UPDATE: this issue will be fixed in Hibernate version 5.5.0

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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