Skip to content

Commit

Permalink
#414 - Test fixes and fixed #451
Browse files Browse the repository at this point in the history
  • Loading branch information
beikov committed Nov 6, 2017
1 parent c9fcd67 commit 7f85c73
Show file tree
Hide file tree
Showing 17 changed files with 135 additions and 72 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ public void testValuesEntityFunctionWithEntity() {
cb.orderByAsc("intEntity.name");

String expected = ""
+ "SELECT intEntity.name, doc.name FROM IntIdEntity(VALUES (?,?), (?,?)) intEntity LEFT JOIN Document doc" +
+ "SELECT intEntity.name, doc.name FROM IntIdEntity(VALUES (?,?,?), (?,?,?)) intEntity LEFT JOIN Document doc" +
onClause("doc.name = intEntity.name") +
" ORDER BY " + renderNullPrecedence("intEntity.name", "ASC", "LAST");

Expand Down Expand Up @@ -203,7 +203,7 @@ public void testValuesEntityFunctionParameter() {
cb.orderByAsc("intEntity.name");

String expected = ""
+ "SELECT intEntity.name, doc.name FROM IntIdEntity(VALUES (?,?)) intEntity LEFT JOIN Document doc" +
+ "SELECT intEntity.name, doc.name FROM IntIdEntity(VALUES (?,?,?)) intEntity LEFT JOIN Document doc" +
onClause("doc.name = intEntity.name") +
" ORDER BY " + renderNullPrecedence("intEntity.name", "ASC", "LAST");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import com.blazebit.persistence.view.CorrelationProvider;
import com.blazebit.persistence.view.FetchStrategy;
import com.blazebit.persistence.view.IdMapping;
import com.blazebit.persistence.view.InverseRemoveStrategy;
import com.blazebit.persistence.view.Mapping;
import com.blazebit.persistence.view.MappingCorrelated;
import com.blazebit.persistence.view.MappingCorrelatedSimple;
Expand Down Expand Up @@ -64,9 +63,6 @@ public abstract class AbstractAttribute<X, Y> implements Attribute<X, Y> {
protected final ManagedViewTypeImplementor<X> declaringType;
protected final Class<Y> javaType;
protected final String mapping;
protected final String mappedBy;
protected final Map<String, String> writableMappedByMapping;
protected final InverseRemoveStrategy inverseRemoveStrategy;
protected final String[] fetches;
protected final FetchStrategy fetchStrategy;
protected final int batchSize;
Expand Down Expand Up @@ -106,9 +102,6 @@ public AbstractAttribute(ManagedViewTypeImplementor<X> declaringType, AttributeM

if (mappingAnnotation instanceof IdMapping) {
this.mapping = ((IdMapping) mappingAnnotation).value();
this.mappedBy = null;
this.writableMappedByMapping = null;
this.inverseRemoveStrategy = null;
this.fetches = EMPTY;
this.fetchStrategy = FetchStrategy.JOIN;
this.batchSize = -1;
Expand All @@ -127,9 +120,6 @@ public AbstractAttribute(ManagedViewTypeImplementor<X> declaringType, AttributeM
} else if (mappingAnnotation instanceof Mapping) {
Mapping m = (Mapping) mappingAnnotation;
this.mapping = m.value();
this.mappedBy = mapping.getMappedBy();
this.writableMappedByMapping = mapping.getWritableMappedByMappings() == null ? null : Collections.unmodifiableMap(mapping.getWritableMappedByMappings());
this.inverseRemoveStrategy = mappedBy == null ? null : mapping.getInverseRemoveStrategy();
this.fetches = m.fetches();
this.fetchStrategy = m.fetch();
this.batchSize = batchSize;
Expand All @@ -147,9 +137,6 @@ public AbstractAttribute(ManagedViewTypeImplementor<X> declaringType, AttributeM
this.correlationExpression = null;
} else if (mappingAnnotation instanceof MappingParameter) {
this.mapping = ((MappingParameter) mappingAnnotation).value();
this.mappedBy = null;
this.writableMappedByMapping = null;
this.inverseRemoveStrategy = null;
this.fetches = EMPTY;
this.fetchStrategy = FetchStrategy.JOIN;
this.batchSize = -1;
Expand All @@ -169,9 +156,6 @@ public AbstractAttribute(ManagedViewTypeImplementor<X> declaringType, AttributeM
} else if (mappingAnnotation instanceof MappingSubquery) {
MappingSubquery mappingSubquery = (MappingSubquery) mappingAnnotation;
this.mapping = null;
this.mappedBy = null;
this.writableMappedByMapping = null;
this.inverseRemoveStrategy = null;
this.fetches = EMPTY;
this.subqueryProvider = mappingSubquery.value();
this.fetchStrategy = FetchStrategy.JOIN;
Expand All @@ -198,9 +182,6 @@ public AbstractAttribute(ManagedViewTypeImplementor<X> declaringType, AttributeM
} else if (mappingAnnotation instanceof MappingCorrelated) {
MappingCorrelated mappingCorrelated = (MappingCorrelated) mappingAnnotation;
this.mapping = null;
this.mappedBy = null;
this.writableMappedByMapping = null;
this.inverseRemoveStrategy = null;
this.fetches = mappingCorrelated.fetches();
this.fetchStrategy = mappingCorrelated.fetch();

Expand Down Expand Up @@ -230,9 +211,6 @@ public AbstractAttribute(ManagedViewTypeImplementor<X> declaringType, AttributeM
} else if (mappingAnnotation instanceof MappingCorrelatedSimple) {
MappingCorrelatedSimple mappingCorrelated = (MappingCorrelatedSimple) mappingAnnotation;
this.mapping = null;
this.mappedBy = null;
this.writableMappedByMapping = null;
this.inverseRemoveStrategy = null;
this.fetches = mappingCorrelated.fetches();
this.fetchStrategy = mappingCorrelated.fetch();

Expand Down Expand Up @@ -262,9 +240,6 @@ public AbstractAttribute(ManagedViewTypeImplementor<X> declaringType, AttributeM
} else {
context.addError("No mapping annotation could be found " + mapping.getErrorLocation());
this.mapping = null;
this.mappedBy = null;
this.writableMappedByMapping = null;
this.inverseRemoveStrategy = null;
this.fetches = EMPTY;
this.fetchStrategy = null;
this.batchSize = Integer.MIN_VALUE;
Expand Down Expand Up @@ -819,18 +794,6 @@ public final String getMapping() {
return mapping;
}

public String getMappedBy() {
return mappedBy;
}

public Map<String, String> getWritableMappedByMappings() {
return writableMappedByMapping;
}

public InverseRemoveStrategy getInverseRemoveStrategy() {
return inverseRemoveStrategy;
}

@Override
public final boolean isSubquery() {
return mappingType == MappingType.SUBQUERY;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,8 @@ public int getAttributeIndex() {

public abstract int getDirtyStateIndex();

public abstract Map<String, String> getWritableMappedByMappings();

@Override
public MemberType getMemberType() {
return MemberType.METHOD;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,14 @@
package com.blazebit.persistence.view.impl.metamodel;

import com.blazebit.persistence.view.CascadeType;
import com.blazebit.persistence.view.InverseRemoveStrategy;
import com.blazebit.persistence.view.metamodel.BasicType;
import com.blazebit.persistence.view.metamodel.FlatViewType;
import com.blazebit.persistence.view.metamodel.ManagedViewType;
import com.blazebit.persistence.view.metamodel.PluralAttribute;
import com.blazebit.persistence.view.metamodel.Type;

import javax.persistence.metamodel.ManagedType;
import java.util.Collections;
import java.util.Comparator;
import java.util.Map;
Expand All @@ -37,6 +39,9 @@ public abstract class AbstractMethodPluralAttribute<X, C, Y> extends AbstractMet

private final Type<Y> elementType;
private final int dirtyStateIndex;
private final String mappedBy;
private final Map<String, String> writableMappedByMapping;
private final InverseRemoveStrategy inverseRemoveStrategy;
private final boolean updatable;
private final boolean mutable;
private final boolean optimisticLockProtected;
Expand Down Expand Up @@ -108,6 +113,22 @@ public AbstractMethodPluralAttribute(ManagedViewTypeImplementor<X> viewType, Met
this.optimisticLockProtected = determineOptimisticLockProtected(mapping, context, mutable);
this.elementInheritanceSubtypes = (Map<ManagedViewType<? extends Y>, String>) (Map<?, ?>) mapping.getElementInheritanceSubtypes(context);
this.dirtyStateIndex = determineDirtyStateIndex(dirtyStateIndex);
if (this.dirtyStateIndex == -1) {
this.mappedBy = null;
this.inverseRemoveStrategy = null;
this.writableMappedByMapping = null;
} else {
ManagedType<?> managedType = context.getEntityMetamodel().getManagedType(declaringType.getEntityClass());
this.mappedBy = mapping.determineMappedBy(managedType, this.mapping, context);
if (this.mappedBy == null) {
this.inverseRemoveStrategy = null;
this.writableMappedByMapping = null;
} else {
this.inverseRemoveStrategy = mapping.getInverseRemoveStrategy();
this.writableMappedByMapping = mapping.determineWritableMappedByMappings(managedType, mappedBy, context);
}
}

this.sorted = mapping.isSorted();

this.ordered = mapping.getContainerBehavior() == AttributeMapping.ContainerBehavior.ORDERED;
Expand All @@ -120,6 +141,21 @@ public int getDirtyStateIndex() {
return dirtyStateIndex;
}

@Override
public Map<String, String> getWritableMappedByMappings() {
return writableMappedByMapping;
}

@Override
public String getMappedBy() {
return mappedBy;
}

@Override
public InverseRemoveStrategy getInverseRemoveStrategy() {
return inverseRemoveStrategy;
}

@Override
public boolean isUpdatable() {
return updatable;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package com.blazebit.persistence.view.impl.metamodel;

import com.blazebit.persistence.view.CascadeType;
import com.blazebit.persistence.view.InverseRemoveStrategy;
import com.blazebit.persistence.view.metamodel.BasicType;
import com.blazebit.persistence.view.metamodel.FlatViewType;
import com.blazebit.persistence.view.metamodel.ManagedViewType;
Expand All @@ -25,6 +26,7 @@
import com.blazebit.persistence.view.metamodel.Type;
import com.blazebit.persistence.view.spi.type.VersionBasicUserType;

import javax.persistence.metamodel.ManagedType;
import java.util.Collections;
import java.util.Map;
import java.util.Set;
Expand All @@ -38,6 +40,9 @@ public abstract class AbstractMethodSingularAttribute<X, Y> extends AbstractMeth

private final Type<Y> type;
private final int dirtyStateIndex;
private final String mappedBy;
private final Map<String, String> writableMappedByMapping;
private final InverseRemoveStrategy inverseRemoveStrategy;
private final boolean updatable;
private final boolean mutable;
private final boolean optimisticLockProtected;
Expand Down Expand Up @@ -113,13 +118,43 @@ public AbstractMethodSingularAttribute(ManagedViewTypeImplementor<X> viewType, M
this.optimisticLockProtected = determineOptimisticLockProtected(mapping, context, mutable);
this.inheritanceSubtypes = (Map<ManagedViewType<? extends Y>, String>) (Map<?, ?>) mapping.getInheritanceSubtypes(context);
this.dirtyStateIndex = determineDirtyStateIndex(dirtyStateIndex);
if (this.dirtyStateIndex == -1) {
this.mappedBy = null;
this.inverseRemoveStrategy = null;
this.writableMappedByMapping = null;
} else {
ManagedType<?> managedType = context.getEntityMetamodel().getManagedType(declaringType.getEntityClass());
this.mappedBy = mapping.determineMappedBy(managedType, this.mapping, context);
if (this.mappedBy == null) {
this.inverseRemoveStrategy = null;
this.writableMappedByMapping = null;
} else {
this.inverseRemoveStrategy = mapping.getInverseRemoveStrategy();
this.writableMappedByMapping = mapping.determineWritableMappedByMappings(managedType, mappedBy, context);
}
}
}

@Override
public int getDirtyStateIndex() {
return dirtyStateIndex;
}

@Override
public Map<String, String> getWritableMappedByMappings() {
return writableMappedByMapping;
}

@Override
public String getMappedBy() {
return mappedBy;
}

@Override
public InverseRemoveStrategy getInverseRemoveStrategy() {
return inverseRemoveStrategy;
}

@Override
public boolean isCollection() {
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,14 +166,16 @@ public void setDefaultBatchSize(Integer defaultBatchSize) {

public abstract String getMappedBy();

public abstract Map<String, String> getWritableMappedByMappings();

public abstract InverseRemoveStrategy getInverseRemoveStrategy();

public boolean isSorted() {
return containerBehavior == ContainerBehavior.SORTED;
}

public abstract String determineMappedBy(ManagedType<?> managedType, String mapping, MetamodelBuildingContext context);

public abstract Map<String, String> determineWritableMappedByMappings(ManagedType<?> managedType, String mappedBy, MetamodelBuildingContext context);

public boolean determineIndexed(MetamodelBuildingContext context, ManagedType<?> managedType) {
if (containerBehavior != null) {
return containerBehavior == ContainerBehavior.INDEXED;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,11 +147,12 @@ public ManagedViewTypeImpl(ViewMapping viewMapping, MetamodelBuildingContext con
attribute = mapping.getMethodAttribute(this, index, -1, context);
}
} else {
// Note that the dirty state index is only a "suggested" index, but the implementation can choose not to use it
attribute = mapping.getMethodAttribute(this, index, dirtyStateIndex, context);
}
if (attribute.getDirtyStateIndex() != -1) {
mutableAttributes.add(attribute);
dirtyStateIndex++;
if (attribute.getDirtyStateIndex() != -1) {
mutableAttributes.add(attribute);
dirtyStateIndex++;
}
}
hasJoinFetchedCollections = hasJoinFetchedCollections || attribute.hasJoinFetchedCollections();
attributes.put(mapping.getName(), attribute);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@ public class MethodAttributeMapping extends AttributeMapping implements EntityVi
private Boolean isUpdatable;
private Boolean isOptimisticLockProtected;
private String mappedBy;
private Map<String, String> writableMappedByMappings;
private boolean mappedByResolved;
private InverseRemoveStrategy inverseRemoveStrategy = InverseRemoveStrategy.SET_NULL;
private Set<CascadeType> cascadeTypes = Collections.emptySet();
Expand Down Expand Up @@ -151,11 +150,6 @@ public String getMappedBy() {
return mappedBy;
}

@Override
public Map<String, String> getWritableMappedByMappings() {
return writableMappedByMappings;
}

@Override
public void setMappedBy(String mappedBy) {
this.mappedBy = mappedBy;
Expand Down Expand Up @@ -227,7 +221,7 @@ public void initializeViewMappings(MetamodelBuildingContext context, Set<Class<?
}
}

private String determineMappedBy(ManagedType<?> managedType, String mapping, MetamodelBuildingContext context) {
public String determineMappedBy(ManagedType<?> managedType, String mapping, MetamodelBuildingContext context) {
if (mappedByResolved) {
return mappedBy;
}
Expand Down Expand Up @@ -264,16 +258,29 @@ private String determineMappedBy(ManagedType<?> managedType, String mapping, Met
}
}

private Map<String, String> determineWritableMappedByMappings(ManagedType<?> managedType, String mappedBy, MetamodelBuildingContext context) {
public Map<String, String> determineWritableMappedByMappings(ManagedType<?> managedType, String mappedBy, MetamodelBuildingContext context) {
ViewMapping elementViewMapping = getElementViewMapping();
EntityType<?> elementType;
if (elementViewMapping != null) {
elementType = context.getEntityMetamodel().entity(elementViewMapping.getEntityClass());
elementType = context.getEntityMetamodel().getEntity(elementViewMapping.getEntityClass());
} else {
elementType = context.getEntityMetamodel().entity(getDeclaredElementType());
Class<?> declaredElementType = getDeclaredElementType();
if (declaredElementType != null) {
elementType = context.getEntityMetamodel().getEntity(declaredElementType);
} else {
elementType = context.getEntityMetamodel().getEntity(getDeclaredType());
}
}
if (elementType == null) {
return null;
}

return context.getJpaProvider().getWritableMappedByMappings((EntityType<?>) managedType, elementType, mappedBy);
Map<String, String> writableMappedByMappings = context.getJpaProvider().getWritableMappedByMappings((EntityType<?>) managedType, elementType, mappedBy);
if (writableMappedByMappings == null) {
return null;
} else {
return Collections.unmodifiableMap(writableMappedByMappings);
}
}

private Set<ViewMapping> initializeDependentCascadeSubtypeMappings(MetamodelBuildingContext context, Set<Class<?>> dependencies, Set<Class<?>> subtypes) {
Expand Down Expand Up @@ -341,10 +348,6 @@ private static String methodReference(Method method) {
mappedBy = determineMappedBy(managedType, AbstractAttribute.stripThisFromMapping(((Mapping) mapping).value()), context);
}
mappedByResolved = true;
if (mappedBy != null && dirtyStateIndex != -1) {
ManagedType<?> managedType = context.getEntityMetamodel().getManagedType(viewType.getEntityClass());
writableMappedByMappings = determineWritableMappedByMappings(managedType, mappedBy, context);
}

boolean correlated = mapping instanceof MappingCorrelated || mapping instanceof MappingCorrelatedSimple;

Expand Down
Loading

0 comments on commit 7f85c73

Please sign in to comment.