Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
beikov committed May 2, 2019
1 parent 3658277 commit 4f6625e
Show file tree
Hide file tree
Showing 12 changed files with 688 additions and 66 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -536,14 +536,14 @@ private String[] getReturningColumns(List<List<Attribute<?, ?>>> attributes) {
sb.append(returningAttribute.get(i).getName()).append('.');
if (returningAttribute.get(i).isCollection()) {
sb.setLength(sb.length() - 1);
joinTable = mainQuery.metamodel.getManagedType(ExtendedManagedType.class, entityType.getJavaType()).getAttribute(sb.toString()).getJoinTable();
joinTable = mainQuery.metamodel.getManagedType(ExtendedManagedType.class, entityType).getAttribute(sb.toString()).getJoinTable();
sb.setLength(0);
}
}
sb.setLength(sb.length() - 1);

if (joinTable == null) {
for (String column : mainQuery.metamodel.getManagedType(ExtendedManagedType.class, entityType.getJavaType()).getAttribute(sb.toString()).getColumnNames()) {
for (String column : mainQuery.metamodel.getManagedType(ExtendedManagedType.class, entityType).getAttribute(sb.toString()).getColumnNames()) {
columns.add(column);
}
} else {
Expand All @@ -567,7 +567,7 @@ protected String[] getReturningColumns() {
List<String> columns = new ArrayList<String>(returningAttributeNames.size());

for (String returningAttributeName : returningAttributeBindingMap.values()) {
for (String column : mainQuery.metamodel.getManagedType(ExtendedManagedType.class, entityType.getJavaType()).getAttribute(returningAttributeName).getColumnNames()) {
for (String column : mainQuery.metamodel.getManagedType(ExtendedManagedType.class, entityType).getAttribute(returningAttributeName).getColumnNames()) {
columns.add(column);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,12 +119,7 @@ public boolean isBag(EntityType<?> ownerType, String attributeName) {

@Override
public boolean isOrphanRemoval(ManagedType<?> ownerType, String attributeName) {
ExtendedManagedType managedType;
if (ownerType instanceof EntityType<?>) {
managedType = entityMetamodel.getManagedType(ExtendedManagedType.class, ((EntityType) ownerType).getName());
} else {
managedType = entityMetamodel.getManagedType(ExtendedManagedType.class, ownerType.getJavaType());
}
ExtendedManagedType managedType = entityMetamodel.getManagedType(ExtendedManagedType.class, ownerType);
ExtendedAttribute attribute = (ExtendedAttribute) managedType.getAttributes().get(attributeName);
return attribute != null && attribute.isOrphanRemoval();
}
Expand All @@ -136,12 +131,7 @@ public boolean isOrphanRemoval(ManagedType<?> ownerType, String elementCollectio

@Override
public boolean isDeleteCascaded(ManagedType<?> ownerType, String attributeName) {
ExtendedManagedType managedType;
if (ownerType instanceof EntityType<?>) {
managedType = entityMetamodel.getManagedType(ExtendedManagedType.class, ((EntityType) ownerType).getName());
} else {
managedType = entityMetamodel.getManagedType(ExtendedManagedType.class, ownerType.getJavaType());
}
ExtendedManagedType managedType = entityMetamodel.getManagedType(ExtendedManagedType.class, ownerType);
ExtendedAttribute attribute = (ExtendedAttribute) managedType.getAttributes().get(attributeName);
return attribute != null && attribute.isDeleteCascaded();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ public void visit(PathExpression expr) {
return;
}

ExtendedManagedType<?> managedType = metamodel.getManagedType(ExtendedManagedType.class, baseNode.getJavaType());
ExtendedManagedType<?> managedType = metamodel.getManagedType(ExtendedManagedType.class, baseNode.getManagedType());
Attribute attr = managedType.getAttribute(pathReference.getField()).getAttribute();

// We constantify collection as a whole to a single element when reaching this point
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ protected boolean collectSplittedOffExpressions(Expression expression) {
return false;
}
String fieldPrefix = field == null ? "" : field + ".";
ExtendedManagedType<?> managedType = metamodel.getManagedType(ExtendedManagedType.class, baseNode.getJavaType());
ExtendedManagedType<?> managedType = metamodel.getManagedType(ExtendedManagedType.class, baseNode.getManagedType());
Map<String, Boolean> orderedAttributes = new TreeMap<>();
EntityType<?> ownerType;
if (baseNode.getParentTreeNode() == null && field == null) {
Expand Down Expand Up @@ -178,7 +178,7 @@ public Boolean visit(PathExpression expr) {
}
}
} else {
ExtendedManagedType<?> managedType = metamodel.getManagedType(ExtendedManagedType.class, baseNode.getJavaType());
ExtendedManagedType<?> managedType = metamodel.getManagedType(ExtendedManagedType.class, baseNode.getManagedType());
attr = managedType.getAttribute(pathReference.getField()).getAttribute();

// This kind of happens when we do an entity select where the entity is split into it's component paths
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ public Boolean visit(PathExpression expr) {
}

// First we check if the target attribute is unique, if it isn't, we don't need to check the join structure
ExtendedManagedType<?> managedType = metamodel.getManagedType(ExtendedManagedType.class, baseNode.getJavaType());
ExtendedManagedType<?> managedType = metamodel.getManagedType(ExtendedManagedType.class, baseNode.getManagedType());
Attribute attr = managedType.getAttribute(field).getAttribute();

if (attr instanceof PluralAttribute<?, ?, ?>) {
Expand Down Expand Up @@ -246,12 +246,7 @@ public Boolean visit(PathExpression expr) {
} else {
// We have to correct the base node for single valued id paths
String associationName = expr.getField().substring(0, dotIndex);
ExtendedManagedType<?> extendedManagedType;
if (baseNode.getManagedType().getJavaType() == null) {
extendedManagedType = metamodel.getManagedType(ExtendedManagedType.class, JpaMetamodelUtils.getTypeName(baseNode.getManagedType()));
} else {
extendedManagedType = metamodel.getManagedType(ExtendedManagedType.class, baseNode.getManagedType().getJavaType());
}
ExtendedManagedType<?> extendedManagedType = metamodel.getManagedType(ExtendedManagedType.class, baseNode.getManagedType());
ExtendedAttribute<?, ?> extendedAttribute = extendedManagedType.getAttribute(associationName);
Attribute<?, ?> attribute = extendedAttribute.getAttribute();
baseNodeKey = new AbstractMap.SimpleEntry<>(baseNode, associationName);
Expand Down Expand Up @@ -333,12 +328,7 @@ public Boolean visit(PathExpression expr) {
// This is relevant for queries like `select e.manyToOne.id, e.manyToOne.name from Entity e order by e.manyToOne.id`
// Normally, the expression `e.manyToOne.id` wouldn't be considered unique, unless there is a unique key with constant equality predicate
// i.e. a predicate like `e.id = 1` that essentially "constantifies" the parent join node
ExtendedManagedType<?> extendedManagedType;
if (baseNode.getManagedType().getJavaType() == null) {
extendedManagedType = metamodel.getManagedType(ExtendedManagedType.class, JpaMetamodelUtils.getTypeName(baseNode.getManagedType()));
} else {
extendedManagedType = metamodel.getManagedType(ExtendedManagedType.class, baseNode.getManagedType().getJavaType());
}
ExtendedManagedType<?> extendedManagedType = metamodel.getManagedType(ExtendedManagedType.class, baseNode.getManagedType());
orderedAttributes = new HashMap<>();
addAttributes(baseNode.getEntityType(), null, "", "", (SingularAttribute<?, ?>) attr, orderedAttributes);
initConstantifiedAttributes(orderedAttributes, constantifiedAttributes);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2253,7 +2253,7 @@ private boolean isSingleValuedAssociationId(JoinResult joinResult, List<PathElem
return false;
}
maybeSingularAssociationName = joinNode.getParentTreeNode().getRelationName();
ExtendedManagedType<?> managedType = metamodel.getManagedType(ExtendedManagedType.class, parent.getJavaType());
ExtendedManagedType<?> managedType = metamodel.getManagedType(ExtendedManagedType.class, parent.getManagedType());
ExtendedAttribute<?, ?> associationAttribute = managedType.getOwnedSingularAttributes().get(maybeSingularAssociationName);
return managedType.getOwnedSingularAttributes().containsKey(maybeSingularAssociationName + "." + maybeSingularAssociationIdExpression)
&& associationAttribute != null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import com.blazebit.persistence.spi.ExtendedManagedType;

import javax.persistence.metamodel.EntityType;
import javax.persistence.metamodel.ManagedType;
import javax.persistence.metamodel.Type;
import java.util.ArrayList;
import java.util.HashSet;
Expand Down Expand Up @@ -401,7 +402,7 @@ private String getNaturalIdAttribute(Expression expression) {
if (identifierOrUniqueKeyEmbeddedPropertyNames.size() == 1) {
// This "fix" only works if we have a single id attribute
String naturalIdAttribute = identifierOrUniqueKeyEmbeddedPropertyNames.get(0);
ExtendedManagedType extendedManagedType = metamodel.getManagedType(ExtendedManagedType.class, pathReference.getType().getJavaType());
ExtendedManagedType extendedManagedType = metamodel.getManagedType(ExtendedManagedType.class, (ManagedType<?>) pathReference.getType());
if (!extendedManagedType.getIdAttribute().getName().equals(naturalIdAttribute)) {
// Now we finally know the natural id attribute name
return naturalIdAttribute;
Expand Down
Loading

0 comments on commit 4f6625e

Please sign in to comment.