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() );
relationalObjectBinder.bindColumns(
mappingDocument,
entitySource.getPrimaryKeyColumnSources(),
sortColumns( entitySource.getPrimaryKeyColumnSources(), entityDescriptor.getIdentifier() ),
keyBinding,
false,
new RelationalObjectBinder.ColumnNamingDelegate() {

View File

@ -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<Selectable> 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" );
}
}

View File

@ -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<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.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();
}
}

View File

@ -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" ) );
}
}