HHH-7940 - Add validation check for indexed fake bidirectional mappings.

This commit is contained in:
Chris Cranford 2016-07-29 14:43:25 -05:00
parent bf823d440c
commit af9882baae
3 changed files with 31 additions and 0 deletions

View File

@ -152,6 +152,19 @@ public final class CollectionMetadataGenerator {
final boolean owningManyToOneWithJoinTableBidirectional = (value instanceof ManyToOne) && (propertyAuditingData.getRelationMappedBy() != null);
final boolean fakeOneToManyBidirectional = (value instanceof OneToMany) && (propertyAuditingData.getAuditMappedBy() != null);
// HHH-7940
// When a @OneToMany(mappedBy="..") is mapped with a @ManyToOne without a join table and the @OneToMany
// uses an @IndexColumn/@OrderColumn annotation, a mapping exception is thrown to indicate how to
// properly map this relation as it needs to be considered a 'fakeOneToManyBidirectional' mapping.
if ( propertyAuditingData.isIndexed() ) {
if ( oneToManyAttachedType && inverseOneToMany && !fakeOneToManyBidirectional && !owningManyToOneWithJoinTableBidirectional ) {
throw new MappingException(
"Mapping failure for [" + referencingEntityConfiguration.getEntityClassName() + "#" + propertyName + "]: " +
"An indexed @OneToMany should be mapped by also using @JoinColumn and @AuditMappedBy."
);
}
}
if ( oneToManyAttachedType && (inverseOneToMany || fakeOneToManyBidirectional || owningManyToOneWithJoinTableBidirectional) ) {
// A one-to-many relation mapped using @ManyToOne and @OneToMany(mappedBy="...")
addOneToManyAttached( fakeOneToManyBidirectional );

View File

@ -17,9 +17,11 @@ import java.util.Set;
import javax.persistence.JoinColumn;
import javax.persistence.MapKey;
import javax.persistence.OneToMany;
import javax.persistence.OrderColumn;
import javax.persistence.Version;
import org.hibernate.MappingException;
import org.hibernate.annotations.IndexColumn;
import org.hibernate.annotations.common.reflection.ClassLoadingException;
import org.hibernate.annotations.common.reflection.ReflectionManager;
import org.hibernate.annotations.common.reflection.XClass;
@ -520,6 +522,7 @@ public class AuditedPropertiesReader {
addPropertyMapKey( property, propertyData );
setPropertyAuditMappedBy( property, propertyData );
setPropertyRelationMappedBy( property, propertyData );
setPropertyIndexed( property, propertyData );
return true;
}
@ -590,6 +593,12 @@ public class AuditedPropertiesReader {
}
}
private void setPropertyIndexed(XProperty property, PropertyAuditingData propertyData) {
if ( property.isAnnotationPresent( OrderColumn.class ) || property.isAnnotationPresent( IndexColumn.class ) ) {
propertyData.setIndexed( true );
}
}
private void addPropertyMapKey(XProperty property, PropertyAuditingData propertyData) {
final MapKey mapKey = property.getAnnotation( MapKey.class );
if ( mapKey != null ) {

View File

@ -35,6 +35,7 @@ public class PropertyAuditingData {
private boolean forceInsertable;
private boolean usingModifiedFlag;
private String modifiedFlagName;
private boolean indexed;
public PropertyAuditingData() {
}
@ -161,6 +162,14 @@ public class PropertyAuditingData {
this.modifiedFlagName = modifiedFlagName;
}
public boolean isIndexed() {
return indexed;
}
public void setIndexed(boolean indexed) {
this.indexed = indexed;
}
public void addAuditingOverride(AuditOverride annotation) {
if ( annotation != null ) {
final String overrideName = annotation.name();