AuditException with @OneToOne-mappedBy and @PrimaryKeyJoinColumn

Description

Both entities are audited:

Person {
@OneToOne(mappedBy = "person")
private Nutzerkonto nutzerkonto;
}

Nutzerkonto {
@OneToOne(optional = false)
@PrimaryKeyJoinColumn
private Person person;
}

Generate Person-audit:
reader.createQuery().forRevisionsOfEntity(Person.class, false, true)
.add(AuditEntity.id().eq(id)) ... .getResultList();

Caused by: org.hibernate.envers.exception.AuditException: This criterion can only be used on a property that is a relation to another property.
at org.hibernate.envers.query.criteria.RelatedAuditExpression.addToQuery(RelatedAuditExpression.java:52) [hibernate-envers-4.0.0.CR5.jar:4.0.0.CR5]
at org.hibernate.envers.query.impl.EntitiesAtRevisionQuery.list(EntitiesAtRevisionQuery.java:94) [hibernate-envers-4.0.0.CR5.jar:4.0.0.CR5]
at org.hibernate.envers.query.impl.AbstractAuditQuery.getSingleResult(AbstractAuditQuery.java:106) [hibernate-envers-4.0.0.CR5.jar:4.0.0.CR5]
at org.hibernate.envers.entities.mapper.relation.OneToOneNotOwningMapper.mapToEntityFromMap(OneToOneNotOwningMapper.java:80) [hibernate-envers-4.0.0.CR5.jar:4.0.0.CR5]
at org.hibernate.envers.entities.mapper.MultiPropertyMapper.mapToEntityFromMap(MultiPropertyMapper.java:115) [hibernate-envers-4.0.0.CR5.jar:4.0.0.CR5]
at org.hibernate.envers.entities.EntityInstantiator.createInstanceFromVersionsEntity(EntityInstantiator.java:99) [hibernate-envers-4.0.0.CR5.jar:4.0.0.CR5]
at org.hibernate.envers.query.impl.RevisionsOfEntityQuery.list(RevisionsOfEntityQuery.java:134) [hibernate-envers-4.0.0.CR5.jar:4.0.0.CR5]
at org.hibernate.envers.query.impl.AbstractAuditQuery.getResultList(AbstractAuditQuery.java:102) [hibernate-envers-4.0.0.CR5.jar:4.0.0.CR5]

Exception via:
org.hibernate.envers.query.criteria.RelatedAuditExpression:

public void addToQuery(AuditConfiguration auditCfg, String entityName, QueryBuilder qb, Parameters parameters) {
String propertyName = propertyNameGetter.get(auditCfg);

RelationDescription relatedEntity = CriteriaTools.getRelatedEntity(auditCfg, entityName, propertyName);

if (relatedEntity == null) {
throw new AuditException("This criterion can only be used on a property that is " +
"a relation to another property.");
} else {
relatedEntity.getIdMapper().addIdEqualsToQuery(parameters, id, null, equals);
}
}

propertyName is null!

triggered via org.hibernate.envers.entities.mapper.relation.OneToOneNotOwningMapper.mapToEntityFromMap:

value = versionsReader.createQuery().forEntitiesAtRevision(entityClass, owningEntityName, revision)
.add(AuditEntity.relatedId(owningReferencePropertyName).eq(primaryKey)).getSingleResult();

OneToOneNotOwningMapper.owningReferencePropertyName is null => this is the major problem here I think and may be connected with @PrimaryKeyJoinColumn. Mapper must choose the primary key and not the join column then.

quick hack:
public OneToOneNotOwningMapper(String owningReferencePropertyName, String owningEntityName,
PropertyData propertyData) {
// HACK for @OneToOne with @PrimaryKeyJoinColumn => null
this.owningReferencePropertyName = owningReferencePropertyName == null ? "id" : owningReferencePropertyName;

should handle this in org.hibernate.envers.configuration.metadata.ToOneRelationMetadataGenerator.addOneToOneNotOwning?! but better the experts...

Environment

JBoss AS 7.1.0 / JDK 1.6.0_29 / Windows 7 64

Status

Assignee

Lukasz Antoniak

Reporter

André Pankraz

Fix versions

Labels

None

backPortable

None

Suitable for new contributors

None

Requires Release Note

None

Pull Request

None

backportDecision

None

Components

Affects versions

4.0.0.CR5

Priority

Major
Configure