HHH-4858 add implicit PERSIST cascade when @MapsId is present

git-svn-id: https://svn.jboss.org/repos/hibernate/core/trunk@18656 1b8cb986-b30d-0410-93ca-fae66ebed9b2
This commit is contained in:
Emmanuel Bernard 2010-01-27 18:58:28 +00:00
parent 66072429f4
commit 016a02ff50
2 changed files with 458 additions and 452 deletions

View File

@ -1328,7 +1328,9 @@ public final class
( (SimpleValue) rootClass.getVersion().getValue() ).getNullValue()
);
}
else if ( property.isAnnotationPresent( ManyToOne.class ) ) {
else {
final boolean isMapsId = property.isAnnotationPresent( MapsId.class );
if ( property.isAnnotationPresent( ManyToOne.class ) ) {
ManyToOne ann = property.getAnnotation( ManyToOne.class );
//check validity
@ -1350,9 +1352,9 @@ public final class
joinColumn.setSecondaryTableName( join.getTable().getName() );
}
}
final boolean mandatory = !ann.optional() || property.isAnnotationPresent( MapsId.class );
final boolean mandatory = !ann.optional() || isMapsId;
bindManyToOne(
getCascadeStrategy( ann.cascade(), hibernateCascade, false),
getCascadeStrategy( ann.cascade(), hibernateCascade, false, isMapsId),
joinColumns,
!mandatory,
ignoreNotFound, onDeleteCascade,
@ -1388,9 +1390,9 @@ public final class
}
}
//MapsId means the columns belong to the pk => not null
final boolean mandatory = !ann.optional() || property.isAnnotationPresent( MapsId.class );
final boolean mandatory = !ann.optional() || isMapsId;
bindOneToOne(
getCascadeStrategy( ann.cascade(), hibernateCascade, ann.orphanRemoval()),
getCascadeStrategy( ann.cascade(), hibernateCascade, ann.orphanRemoval(), isMapsId),
joinColumns,
!mandatory,
getFetchMode( ann.fetch() ),
@ -1425,7 +1427,7 @@ public final class
joinColumn.setSecondaryTableName( join.getTable().getName() );
}
}
bindAny( getCascadeStrategy( null, hibernateCascade, false), //@Any has not cascade attribute
bindAny( getCascadeStrategy( null, hibernateCascade, false, isMapsId), //@Any has not cascade attribute
joinColumns, onDeleteCascade, nullability,
propertyHolder, inferredData, entityBinder,
isIdentifierMapper, mappings );
@ -1638,7 +1640,7 @@ public final class
mappings.getReflectionManager().toXClass( oneToManyAnn.targetEntity() )
);
collectionBinder.setCascadeStrategy(
getCascadeStrategy( oneToManyAnn.cascade(), hibernateCascade, oneToManyAnn.orphanRemoval()) );
getCascadeStrategy( oneToManyAnn.cascade(), hibernateCascade, oneToManyAnn.orphanRemoval(), false) );
collectionBinder.setOneToMany( true );
}
else if ( elementCollectionAnn != null
@ -1665,7 +1667,7 @@ public final class
collectionBinder.setTargetEntity(
mappings.getReflectionManager().toXClass( manyToManyAnn.targetEntity() )
);
collectionBinder.setCascadeStrategy( getCascadeStrategy( manyToManyAnn.cascade(), hibernateCascade, false) );
collectionBinder.setCascadeStrategy( getCascadeStrategy( manyToManyAnn.cascade(), hibernateCascade, false, false) );
collectionBinder.setOneToMany( false );
}
else if ( property.isAnnotationPresent( ManyToAny.class ) ) {
@ -1673,7 +1675,7 @@ public final class
collectionBinder.setTargetEntity(
mappings.getReflectionManager().toXClass( void.class )
);
collectionBinder.setCascadeStrategy( getCascadeStrategy( null, hibernateCascade, false) );
collectionBinder.setCascadeStrategy( getCascadeStrategy( null, hibernateCascade, false, false) );
collectionBinder.setOneToMany( false );
}
collectionBinder.setMappedBy( mappedBy );
@ -1803,6 +1805,7 @@ public final class
}
}
}
}
//init index
//process indexes after everything: in second pass, many to one has to be done before indexes
Index index = property.getAnnotation( Index.class );
@ -2507,7 +2510,7 @@ public final class
private static String getCascadeStrategy(
javax.persistence.CascadeType[] ejbCascades, Cascade hibernateCascadeAnnotation,
boolean orphanRemoval) {
boolean orphanRemoval, boolean mapsId) {
EnumSet<CascadeType> hibernateCascadeSet = convertToHibernateCascadeType( ejbCascades );
CascadeType[] hibernateCascades = hibernateCascadeAnnotation == null ?
null :
@ -2521,6 +2524,9 @@ public final class
hibernateCascadeSet.add(CascadeType.DELETE_ORPHAN);
hibernateCascadeSet.add(CascadeType.REMOVE);
}
if (mapsId) {
hibernateCascadeSet.add(CascadeType.PERSIST);
}
StringBuilder cascade = new StringBuilder();
for ( CascadeType aHibernateCascadeSet : hibernateCascadeSet ) {

View File

@ -23,8 +23,8 @@ public class
d.emp = e;
d.id = new DependentId();
d.id.name = "Doggy";
s.persist( e );
s.persist( d );
s.persist( e );
s.flush();
s.clear();
d = (Dependent) s.get( Dependent.class, d.id );