Skip to content

Commit

Permalink
Support cascade style definition
Browse files Browse the repository at this point in the history
  • Loading branch information
DavideD committed Jan 16, 2019
1 parent df04f48 commit 2904294
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 4 deletions.
Expand Up @@ -135,7 +135,7 @@ else if ( action.performOnLazyProperty() ) {
}
}
else {
child = descriptor.getPropertyValue( parent, i );
child = descriptor.getPropertyValues( parent )[i];
}
cascadeProperty(
action,
Expand Down
Expand Up @@ -263,13 +263,43 @@ private static Map<String, CascadeStyle> buildBaseCascadeStyleMap() {
* @return The appropriate CascadeStyle
*/
public static CascadeStyle getCascadeStyle(String cascade) {
if ( cascade == null ) {
return NONE;
}

CascadeStyle style = STYLES.get( cascade );
if ( style == null ) {
throw new MappingException( "Unsupported cascade style: " + cascade );
if ( cascade.indexOf( "," ) > -1 ) {
return getMultipleCascadeStyle( cascade );
}
else {
throw new MappingException( "Unsupported cascade style: " + cascade );
}
}

return style;
}

private static CascadeStyle getMultipleCascadeStyle(String cascade) {
StringBuilder exceptionBuilder = new StringBuilder();
String[] cascadeTypes = cascade.split( "," );

CascadeStyle[] styles = new CascadeStyle[cascadeTypes.length];
int i = 0;
for ( String cascadeType : cascadeTypes ) {
CascadeStyle cascadeStyle = STYLES.get( cascadeType );
if ( cascadeStyle != null ) {
styles[i++] = cascadeStyle;
}
else {
exceptionBuilder.append( ", " );
exceptionBuilder.append( cascadeType );
}
}
else {
return style;
if ( exceptionBuilder.length() > 0 ) {
throw new MappingException( "Unsupported cascade styles: " + exceptionBuilder.substring( 2 ) );
}
return new MultipleCascadeStyle( styles );
}

public static void registerCascadeStyle(String name, BaseCascadeStyle cascadeStyle) {
Expand Down
Expand Up @@ -27,6 +27,8 @@
import org.hibernate.engine.FetchTiming;
import org.hibernate.engine.internal.ForeignKeys;
import org.hibernate.engine.internal.NonNullableTransientDependencies;
import org.hibernate.engine.spi.CascadeStyle;
import org.hibernate.engine.spi.CascadeStyles;
import org.hibernate.engine.spi.EntityKey;
import org.hibernate.engine.spi.EntityUniqueKey;
import org.hibernate.engine.spi.SharedSessionContractImplementor;
Expand Down Expand Up @@ -128,6 +130,7 @@ public class SingularPersistentAttributeEntity<O, J>
private final FetchStrategy fetchStrategy;

private final NotFoundAction notFoundAction;
private final String cascade;

private StateArrayContributor referencedUkAttribute;
private SingleEntityLoader singleEntityLoader;
Expand Down Expand Up @@ -202,13 +205,19 @@ public SingularPersistentAttributeEntity(

instantiationComplete( bootModelAttribute, context );

this.cascade = bootModelAttribute.getCascade();
this.fetchStrategy = DomainModelHelper.determineFetchStrategy(
bootModelAttribute,
runtimeModelContainer,
entityDescriptor
);
}

@Override
public CascadeStyle getCascadeStyle() {
return CascadeStyles.getCascadeStyle( this.cascade );
}

@Override
public boolean isCircular(FetchParent fetchParent) {
final NavigableContainer parentNavigableContainer = fetchParent.getNavigableContainer();
Expand Down
Expand Up @@ -27,6 +27,7 @@
import org.hibernate.cache.spi.entry.CacheEntryStructure;
import org.hibernate.engine.internal.Versioning;
import org.hibernate.engine.spi.CascadeStyle;
import org.hibernate.engine.spi.CascadeStyles;
import org.hibernate.engine.spi.EntityEntry;
import org.hibernate.engine.spi.SessionFactoryImplementor;
import org.hibernate.engine.spi.SharedSessionContractImplementor;
Expand Down Expand Up @@ -876,6 +877,12 @@ public CascadeStyle[] getPropertyCascadeStyles() {

@Override
public boolean hasCascades() {
for ( StateArrayContributor contributor : getStateArrayContributors() ) {
CascadeStyle cascadeStyle = contributor.getCascadeStyle();
if ( CascadeStyles.NONE != cascadeStyle ) {
return true;
}
}
return false;
}

Expand Down

0 comments on commit 2904294

Please sign in to comment.