HHH-8567 - Query identifier properties
This commit is contained in:
parent
f675b67c27
commit
e5e5ef14eb
|
@ -23,6 +23,11 @@
|
|||
*/
|
||||
package org.hibernate.envers.query.criteria.internal;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
import org.hibernate.engine.spi.SessionFactoryImplementor;
|
||||
import org.hibernate.envers.configuration.spi.AuditConfiguration;
|
||||
import org.hibernate.envers.exception.AuditException;
|
||||
import org.hibernate.envers.internal.entities.RelationDescription;
|
||||
|
@ -30,6 +35,8 @@ import org.hibernate.envers.internal.entities.RelationType;
|
|||
import org.hibernate.envers.internal.reader.AuditReaderImplementor;
|
||||
import org.hibernate.envers.query.criteria.AuditId;
|
||||
import org.hibernate.envers.query.internal.property.PropertyNameGetter;
|
||||
import org.hibernate.type.EmbeddedComponentType;
|
||||
import org.hibernate.type.Type;
|
||||
|
||||
/**
|
||||
* @author Adam Warski (adam at warski dot org)
|
||||
|
@ -85,13 +92,40 @@ public abstract class CriteriaTools {
|
|||
public static String determinePropertyName(
|
||||
AuditConfiguration auditCfg, AuditReaderImplementor versionsReader,
|
||||
String entityName, String propertyName) {
|
||||
final SessionFactoryImplementor sessionFactory = versionsReader.getSessionImplementor().getFactory();
|
||||
|
||||
if ( AuditId.IDENTIFIER_PLACEHOLDER.equals( propertyName ) ) {
|
||||
final String identifierPropertyName = versionsReader.getSessionImplementor()
|
||||
.getFactory()
|
||||
.getEntityPersister( entityName )
|
||||
.getIdentifierPropertyName();
|
||||
final String identifierPropertyName = sessionFactory.getEntityPersister( entityName ).getIdentifierPropertyName();
|
||||
propertyName = auditCfg.getAuditEntCfg().getOriginalIdPropName() + "." + identifierPropertyName;
|
||||
}
|
||||
else {
|
||||
final List<String> identifierPropertyNames = identifierPropertyNames( sessionFactory, entityName );
|
||||
if ( identifierPropertyNames.contains( propertyName ) ) {
|
||||
propertyName = auditCfg.getAuditEntCfg().getOriginalIdPropName() + "." + propertyName;
|
||||
}
|
||||
}
|
||||
|
||||
return propertyName;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param sessionFactory Session factory.
|
||||
* @param entityName Entity name.
|
||||
*
|
||||
* @return List of property names representing entity identifier.
|
||||
*/
|
||||
private static List<String> identifierPropertyNames(SessionFactoryImplementor sessionFactory, String entityName) {
|
||||
final String identifierPropertyName = sessionFactory.getEntityPersister( entityName ).getIdentifierPropertyName();
|
||||
if ( identifierPropertyName != null ) {
|
||||
// Single id.
|
||||
return Arrays.asList( identifierPropertyName );
|
||||
}
|
||||
final Type identifierType = sessionFactory.getEntityPersister( entityName ).getIdentifierType();
|
||||
if ( identifierType instanceof EmbeddedComponentType ) {
|
||||
// Multiple ids.
|
||||
final EmbeddedComponentType embeddedComponentType = (EmbeddedComponentType) identifierType;
|
||||
return Arrays.asList( embeddedComponentType.getPropertyNames() );
|
||||
}
|
||||
return Collections.EMPTY_LIST;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -36,6 +36,10 @@ import org.hibernate.envers.query.AuditEntity;
|
|||
import org.hibernate.envers.test.BaseEnversJPAFunctionalTestCase;
|
||||
import org.hibernate.envers.test.Priority;
|
||||
import org.hibernate.envers.test.entities.StrIntTestEntity;
|
||||
import org.hibernate.envers.test.entities.ids.EmbId;
|
||||
import org.hibernate.envers.test.entities.ids.EmbIdTestEntity;
|
||||
import org.hibernate.envers.test.entities.ids.MulId;
|
||||
import org.hibernate.envers.test.entities.ids.MulIdTestEntity;
|
||||
import org.hibernate.envers.test.tools.TestTools;
|
||||
import org.hibernate.testing.TestForIssue;
|
||||
import org.junit.Assert;
|
||||
|
@ -50,10 +54,12 @@ public class SimpleQuery extends BaseEnversJPAFunctionalTestCase {
|
|||
private Integer id1;
|
||||
private Integer id2;
|
||||
private Integer id3;
|
||||
private MulId mulId1;
|
||||
private EmbId embId1;
|
||||
|
||||
@Override
|
||||
protected Class<?>[] getAnnotatedClasses() {
|
||||
return new Class[] {StrIntTestEntity.class};
|
||||
return new Class[] { StrIntTestEntity.class, MulIdTestEntity.class, EmbIdTestEntity.class };
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -80,6 +86,12 @@ public class SimpleQuery extends BaseEnversJPAFunctionalTestCase {
|
|||
// Revision 2
|
||||
em.getTransaction().begin();
|
||||
|
||||
mulId1 = new MulId( 1, 2 );
|
||||
em.persist( new MulIdTestEntity( mulId1.getId1(), mulId1.getId2(), "data" ) );
|
||||
|
||||
embId1 = new EmbId( 3, 4 );
|
||||
em.persist( new EmbIdTestEntity( embId1, "something" ) );
|
||||
|
||||
site1 = em.find( StrIntTestEntity.class, id1 );
|
||||
site2 = em.find( StrIntTestEntity.class, id2 );
|
||||
|
||||
|
@ -411,4 +423,39 @@ public class SimpleQuery extends BaseEnversJPAFunctionalTestCase {
|
|||
|
||||
Assert.assertEquals( site1, result );
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestForIssue(jiraKey = "HHH-8567")
|
||||
public void testIdPropertyRestriction() {
|
||||
StrIntTestEntity ver2 = (StrIntTestEntity) getAuditReader().createQuery()
|
||||
.forEntitiesAtRevision( StrIntTestEntity.class, 2 )
|
||||
.add( AuditEntity.property( "id" ).eq( id2 ) )
|
||||
.getSingleResult();
|
||||
|
||||
Assert.assertEquals( new StrIntTestEntity( "a", 20, id2 ), ver2 );
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestForIssue(jiraKey = "HHH-8567")
|
||||
public void testMultipleIdPropertyRestriction() {
|
||||
MulIdTestEntity ver2 = (MulIdTestEntity) getAuditReader().createQuery()
|
||||
.forEntitiesAtRevision( MulIdTestEntity.class, 2 )
|
||||
.add( AuditEntity.property( "id1" ).eq( mulId1.getId1() ) )
|
||||
.add( AuditEntity.property( "id2" ).eq( mulId1.getId2() ) )
|
||||
.getSingleResult();
|
||||
|
||||
Assert.assertEquals( new MulIdTestEntity( mulId1.getId1(), mulId1.getId2(), "data" ), ver2 );
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestForIssue(jiraKey = "HHH-8567")
|
||||
public void testEmbeddedIdPropertyRestriction() {
|
||||
EmbIdTestEntity ver2 = (EmbIdTestEntity) getAuditReader().createQuery()
|
||||
.forEntitiesAtRevision( EmbIdTestEntity.class, 2 )
|
||||
.add( AuditEntity.property( "id.x" ).eq( embId1.getX() ) )
|
||||
.add( AuditEntity.property( "id.y" ).eq( embId1.getY() ) )
|
||||
.getSingleResult();
|
||||
|
||||
Assert.assertEquals( new EmbIdTestEntity( embId1, "something" ), ver2 );
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue