use of com.blazebit.persistence.view.metamodel.SingularAttribute in project blaze-persistence by Blazebit.
the class ViewTypeObjectBuilderTemplate method applySubviewMapping.
@SuppressWarnings("unchecked")
private ViewTypeObjectBuilderTemplate<Object[]>[] applySubviewMapping(MappingAttribute<? super T, ?> mappingAttribute, String subviewAttributePath, TupleIdDescriptor tupleIdDescriptor, ManagedViewTypeImplementor<Object[]> managedViewType, TupleElementMapperBuilder mapperBuilder, ViewJpqlMacro viewJpqlMacro, EmbeddingViewJpqlMacro embeddingViewJpqlMacro, ExpressionFactory ef, boolean isKey, boolean nullIfEmpty) {
AbstractAttribute<?, ?> attribute = (AbstractAttribute<?, ?>) mappingAttribute;
String subviewAliasPrefix = mapperBuilder.getAlias(mappingAttribute, isKey);
String subviewMappingPrefix;
String subviewIdPrefix;
int startIndex;
if (mappingAttribute.getFetchStrategy() == FetchStrategy.MULTISET) {
startIndex = 0;
} else {
startIndex = tupleOffset + mapperBuilder.mapperIndex();
}
boolean updatableObjectCache = managedViewType.isUpdatable() || managedViewType.isCreatable();
TupleIdDescriptor subviewTupleIdDescriptor = new TupleIdDescriptor(tupleIdDescriptor);
TupleIdDescriptor subviewIdDescriptor;
String multisetCorrelationExpression;
if (mappingAttribute.getFetchStrategy() != FetchStrategy.MULTISET) {
if (isKey) {
subviewMappingPrefix = mapperBuilder.getKeyMapping((MapAttribute<?, ?, ?>) mappingAttribute);
} else {
subviewMappingPrefix = mapperBuilder.getMapping(mappingAttribute);
}
subviewIdPrefix = subviewMappingPrefix;
multisetCorrelationExpression = null;
} else {
// Must be in sync with com.blazebit.persistence.view.impl.objectbuilder.mapper.MultisetTupleElementMapper.applyMapping
subviewMappingPrefix = getMultisetResultAlias(subviewAttributePath);
subviewIdPrefix = subviewMappingPrefix;
multisetCorrelationExpression = mapperBuilder.getMapping(mappingAttribute);
}
String oldViewPath = viewJpqlMacro.getViewPath();
viewJpqlMacro.setViewPath(subviewMappingPrefix);
if (managedViewType instanceof ViewType<?>) {
// When the attribute is not update mappable i.e. joining over other associations, we use its parent's parent id
if (attribute.isUpdateMappable()) {
subviewIdDescriptor = new TupleIdDescriptor();
} else {
subviewIdDescriptor = new TupleIdDescriptor(tupleIdDescriptor);
}
} else {
subviewIdDescriptor = new TupleIdDescriptor(tupleIdDescriptor);
subviewIdDescriptor.addIdPosition(flatViewIdPosition(mappingAttribute));
subviewTupleIdDescriptor.addIdPosition(flatViewIdPosition(mappingAttribute));
}
int endTupleElementsToAdd = 0;
String indexExpression = null;
ViewTypeObjectBuilderTemplate<Object[]> indexTemplate = null;
if (mappingAttribute.getFetchStrategy() == FetchStrategy.MULTISET) {
if (attribute.getKeyMappingExpression() != null) {
MapAttribute<?, ?, ?> mapAttribute = (MapAttribute<?, ?, ?>) attribute;
indexExpression = mapperBuilder.getKeyMapping(subviewMappingPrefix, mapAttribute);
if (mapAttribute.isKeySubview()) {
indexTemplate = new ViewTypeObjectBuilderTemplate<Object[]>(viewRoot, viewRootAlias, subviewAttributePath, subviewAliasPrefix, indexExpression, indexExpression, subviewTupleIdDescriptor, subviewIdDescriptor, 1, 0, viewJpqlMacro, embeddingViewJpqlMacro, (Map<ManagedViewType<? extends Object[]>, String>) (Map<?, ?>) mapAttribute.getKeyInheritanceSubtypeMappings(), evm, ef, (ManagedViewTypeImplementor<Object[]>) mapAttribute.getKeyType(), null, proxyFactory);
}
} else if (attribute.getMappingIndexExpression() != null) {
indexExpression = mapperBuilder.getIndexMapping(subviewMappingPrefix, (ListAttribute<?, ?>) attribute);
}
if (updatableObjectCache && managedViewType.getMappingType() == Type.MappingType.FLAT_VIEW) {
if (indexExpression != null) {
endTupleElementsToAdd = 1;
} else if (indexTemplate != null) {
endTupleElementsToAdd = indexTemplate.effectiveTupleSize;
}
}
}
Map<ManagedViewType<? extends Object[]>, String> inheritanceSubtypeMappings;
if (isKey) {
inheritanceSubtypeMappings = (Map<ManagedViewType<? extends Object[]>, String>) (Map<?, ?>) ((MapAttribute<?, ?, ?>) mappingAttribute).getKeyInheritanceSubtypeMappings();
} else if (mappingAttribute instanceof PluralAttribute<?, ?, ?>) {
inheritanceSubtypeMappings = (Map<ManagedViewType<? extends Object[]>, String>) (Map<?, ?>) ((PluralAttribute<?, ?, ?>) mappingAttribute).getElementInheritanceSubtypeMappings();
} else {
inheritanceSubtypeMappings = (Map<ManagedViewType<? extends Object[]>, String>) (Map<?, ?>) ((SingularAttribute<?, ?>) mappingAttribute).getInheritanceSubtypeMappings();
}
String embeddingViewPath = mapperBuilder.getMapping();
String oldEmbeddingViewPath = embeddingViewJpqlMacro.getEmbeddingViewPath();
embeddingViewJpqlMacro.setEmbeddingViewPath(embeddingViewPath);
ViewTypeObjectBuilderTemplate<Object[]> template = new ViewTypeObjectBuilderTemplate<Object[]>(viewRoot, viewRootAlias, subviewAttributePath, subviewAliasPrefix, subviewMappingPrefix, subviewIdPrefix, subviewTupleIdDescriptor, subviewIdDescriptor, startIndex, endTupleElementsToAdd, viewJpqlMacro, embeddingViewJpqlMacro, inheritanceSubtypeMappings, evm, ef, managedViewType, null, proxyFactory);
ViewTypeObjectBuilderTemplate<Object[]>[] templates = null;
if (mappingAttribute.getFetchStrategy() == FetchStrategy.MULTISET) {
String multisetResultAlias = getMultisetResultAlias(subviewAttributePath);
mapperBuilder.addMapper(new MultisetTupleElementMapper(template, multisetCorrelationExpression, subviewAttributePath, multisetResultAlias, embeddingViewPath, indexExpression, indexTemplate, createLimiter(mapperBuilder, multisetResultAlias, mappingAttribute)));
templates = new ViewTypeObjectBuilderTemplate[] { template, indexTemplate };
} else {
mapperBuilder.addMappers(template.mappers);
mapperBuilder.addSecondaryMappers(template.secondaryMappers);
mapperBuilder.addTupleTransformatorFactory(template.tupleTransformatorFactory);
mapperBuilder.addTupleTransformerFactory(new SubviewTupleTransformerFactory(subviewAttributePath, template, updatableObjectCache, nullIfEmpty));
}
embeddingViewJpqlMacro.setEmbeddingViewPath(oldEmbeddingViewPath);
viewJpqlMacro.setViewPath(oldViewPath);
return templates;
}
use of com.blazebit.persistence.view.metamodel.SingularAttribute in project blaze-persistence by Blazebit.
the class ProxyFactory method addSetter.
private CtMethod addSetter(AbstractMethodAttribute<?, ?> attribute, CtClass cc, CtField attributeField, String methodName, CtField mutableStateField, boolean dirtyChecking, boolean isId) throws CannotCompileException, NotFoundException {
FieldInfo finfo = attributeField.getFieldInfo2();
String fieldType = finfo.getDescriptor();
String desc = "(" + fieldType + ")V";
ConstPool cp = finfo.getConstPool();
MethodInfo minfo = new MethodInfo(cp, methodName, desc);
minfo.setAccessFlags(AccessFlag.PUBLIC);
String fieldName = finfo.getName();
StringBuilder sb = new StringBuilder();
sb.append("{\n");
boolean invalidSetter = false;
// When the declaring type is updatable/creatable we only allow setting the id value on new objects
if (isId) {
if (attribute != null && attribute.getDeclaringType().isCreatable()) {
sb.append("\tif ($0.$$_kind != (byte) 2) {\n");
sb.append("\t\tthrow new IllegalArgumentException(\"Updating the id attribute '").append(attribute.getName()).append("' is only allowed for new entity view objects created via EntityViewManager.create()!\");\n");
sb.append("\t}\n");
} else if (attribute != null && attribute.getDeclaringType().isUpdatable()) {
sb.append("\tthrow new IllegalArgumentException(\"Updating the id attribute '").append(attribute.getName()).append("' is only allowed for new entity view objects created via EntityViewManager.create()!\");\n");
invalidSetter = true;
}
}
// Disallow calling the setter on a mutable only relation with objects of a different identity as that might indicate a programming error
if (attribute != null && attribute.getDirtyStateIndex() != -1 && !attribute.isUpdatable() && (attribute.getDeclaringType().isCreatable() || attribute.getDeclaringType().isUpdatable())) {
sb.append("\tObject tmp;\n");
sb.append("\tif ($1 != $0.").append(fieldName);
if (attribute.isCollection()) {
// TODO: We could theoretically support collections too by looking into them and asserting equality element-wise
} else {
SingularAttribute<?, ?> singularAttribute = (SingularAttribute<?, ?>) attribute;
Type<?> type = singularAttribute.getType();
if (attribute.isSubview()) {
if (!(type instanceof FlatViewType<?>)) {
String idMethodName = ((ViewType<?>) type).getIdAttribute().getJavaMethod().getName();
sb.append(" && ");
sb.append("($1 == null || (tmp = $1.");
sb.append(idMethodName);
sb.append("()) == null || !java.util.Objects.equals(tmp, $0.");
sb.append(fieldName);
sb.append('.').append(idMethodName);
sb.append("()))");
}
} else {
BasicTypeImpl<?> basicType = (BasicTypeImpl<?>) type;
boolean jpaEntity = basicType.isJpaEntity();
if (jpaEntity) {
IdentifiableType<?> identifiableType = (IdentifiableType<?>) basicType.getManagedType();
for (javax.persistence.metamodel.SingularAttribute<?, ?> idAttribute : JpaMetamodelUtils.getIdAttributes(identifiableType)) {
Class<?> idClass = JpaMetamodelUtils.resolveFieldClass(basicType.getJavaType(), idAttribute);
String idAccessor = addIdAccessor(cc, identifiableType, idAttribute, pool.get(idClass.getName()));
sb.append(" && ");
sb.append("($1 == null || (tmp = ");
sb.append(idAccessor);
sb.append("($1)) == null || !java.util.Objects.equals(tmp, ");
sb.append(idAccessor);
sb.append("($0.");
sb.append(fieldName);
sb.append(")))");
}
}
}
}
sb.append(") {\n");
sb.append("\t\tthrow new IllegalArgumentException(\"Updating the mutable-only attribute '").append(attribute.getName()).append("' with a value that has not the same identity is not allowed! Consider making the attribute updatable or update the value directly instead of replacing it!\");\n");
sb.append("\t}\n");
}
if (attribute != null && attribute.isUpdatable() && dirtyChecking) {
if (attribute.isCollection()) {
if (strictCascadingCheck) {
// With strict cascading checks enabled, we don't allow setting collections of mutable subviews
boolean mutableElement = !attribute.getUpdateCascadeAllowedSubtypes().isEmpty() || !attribute.getPersistCascadeAllowedSubtypes().isEmpty();
if (mutableElement && ((AbstractMethodPluralAttribute<?, ?, ?>) attribute).getElementType().getMappingType() != Type.MappingType.BASIC) {
sb.append("\t\tthrow new IllegalArgumentException(\"Replacing a collection that PERSIST or UPDATE cascades is prohibited by default! Instead, replace the contents by doing clear() and addAll()!\");\n");
}
}
} else {
// Only consider subviews here for now
if (attribute.isSubview()) {
String subtypeArray = addAllowedSubtypeField(cc, attribute);
addParentRequiringUpdateSubtypesField(cc, attribute);
addParentRequiringCreateSubtypesField(cc, attribute);
sb.append("\tif ($1 != null) {\n");
sb.append("\t\tClass c;\n");
sb.append("\t\tboolean isNew;\n");
sb.append("\t\tif ($1 instanceof ").append(EntityViewProxy.class.getName()).append(") {\n");
sb.append("\t\t\tc = ((").append(EntityViewProxy.class.getName()).append(") $1).$$_getEntityViewClass();\n");
sb.append("\t\t\tisNew = ((").append(EntityViewProxy.class.getName()).append(") $1).$$_isNew();\n");
sb.append("\t\t} else {\n");
sb.append("\t\t\tc = $1.getClass();\n");
sb.append("\t\t\tisNew = false;\n");
sb.append("\t\t}\n");
sb.append("\t\tif (!").append(attributeField.getDeclaringClass().getName()).append('#').append(attribute.getName()).append("_$$_subtypes.contains(c)) {\n");
sb.append("\t\t\tthrow new IllegalArgumentException(");
sb.append("\"Allowed subtypes for attribute '").append(attribute.getName()).append("' are [").append(subtypeArray).append("] but got an instance of: \"");
sb.append(".concat(c.getName())");
sb.append(");\n");
sb.append("\t\t}\n");
if (strictCascadingCheck) {
sb.append("\t\tif ($0 != $1 && !isNew && ").append(attributeField.getDeclaringClass().getName()).append('#').append(attribute.getName()).append("_$$_parentRequiringUpdateSubtypes.contains(c) && !((").append(DirtyTracker.class.getName()).append(") $1).$$_hasParent()) {\n");
sb.append("\t\t\tthrow new IllegalArgumentException(");
sb.append("\"Setting instances of type [\" + c.getName() + \"] on attribute '").append(attribute.getName()).append("' is not allowed until they are assigned to an attribute that cascades the type! ");
sb.append("If you want this attribute to cascade, annotate it with @UpdatableMapping(cascade = { UPDATE }). You can also turn off strict cascading checks by setting ConfigurationProperties.UPDATER_STRICT_CASCADING_CHECK to false\"");
sb.append(");\n");
sb.append("\t\t}\n");
sb.append("\t\tif ($0 != $1 && isNew && ").append(attributeField.getDeclaringClass().getName()).append('#').append(attribute.getName()).append("_$$_parentRequiringCreateSubtypes.contains(c) && !((").append(DirtyTracker.class.getName()).append(") $1).$$_hasParent()) {\n");
sb.append("\t\t\tthrow new IllegalArgumentException(");
sb.append("\"Setting instances of type [\" + c.getName() + \"] on attribute '").append(attribute.getName()).append("' is not allowed until they are assigned to an attribute that cascades the type! ");
sb.append("If you want this attribute to cascade, annotate it with @UpdatableMapping(cascade = { PERSIST }). You can also turn off strict cascading checks by setting ConfigurationProperties.UPDATER_STRICT_CASCADING_CHECK to false\"");
sb.append(");\n");
sb.append("\t\t}\n");
}
sb.append("\t}\n");
}
}
}
if (attribute != null && attribute.getDirtyStateIndex() != -1) {
int mutableStateIndex = attribute.getDirtyStateIndex();
// Unset previous object parent
if (attribute.isCollection()) {
sb.append("\tif ($0.").append(fieldName).append(" != null && $0.").append(fieldName).append(" != $1) {\n");
if (attribute instanceof MapAttribute<?, ?, ?>) {
sb.append("\t\tif ($0.").append(fieldName).append(" instanceof ").append(RecordingMap.class.getName()).append(") {\n");
sb.append("\t\t\t((").append(RecordingMap.class.getName()).append(") $0.").append(fieldName).append(").$$_unsetParent();\n");
sb.append("\t\t}\n");
} else {
sb.append("\t\tif ($0.").append(fieldName).append(" instanceof ").append(RecordingCollection.class.getName()).append(") {\n");
sb.append("\t\t\t((").append(RecordingCollection.class.getName()).append(") $0.").append(fieldName).append(").$$_unsetParent();\n");
sb.append("\t\t}\n");
}
sb.append("\t}\n");
} else if (attribute.isSubview()) {
if (attribute.isUpdatableOnly() && !attribute.isCorrelated()) {
sb.append("\tif ($0.").append(fieldName).append(" != $1 && $0.").append(fieldName).append(" instanceof ").append(MutableStateTrackable.class.getName()).append(") {\n");
sb.append("\t\t\t((").append(MutableStateTrackable.class.getName()).append(") $0.").append(fieldName).append(").$$_removeReadOnlyParent($0, ").append(mutableStateIndex).append(");\n");
sb.append("\t} else if ($0.").append(fieldName).append(" != $1 && $0.").append(fieldName).append(" instanceof ").append(BasicDirtyTracker.class.getName()).append(") {\n");
sb.append("\t\t\t((").append(BasicDirtyTracker.class.getName()).append(") $0.").append(fieldName).append(").$$_unsetParent();\n");
} else {
sb.append("\tif ($0.").append(fieldName).append(" != $1 && $0.").append(fieldName).append(" instanceof ").append(BasicDirtyTracker.class.getName()).append(") {\n");
sb.append("\t\t((").append(BasicDirtyTracker.class.getName()).append(") $0.").append(fieldName).append(").$$_unsetParent();\n");
}
sb.append("\t}\n");
}
if (mutableStateField != null) {
// this.mutableState[mutableStateIndex] = $1
sb.append("\t$0.").append(mutableStateField.getName()).append("[").append(mutableStateIndex).append("] = ");
renderValueForArray(sb, attributeField.getType(), "$1");
}
if (dirtyChecking) {
// this.dirty = true
sb.append("\t$0.$$_markDirty(").append(mutableStateIndex).append(");\n");
// Set new objects parent
if (attribute.isCollection() || attribute.isSubview()) {
sb.append("\tif ($0.$$_initialized && $1 != null && $0.").append(fieldName).append(" != $1) {\n");
if (attribute.isCollection()) {
if (attribute instanceof MapAttribute<?, ?, ?>) {
sb.append("\t\tif ($1 instanceof ").append(RecordingMap.class.getName()).append(") {\n");
sb.append("\t\t\t((").append(RecordingMap.class.getName()).append(") $1).$$_setParent($0, ").append(mutableStateIndex).append(");\n");
sb.append("\t\t}\n");
} else {
sb.append("\t\tif ($1 instanceof ").append(RecordingCollection.class.getName()).append(") {\n");
sb.append("\t\t\t((").append(RecordingCollection.class.getName()).append(") $1).$$_setParent($0, ").append(mutableStateIndex).append(");\n");
sb.append("\t\t}\n");
}
} else if (attribute.isSubview()) {
// Correlated attributes are treated special, we don't consider correlated attributes read-only parents
if (attribute.isUpdatableOnly() && !attribute.isCorrelated()) {
sb.append("\t\tif ($1 instanceof ").append(MutableStateTrackable.class.getName()).append(") {\n");
sb.append("\t\t\t((").append(MutableStateTrackable.class.getName()).append(") $1).$$_addReadOnlyParent($0, ").append(mutableStateIndex).append(");\n");
sb.append("\t\t} else if ($1 instanceof ").append(BasicDirtyTracker.class.getName()).append(") {\n");
sb.append("\t\t\t((").append(BasicDirtyTracker.class.getName()).append(") $1).$$_setParent($0, ").append(mutableStateIndex).append(");\n");
} else {
sb.append("\t\tif ($1 instanceof ").append(BasicDirtyTracker.class.getName()).append(") {\n");
sb.append("\t\t\t((").append(BasicDirtyTracker.class.getName()).append(") $1).$$_setParent($0, ").append(mutableStateIndex).append(");\n");
}
sb.append("\t\t}\n");
}
sb.append("\t}\n");
}
}
}
if (!invalidSetter) {
sb.append("\t$0.").append(fieldName).append(" = $1;\n");
}
sb.append('}');
CtMethod method = CtMethod.make(minfo, cc);
method.setBody(sb.toString());
cc.addMethod(method);
return method;
}
use of com.blazebit.persistence.view.metamodel.SingularAttribute in project blaze-persistence by Blazebit.
the class Mappers method forViewConvertToViewAttributeMapping.
public static <S, T> Mapper<S, T> forViewConvertToViewAttributeMapping(EntityViewManagerImpl evm, ViewType<S> sourceViewType, ViewType<T> targetViewType, String mappedBy, Mapper<S, T> additionalMapper) {
List<Mapper<S, T>> mappers = new ArrayList<>();
AttributeAccessor entityAccessor = Accessors.forEntityMapping(evm, sourceViewType.getEntityClass(), ((MappingAttribute<?, ?>) sourceViewType.getIdAttribute()).getMapping());
ExpressionFactory ef = evm.getCriteriaBuilderFactory().getService(ExpressionFactory.class);
for (MethodAttribute<?, ?> attribute : targetViewType.getAttributes()) {
if (attribute.isUpdatable() && attribute instanceof MappingAttribute<?, ?> && attribute instanceof SingularAttribute<?, ?>) {
if (mappedBy.equals(((MappingAttribute) attribute).getMapping())) {
ViewType<?> attributeType = (ViewType<?>) ((SingularAttribute<?, ?>) attribute).getType();
Type<?> attributeViewIdType = ((SingularAttribute<?, ?>) attributeType.getIdAttribute()).getType();
EntityTupleizer entityTupleizer = null;
ObjectBuilder<?> idViewBuilder = null;
if (attributeViewIdType instanceof ManagedViewType<?>) {
entityTupleizer = new DefaultEntityTupleizer(evm, (ManagedViewType<?>) attributeViewIdType);
idViewBuilder = (ObjectBuilder<Object>) evm.getTemplate(new MacroConfigurationExpressionFactory(ef, ef.getDefaultMacroConfiguration()), (ManagedViewTypeImplementor<?>) attributeViewIdType, null, null, new MutableViewJpqlMacro(), null, new MutableEmbeddingViewJpqlMacro(), 0).createObjectBuilder(null, null, null, 0, false, false);
}
mappers.add(new ReferenceViewAttributeMapper<S, T>(evm, entityAccessor, attributeType.getJavaType(), entityTupleizer, Accessors.forMutableViewAttribute(evm, attribute), idViewBuilder));
}
}
}
if (mappers.isEmpty()) {
return additionalMapper;
}
if (additionalMapper != null) {
mappers.add(additionalMapper);
}
return new CompositeMapper<>(mappers.toArray(new Mapper[mappers.size()]));
}
Aggregations