From 5b8bae5cc7cc14598fc630ab6bbd953addc56f2d Mon Sep 17 00:00:00 2001 From: Nathan Xu Date: Tue, 29 Sep 2020 17:28:35 -0400 Subject: [PATCH] HHH-14234 fix issue denormalized table should not inherit indexes from parent table --- .../hibernate/mapping/DenormalizedTable.java | 10 ++- ...lePhysicalIncludedTableConstraintTest.java | 82 +++++++++++++++++++ 2 files changed, 88 insertions(+), 4 deletions(-) create mode 100644 hibernate-core/src/test/java/org/hibernate/test/inheritance/DenormalizedTablePhysicalIncludedTableConstraintTest.java diff --git a/hibernate-core/src/main/java/org/hibernate/mapping/DenormalizedTable.java b/hibernate-core/src/main/java/org/hibernate/mapping/DenormalizedTable.java index 74362c4691..7f88b02f09 100644 --- a/hibernate-core/src/main/java/org/hibernate/mapping/DenormalizedTable.java +++ b/hibernate-core/src/main/java/org/hibernate/mapping/DenormalizedTable.java @@ -111,10 +111,12 @@ public class DenormalizedTable extends Table { @Override public Iterator getUniqueKeyIterator() { - Iterator iter = includedTable.getUniqueKeyIterator(); - while ( iter.hasNext() ) { - UniqueKey uk = (UniqueKey) iter.next(); - createUniqueKey( uk.getColumns() ); + if ( !includedTable.isPhysicalTable() ) { + Iterator iter = includedTable.getUniqueKeyIterator(); + while ( iter.hasNext() ) { + UniqueKey uk = (UniqueKey) iter.next(); + createUniqueKey( uk.getColumns() ); + } } return getUniqueKeys().values().iterator(); } diff --git a/hibernate-core/src/test/java/org/hibernate/test/inheritance/DenormalizedTablePhysicalIncludedTableConstraintTest.java b/hibernate-core/src/test/java/org/hibernate/test/inheritance/DenormalizedTablePhysicalIncludedTableConstraintTest.java new file mode 100644 index 0000000000..1fdf19198e --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/test/inheritance/DenormalizedTablePhysicalIncludedTableConstraintTest.java @@ -0,0 +1,82 @@ +package org.hibernate.test.inheritance; + +import java.io.Serializable; +import java.util.Map; +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.Id; +import javax.persistence.Inheritance; +import javax.persistence.InheritanceType; +import javax.persistence.Table; +import javax.persistence.UniqueConstraint; + +import org.hibernate.cfg.AvailableSettings; +import org.hibernate.dialect.MariaDBDialect; +import org.hibernate.dialect.MySQLDialect; + +import org.hibernate.testing.SkipForDialect; +import org.hibernate.testing.SkipForDialects; +import org.hibernate.testing.TestForIssue; +import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase; +import org.junit.Test; + +/** + * @author Vlad Paln + * @author Nathan Xu + */ +@TestForIssue( jiraKey = "HHH-14234" ) +@SkipForDialects( { + @SkipForDialect( value = MySQLDialect.class, comment = "skip it for it support constraint name uniqueness in table, not db" ), + @SkipForDialect( value = MariaDBDialect.class, comment = "skip it for it support constraint name uniqueness in table, not db" ) +} ) +public class DenormalizedTablePhysicalIncludedTableConstraintTest extends BaseNonConfigCoreFunctionalTestCase { + + @Override + protected void addSettings(Map settings) { + settings.put( AvailableSettings.HBM2DDL_HALT_ON_ERROR, Boolean.TRUE.toString() ); + } + + @Override + protected Class[] getAnnotatedClasses() { + return new Class[] { + SuperClass.class, + SubClass.class + }; + } + + @Test + public void testUniqueConstraintFromSupTableNotAppliedToSubTable() { + // Unique constraint should be unique in db (except for MySQL and Mariadb). + // Without fixing, exception will be thrown when unique constraint in 'supTable' is applied to 'subTable' as well + } + + @Entity(name = "SuperClass") + @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS) + @Table( name = "supTable", + uniqueConstraints = { + @UniqueConstraint( name = "UK", + columnNames = {"colOne", "colTwo"}) + } + ) + static class SuperClass implements Serializable { + + @Id + @GeneratedValue + Long id; + + @Column(name = "colOne") + Long colOne; + + @Column(name = "colTwo") + Long colTwo; + } + + @Entity(name = "SubClass") + @Table( name = "subTable" ) + static class SubClass extends SuperClass { + + @Column(name = "colThree") + Long colThree; + } +}