diff --git a/hibernate-core/src/main/java/org/hibernate/boot/model/source/internal/hbm/ModelBinder.java b/hibernate-core/src/main/java/org/hibernate/boot/model/source/internal/hbm/ModelBinder.java index 9379a3828e..43f5402f2b 100644 --- a/hibernate-core/src/main/java/org/hibernate/boot/model/source/internal/hbm/ModelBinder.java +++ b/hibernate-core/src/main/java/org/hibernate/boot/model/source/internal/hbm/ModelBinder.java @@ -618,7 +618,7 @@ public class ModelBinder { keyBinding.setCascadeDeleteEnabled( entitySource.isCascadeDeleteEnabled() ); relationalObjectBinder.bindColumns( mappingDocument, - entitySource.getPrimaryKeyColumnSources(), + sortColumns( entitySource.getPrimaryKeyColumnSources(), entityDescriptor.getIdentifier() ), keyBinding, false, new RelationalObjectBinder.ColumnNamingDelegate() { diff --git a/hibernate-envers/src/test/java/org/hibernate/envers/test/integration/reventity/LongRevEntityInheritanceChildAuditing.java b/hibernate-envers/src/test/java/org/hibernate/envers/test/integration/reventity/LongRevEntityInheritanceChildAuditing.java index 81b7dd1dc9..75d2a619ea 100644 --- a/hibernate-envers/src/test/java/org/hibernate/envers/test/integration/reventity/LongRevEntityInheritanceChildAuditing.java +++ b/hibernate-envers/src/test/java/org/hibernate/envers/test/integration/reventity/LongRevEntityInheritanceChildAuditing.java @@ -9,6 +9,7 @@ package org.hibernate.envers.test.integration.reventity; import java.util.Iterator; +import org.hibernate.mapping.Selectable; import org.hibernate.orm.test.envers.BaseEnversJPAFunctionalTestCase; import org.hibernate.orm.test.envers.integration.inheritance.joined.ChildEntity; import org.hibernate.orm.test.envers.integration.inheritance.joined.ParentEntity; @@ -18,6 +19,7 @@ import org.hibernate.orm.test.envers.integration.reventity.LongRevNumberRevEntit import org.junit.Test; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; /** * A join-inheritance test using a custom revision entity where the revision number is a long, mapped in the database @@ -33,14 +35,17 @@ public class LongRevEntityInheritanceChildAuditing extends BaseEnversJPAFunction @Test public void testChildRevColumnType() { - // We need the second column - Iterator childEntityKeyColumnsIterator = metadata() - .getEntityBinding(ChildEntity.class.getName() + "_AUD" ) + // Hibernate now sorts columns that are part of the key and therefore this test needs to test + // for the existence of the specific key column rather than the expectation that is exists at + // a specific order in the iterator. + Iterator childEntityKeyColumnsIterator = metadata() + .getEntityBinding( ChildEntity.class.getName() + "_AUD" ) .getKey() .getColumnIterator(); - childEntityKeyColumnsIterator.next(); - Column second = (Column) childEntityKeyColumnsIterator.next(); - assertEquals( second.getSqlType(), "int" ); + final String revisionColumnName = getConfiguration().getRevisionFieldName(); + Column column = getColumnFromIteratorByName( childEntityKeyColumnsIterator, revisionColumnName ); + assertNotNull( column ); + assertEquals( column.getSqlType(), "int" ); } } \ No newline at end of file diff --git a/hibernate-envers/src/test/java/org/hibernate/orm/test/envers/AbstractEnversTest.java b/hibernate-envers/src/test/java/org/hibernate/orm/test/envers/AbstractEnversTest.java index dcaa5c224f..2114ba96c0 100644 --- a/hibernate-envers/src/test/java/org/hibernate/orm/test/envers/AbstractEnversTest.java +++ b/hibernate-envers/src/test/java/org/hibernate/orm/test/envers/AbstractEnversTest.java @@ -7,10 +7,13 @@ package org.hibernate.orm.test.envers; import java.util.Arrays; +import java.util.Iterator; import java.util.List; import org.hibernate.testing.cleaner.DatabaseCleaner; +import org.hibernate.mapping.Column; +import org.hibernate.mapping.Selectable; import org.junit.runner.RunWith; import org.junit.runners.Parameterized; @@ -47,4 +50,14 @@ public abstract class AbstractEnversTest { public String getAuditStrategy() { return auditStrategy; } + + protected Column getColumnFromIteratorByName(Iterator iterator, String columnName) { + while ( iterator.hasNext() ) { + Column column = (Column) iterator.next(); + if ( column.getName().equals( columnName) ) { + return column; + } + } + return null; + } } diff --git a/hibernate-envers/src/test/java/org/hibernate/orm/test/envers/BaseEnversJPAFunctionalTestCase.java b/hibernate-envers/src/test/java/org/hibernate/orm/test/envers/BaseEnversJPAFunctionalTestCase.java index b49470cec7..329049e2db 100644 --- a/hibernate-envers/src/test/java/org/hibernate/orm/test/envers/BaseEnversJPAFunctionalTestCase.java +++ b/hibernate-envers/src/test/java/org/hibernate/orm/test/envers/BaseEnversJPAFunctionalTestCase.java @@ -22,6 +22,8 @@ import org.hibernate.engine.transaction.internal.jta.JtaStatusHelper; import org.hibernate.envers.AuditReader; import org.hibernate.envers.AuditReaderFactory; import org.hibernate.envers.boot.internal.EnversIntegrator; +import org.hibernate.envers.boot.internal.EnversService; +import org.hibernate.envers.configuration.Configuration; import org.hibernate.envers.configuration.EnversSettings; import org.hibernate.internal.util.StringHelper; import org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl; @@ -37,6 +39,8 @@ import org.hibernate.testing.jta.TestingJtaPlatformImpl; import org.hibernate.testing.junit4.Helper; import org.hibernate.testing.orm.jpa.PersistenceUnitDescriptorAdapter; import org.hibernate.testing.orm.junit.DialectContext; + +import org.hibernate.service.ServiceRegistry; import org.junit.After; import jakarta.persistence.EntityManager; @@ -299,4 +303,9 @@ public abstract class BaseEnversJPAFunctionalTestCase extends AbstractEnversTest em = entityManagerFactory.createEntityManager( properties ); return em; } + + public Configuration getConfiguration() { + final ServiceRegistry serviceRegistry = metadata().getMetadataBuildingOptions().getServiceRegistry(); + return serviceRegistry.getService( EnversService.class ).getConfig(); + } } diff --git a/hibernate-envers/src/test/java/org/hibernate/orm/test/envers/integration/inheritance/joined/primarykeyjoin/ChildPrimaryKeyJoinAuditing.java b/hibernate-envers/src/test/java/org/hibernate/orm/test/envers/integration/inheritance/joined/primarykeyjoin/ChildPrimaryKeyJoinAuditing.java index d82ce6ab2e..f17970f2a0 100644 --- a/hibernate-envers/src/test/java/org/hibernate/orm/test/envers/integration/inheritance/joined/primarykeyjoin/ChildPrimaryKeyJoinAuditing.java +++ b/hibernate-envers/src/test/java/org/hibernate/orm/test/envers/integration/inheritance/joined/primarykeyjoin/ChildPrimaryKeyJoinAuditing.java @@ -8,12 +8,13 @@ package org.hibernate.orm.test.envers.integration.inheritance.joined.primarykeyjoin; import java.util.Arrays; + import jakarta.persistence.EntityManager; +import org.hibernate.mapping.PersistentClass; import org.hibernate.orm.test.envers.BaseEnversJPAFunctionalTestCase; import org.hibernate.orm.test.envers.Priority; import org.hibernate.orm.test.envers.integration.inheritance.joined.ParentEntity; -import org.hibernate.mapping.Column; import org.junit.Assert; import org.junit.Test; @@ -82,11 +83,10 @@ public class ChildPrimaryKeyJoinAuditing extends BaseEnversJPAFunctionalTestCase @Test public void testChildIdColumnName() { - Assert.assertEquals( - "other_id", - ((Column) metadata().getEntityBinding( - "org.hibernate.orm.test.envers.integration.inheritance.joined.primarykeyjoin.ChildPrimaryKeyJoinEntity_AUD" - ).getKey().getColumnIterator().next()).getName() - ); + // Hibernate now sorts columns that are part of the key and therefore this test needs to test + // for the existence of the specific key column rather than the expectation that is exists at + // a specific order in the iterator. + final PersistentClass persistentClass = metadata().getEntityBinding( ChildPrimaryKeyJoinEntity.class.getName() + "_AUD" ); + Assert.assertNotNull( getColumnFromIteratorByName( persistentClass.getKey().getColumnIterator(), "other_id" ) ); } }