Fix HBM joined-subclass key order by sorting

This commit is contained in:
Chris Cranford 2021-11-27 02:33:53 -05:00
parent f83fb54614
commit 9124fd84b4
5 changed files with 41 additions and 14 deletions

View File

@ -618,7 +618,7 @@ public class ModelBinder {
keyBinding.setCascadeDeleteEnabled( entitySource.isCascadeDeleteEnabled() ); keyBinding.setCascadeDeleteEnabled( entitySource.isCascadeDeleteEnabled() );
relationalObjectBinder.bindColumns( relationalObjectBinder.bindColumns(
mappingDocument, mappingDocument,
entitySource.getPrimaryKeyColumnSources(), sortColumns( entitySource.getPrimaryKeyColumnSources(), entityDescriptor.getIdentifier() ),
keyBinding, keyBinding,
false, false,
new RelationalObjectBinder.ColumnNamingDelegate() { new RelationalObjectBinder.ColumnNamingDelegate() {

View File

@ -9,6 +9,7 @@ package org.hibernate.envers.test.integration.reventity;
import java.util.Iterator; import java.util.Iterator;
import org.hibernate.mapping.Selectable;
import org.hibernate.orm.test.envers.BaseEnversJPAFunctionalTestCase; 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.ChildEntity;
import org.hibernate.orm.test.envers.integration.inheritance.joined.ParentEntity; 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 org.junit.Test;
import static org.junit.Assert.assertEquals; 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 * 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 @Test
public void testChildRevColumnType() { public void testChildRevColumnType() {
// We need the second column // Hibernate now sorts columns that are part of the key and therefore this test needs to test
Iterator childEntityKeyColumnsIterator = metadata() // for the existence of the specific key column rather than the expectation that is exists at
.getEntityBinding(ChildEntity.class.getName() + "_AUD" ) // a specific order in the iterator.
Iterator<Selectable> childEntityKeyColumnsIterator = metadata()
.getEntityBinding( ChildEntity.class.getName() + "_AUD" )
.getKey() .getKey()
.getColumnIterator(); .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" );
} }
} }

View File

@ -7,10 +7,13 @@
package org.hibernate.orm.test.envers; package org.hibernate.orm.test.envers;
import java.util.Arrays; import java.util.Arrays;
import java.util.Iterator;
import java.util.List; import java.util.List;
import org.hibernate.testing.cleaner.DatabaseCleaner; import org.hibernate.testing.cleaner.DatabaseCleaner;
import org.hibernate.mapping.Column;
import org.hibernate.mapping.Selectable;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.junit.runners.Parameterized; import org.junit.runners.Parameterized;
@ -47,4 +50,14 @@ public abstract class AbstractEnversTest {
public String getAuditStrategy() { public String getAuditStrategy() {
return auditStrategy; return auditStrategy;
} }
protected Column getColumnFromIteratorByName(Iterator<Selectable> iterator, String columnName) {
while ( iterator.hasNext() ) {
Column column = (Column) iterator.next();
if ( column.getName().equals( columnName) ) {
return column;
}
}
return null;
}
} }

View File

@ -22,6 +22,8 @@ import org.hibernate.engine.transaction.internal.jta.JtaStatusHelper;
import org.hibernate.envers.AuditReader; import org.hibernate.envers.AuditReader;
import org.hibernate.envers.AuditReaderFactory; import org.hibernate.envers.AuditReaderFactory;
import org.hibernate.envers.boot.internal.EnversIntegrator; 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.envers.configuration.EnversSettings;
import org.hibernate.internal.util.StringHelper; import org.hibernate.internal.util.StringHelper;
import org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl; 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.junit4.Helper;
import org.hibernate.testing.orm.jpa.PersistenceUnitDescriptorAdapter; import org.hibernate.testing.orm.jpa.PersistenceUnitDescriptorAdapter;
import org.hibernate.testing.orm.junit.DialectContext; import org.hibernate.testing.orm.junit.DialectContext;
import org.hibernate.service.ServiceRegistry;
import org.junit.After; import org.junit.After;
import jakarta.persistence.EntityManager; import jakarta.persistence.EntityManager;
@ -299,4 +303,9 @@ public abstract class BaseEnversJPAFunctionalTestCase extends AbstractEnversTest
em = entityManagerFactory.createEntityManager( properties ); em = entityManagerFactory.createEntityManager( properties );
return em; return em;
} }
public Configuration getConfiguration() {
final ServiceRegistry serviceRegistry = metadata().getMetadataBuildingOptions().getServiceRegistry();
return serviceRegistry.getService( EnversService.class ).getConfig();
}
} }

View File

@ -8,12 +8,13 @@
package org.hibernate.orm.test.envers.integration.inheritance.joined.primarykeyjoin; package org.hibernate.orm.test.envers.integration.inheritance.joined.primarykeyjoin;
import java.util.Arrays; import java.util.Arrays;
import jakarta.persistence.EntityManager; import jakarta.persistence.EntityManager;
import org.hibernate.mapping.PersistentClass;
import org.hibernate.orm.test.envers.BaseEnversJPAFunctionalTestCase; import org.hibernate.orm.test.envers.BaseEnversJPAFunctionalTestCase;
import org.hibernate.orm.test.envers.Priority; import org.hibernate.orm.test.envers.Priority;
import org.hibernate.orm.test.envers.integration.inheritance.joined.ParentEntity; import org.hibernate.orm.test.envers.integration.inheritance.joined.ParentEntity;
import org.hibernate.mapping.Column;
import org.junit.Assert; import org.junit.Assert;
import org.junit.Test; import org.junit.Test;
@ -82,11 +83,10 @@ public class ChildPrimaryKeyJoinAuditing extends BaseEnversJPAFunctionalTestCase
@Test @Test
public void testChildIdColumnName() { public void testChildIdColumnName() {
Assert.assertEquals( // Hibernate now sorts columns that are part of the key and therefore this test needs to test
"other_id", // for the existence of the specific key column rather than the expectation that is exists at
((Column) metadata().getEntityBinding( // a specific order in the iterator.
"org.hibernate.orm.test.envers.integration.inheritance.joined.primarykeyjoin.ChildPrimaryKeyJoinEntity_AUD" final PersistentClass persistentClass = metadata().getEntityBinding( ChildPrimaryKeyJoinEntity.class.getName() + "_AUD" );
).getKey().getColumnIterator().next()).getName() Assert.assertNotNull( getColumnFromIteratorByName( persistentClass.getKey().getColumnIterator(), "other_id" ) );
);
} }
} }