diff --git a/hibernate-core/src/main/java/org/hibernate/mapping/SimpleValue.java b/hibernate-core/src/main/java/org/hibernate/mapping/SimpleValue.java index cd221b2f6e..5c672a0a4e 100644 --- a/hibernate-core/src/main/java/org/hibernate/mapping/SimpleValue.java +++ b/hibernate-core/src/main/java/org/hibernate/mapping/SimpleValue.java @@ -207,13 +207,13 @@ public abstract class SimpleValue implements KeyValue { final boolean[] originalUpdatability = ArrayHelper.toBooleanArray( updatability ); for ( int i = 0; i < originalOrder.length; i++ ) { final int originalIndex = originalOrder[i]; - final Selectable selectable = originalColumns[originalIndex]; + final Selectable selectable = originalColumns[i]; if ( selectable instanceof Column ) { - ( (Column) selectable ).setTypeIndex( i ); + ( (Column) selectable ).setTypeIndex( originalIndex ); } - columns.set( i, selectable ); - insertability.set( i, originalInsertability[originalIndex] ); - updatability.set( i, originalUpdatability[originalIndex] ); + columns.set( originalIndex, selectable ); + insertability.set( originalIndex, originalInsertability[i] ); + updatability.set( originalIndex, originalUpdatability[i] ); } } } diff --git a/hibernate-envers/src/test/java/org/hibernate/orm/test/envers/integration/inheritance/joined/JoinedSubclassWithEmbeddableIdTest.java b/hibernate-envers/src/test/java/org/hibernate/orm/test/envers/integration/inheritance/joined/JoinedSubclassWithEmbeddableIdTest.java new file mode 100644 index 0000000000..b8f7f7aaa7 --- /dev/null +++ b/hibernate-envers/src/test/java/org/hibernate/orm/test/envers/integration/inheritance/joined/JoinedSubclassWithEmbeddableIdTest.java @@ -0,0 +1,123 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * License: GNU Lesser General Public License (LGPL), version 2.1 or later. + * See the lgpl.txt file in the root directory or . + */ +package org.hibernate.orm.test.envers.integration.inheritance.joined; + +import org.hibernate.envers.Audited; +import org.hibernate.envers.configuration.EnversSettings; +import org.hibernate.testing.TestForIssue; +import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.ServiceRegistry; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; +import org.hibernate.testing.orm.junit.Setting; + +import org.junit.jupiter.api.Test; + +import jakarta.persistence.Embeddable; +import jakarta.persistence.EmbeddedId; +import jakarta.persistence.Entity; +import jakarta.persistence.Inheritance; +import jakarta.persistence.InheritanceType; +import jakarta.persistence.Table; +import jakarta.persistence.Version; + +import static org.junit.jupiter.api.Assertions.fail; + +/** + * @author Jan Schatteman + */ +@ServiceRegistry(settings = { + @Setting(name = EnversSettings.DO_NOT_AUDIT_OPTIMISTIC_LOCKING_FIELD, value = "false"), + @Setting(name = EnversSettings.AUDIT_TABLE_SUFFIX, value = "_L"), + @Setting(name = EnversSettings.REVISION_ON_COLLECTION_CHANGE, value = "false") +}) +@DomainModel( + annotatedClasses = { + JoinedSubclassWithEmbeddableIdTest.Parent.class, + JoinedSubclassWithEmbeddableIdTest.Child.class, + JoinedSubclassWithEmbeddableIdTest.ParentEmbeddedId.class + } +) +@SessionFactory +@TestForIssue( jiraKey = "HHH-15686") +public class JoinedSubclassWithEmbeddableIdTest { + + @Test + public void testColumnOrderingOnChildRevisionTable(SessionFactoryScope scope) { + scope.inTransaction( + session -> { + ParentEmbeddedId chId = new ParentEmbeddedId(); + chId.setId( 2 ); + chId.setOtherId( 21 ); + chId.setCompany( "Acme" ); + + Child c = new Child(); + c.setId( chId ); + c.setFullName( "John Doe" ); + c.setDescription( "I'm a childish entity" ); + + try { + session.persist( c ); + } catch (Exception e) { + fail(e); + } + } + ); + } + + @Entity(name = "Parent") + @Table(name = "T_Parent") + @Inheritance(strategy = InheritanceType.JOINED) + @Audited + public static class Parent { + + @EmbeddedId + private ParentEmbeddedId id; + private String description; + @Version + private int version; + + public void setId(ParentEmbeddedId id) { + this.id = id; + } + + public void setDescription(String description) { + this.description = description; + } + } + + @Embeddable + public static class ParentEmbeddedId { + private Integer id; + private String company; + private Integer otherId; + + public void setId(Integer id) { + this.id = id; + } + + public void setCompany(String company) { + this.company = company; + } + + public void setOtherId(Integer otherId) { + this.otherId = otherId; + } + } + + @Entity(name = "Child") + @Table(name = "T_Child") + @Audited + public static class Child extends Parent { + private String fullName; + + public void setFullName(String fullName) { + this.fullName = fullName; + } + } + +}