From c48d02ad183ac67b5021d3e178da7c18cb3fa82c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Timo=20Westk=C3=A4mper?= Date: Sat, 24 Oct 2009 16:52:24 +0000 Subject: [PATCH] improvements to PEntityMap and PEntityList --- .../com/mysema/query/domain/RelationTest.java | 9 ++++ .../java/com/mysema/query/alias/Alias.java | 12 ++--- .../query/alias/AliasAwarePathFactory.java | 16 +++--- .../com/mysema/query/alias/PathFactory.java | 8 +-- .../PropertyAccessInvocationHandler.java | 8 +-- .../mysema/query/alias/SimplePathFactory.java | 38 ++++++------- .../query/codegen/EntitySerializer.java | 54 ++++++++++++------- .../query/codegen/SupertypeSerializer.java | 12 ++--- .../java/com/mysema/query/types/Visitor.java | 4 +- .../com/mysema/query/types/VisitorBase.java | 4 +- .../query/types/path/PComponentList.java | 13 +---- .../query/types/path/PComponentMap.java | 7 ++- .../com/mysema/query/types/path/PEntity.java | 44 +++++++-------- .../mysema/query/types/path/PEntityList.java | 36 +++++++------ .../mysema/query/types/path/PEntityMap.java | 42 ++++++++------- .../mysema/query/types/path/PathInits.java | 2 + .../java/com/mysema/query/hql/HQLQuery.java | 18 +++---- .../com/mysema/query/hql/HQLQueryBase.java | 28 +++++----- .../mysema/query/jdoql/testdomain/QStore.java | 10 ++-- 19 files changed, 197 insertions(+), 168 deletions(-) diff --git a/querydsl-apt/src/test/java/com/mysema/query/domain/RelationTest.java b/querydsl-apt/src/test/java/com/mysema/query/domain/RelationTest.java index 25115f8a9..d7a213d10 100644 --- a/querydsl-apt/src/test/java/com/mysema/query/domain/RelationTest.java +++ b/querydsl-apt/src/test/java/com/mysema/query/domain/RelationTest.java @@ -1,5 +1,7 @@ package com.mysema.query.domain; +import static org.junit.Assert.*; + import java.util.Collection; import java.util.List; import java.util.Map; @@ -115,5 +117,12 @@ public class RelationTest extends AbstractTest{ match(PEntityMap.class, "map8"); match(PComponentMap.class, "map9"); } + + @Test + public void listUsage(){ + String expected = "relationType.list.get(0).set"; + assertEquals(expected, QRelationType.relationType.list.get(0).set.toString()); + assertEquals(expected, QRelationType.relationType.list(0).set.toString()); + } } diff --git a/querydsl-core/src/main/java/com/mysema/query/alias/Alias.java b/querydsl-core/src/main/java/com/mysema/query/alias/Alias.java index 0712650a5..3e440f374 100644 --- a/querydsl-core/src/main/java/com/mysema/query/alias/Alias.java +++ b/querydsl-core/src/main/java/com/mysema/query/alias/Alias.java @@ -18,12 +18,12 @@ import com.mysema.query.types.path.PBoolean; import com.mysema.query.types.path.PBooleanArray; import com.mysema.query.types.path.PComparable; import com.mysema.query.types.path.PComparableArray; +import com.mysema.query.types.path.PComponentList; +import com.mysema.query.types.path.PComponentMap; import com.mysema.query.types.path.PDate; import com.mysema.query.types.path.PDateTime; import com.mysema.query.types.path.PEntity; import com.mysema.query.types.path.PEntityCollection; -import com.mysema.query.types.path.PEntityList; -import com.mysema.query.types.path.PEntityMap; import com.mysema.query.types.path.PNumber; import com.mysema.query.types.path.PSimple; import com.mysema.query.types.path.PString; @@ -151,12 +151,12 @@ public final class Alias { return pathFactory.createTime(arg); } - public static PEntityMap $(Map args) { - return pathFactory.createEntityMap(args); + public static PComponentMap $(Map args) { + return pathFactory.createMap(args); } - public static PEntityList $(List args) { - return pathFactory.createEntityList(args); + public static PComponentList $(List args) { + return pathFactory.createList(args); } public static PEntity $(D arg) { diff --git a/querydsl-core/src/main/java/com/mysema/query/alias/AliasAwarePathFactory.java b/querydsl-core/src/main/java/com/mysema/query/alias/AliasAwarePathFactory.java index 6d914a2c7..efd5cae15 100644 --- a/querydsl-core/src/main/java/com/mysema/query/alias/AliasAwarePathFactory.java +++ b/querydsl-core/src/main/java/com/mysema/query/alias/AliasAwarePathFactory.java @@ -14,12 +14,12 @@ import com.mysema.query.types.path.PBoolean; import com.mysema.query.types.path.PBooleanArray; import com.mysema.query.types.path.PComparable; import com.mysema.query.types.path.PComparableArray; +import com.mysema.query.types.path.PComponentList; +import com.mysema.query.types.path.PComponentMap; import com.mysema.query.types.path.PDate; import com.mysema.query.types.path.PDateTime; import com.mysema.query.types.path.PEntity; import com.mysema.query.types.path.PEntityCollection; -import com.mysema.query.types.path.PEntityList; -import com.mysema.query.types.path.PEntityMap; import com.mysema.query.types.path.PNumber; import com.mysema.query.types.path.PString; import com.mysema.query.types.path.PStringArray; @@ -105,14 +105,14 @@ class AliasAwarePathFactory implements PathFactory { return rv != null ? rv : factory.createEntityCollection(arg); } - public PEntityList createEntityList(List arg) { - PEntityList rv = aliasFactory.> getCurrentAndReset(); - return rv != null ? rv : factory.createEntityList(arg); + public PComponentList createList(List arg) { + PComponentList rv = aliasFactory.> getCurrentAndReset(); + return rv != null ? rv : factory.createList(arg); } - public PEntityMap createEntityMap(Map arg) { - PEntityMap rv = aliasFactory.> getCurrentAndReset(); - return rv != null ? rv : factory.createEntityMap(arg); + public PComponentMap createMap(Map arg) { + PComponentMap rv = aliasFactory.> getCurrentAndReset(); + return rv != null ? rv : factory.createMap(arg); } public > PNumber createNumber(D arg) { diff --git a/querydsl-core/src/main/java/com/mysema/query/alias/PathFactory.java b/querydsl-core/src/main/java/com/mysema/query/alias/PathFactory.java index f27e15cce..dba5cd03d 100644 --- a/querydsl-core/src/main/java/com/mysema/query/alias/PathFactory.java +++ b/querydsl-core/src/main/java/com/mysema/query/alias/PathFactory.java @@ -14,12 +14,12 @@ import com.mysema.query.types.path.PBoolean; import com.mysema.query.types.path.PBooleanArray; import com.mysema.query.types.path.PComparable; import com.mysema.query.types.path.PComparableArray; +import com.mysema.query.types.path.PComponentList; +import com.mysema.query.types.path.PComponentMap; import com.mysema.query.types.path.PDate; import com.mysema.query.types.path.PDateTime; import com.mysema.query.types.path.PEntity; import com.mysema.query.types.path.PEntityCollection; -import com.mysema.query.types.path.PEntityList; -import com.mysema.query.types.path.PEntityMap; import com.mysema.query.types.path.PNumber; import com.mysema.query.types.path.PString; import com.mysema.query.types.path.PStringArray; @@ -54,9 +54,9 @@ interface PathFactory { PEntity createEntity(D arg); - PEntityList createEntityList(List arg); + PComponentList createList(List arg); - PEntityMap createEntityMap(Map arg); + PComponentMap createMap(Map arg); PEntityCollection createEntityCollection(Collection arg); diff --git a/querydsl-core/src/main/java/com/mysema/query/alias/PropertyAccessInvocationHandler.java b/querydsl-core/src/main/java/com/mysema/query/alias/PropertyAccessInvocationHandler.java index d79c89266..f84bf610b 100644 --- a/querydsl-core/src/main/java/com/mysema/query/alias/PropertyAccessInvocationHandler.java +++ b/querydsl-core/src/main/java/com/mysema/query/alias/PropertyAccessInvocationHandler.java @@ -33,12 +33,12 @@ import com.mysema.query.types.expr.EMap; import com.mysema.query.types.expr.Expr; import com.mysema.query.types.path.PBoolean; import com.mysema.query.types.path.PComparable; +import com.mysema.query.types.path.PComponentList; +import com.mysema.query.types.path.PComponentMap; import com.mysema.query.types.path.PDate; import com.mysema.query.types.path.PDateTime; import com.mysema.query.types.path.PEntity; import com.mysema.query.types.path.PEntityCollection; -import com.mysema.query.types.path.PEntityList; -import com.mysema.query.types.path.PEntityMap; import com.mysema.query.types.path.PList; import com.mysema.query.types.path.PMap; import com.mysema.query.types.path.PNumber; @@ -274,7 +274,7 @@ class PropertyAccessInvocationHandler implements MethodInterceptor { } else if (List.class.isAssignableFrom(type)) { Class elementType = getTypeParameter(genericType, 0); - path = new PEntityList(elementType, elementType.getSimpleName(), pm); + path = new PComponentList(elementType, pm); rv = (T) aliasFactory.createAliasForProp(type, parent, path); } else if (Set.class.isAssignableFrom(type)) { @@ -290,7 +290,7 @@ class PropertyAccessInvocationHandler implements MethodInterceptor { } else if (Map.class.isAssignableFrom(type)) { Class keyType = getTypeParameter(genericType, 0); Class valueType = getTypeParameter(genericType, 1); - path = new PEntityMap(keyType, valueType, valueType.getSimpleName(), pm); + path = new PComponentMap(keyType, valueType, pm); rv = (T) aliasFactory.createAliasForProp(type, parent, path); } else if (Enum.class.isAssignableFrom(type)) { diff --git a/querydsl-core/src/main/java/com/mysema/query/alias/SimplePathFactory.java b/querydsl-core/src/main/java/com/mysema/query/alias/SimplePathFactory.java index c390c026f..16297bb99 100644 --- a/querydsl-core/src/main/java/com/mysema/query/alias/SimplePathFactory.java +++ b/querydsl-core/src/main/java/com/mysema/query/alias/SimplePathFactory.java @@ -20,11 +20,12 @@ import com.mysema.query.types.path.PBoolean; import com.mysema.query.types.path.PBooleanArray; import com.mysema.query.types.path.PComparable; import com.mysema.query.types.path.PComparableArray; +import com.mysema.query.types.path.PComponentList; +import com.mysema.query.types.path.PComponentMap; import com.mysema.query.types.path.PDate; import com.mysema.query.types.path.PDateTime; import com.mysema.query.types.path.PEntity; import com.mysema.query.types.path.PEntityCollection; -import com.mysema.query.types.path.PEntityList; import com.mysema.query.types.path.PEntityMap; import com.mysema.query.types.path.PNumber; import com.mysema.query.types.path.PString; @@ -87,30 +88,30 @@ class SimplePathFactory implements PathFactory { } }); - private final Map, PEntityList> elToPath = new PathFactory, PEntityList>( - new Transformer, PEntityList>() { + private final Map, PComponentList> elToPath = new PathFactory, PComponentList>( + new Transformer, PComponentList>() { @SuppressWarnings("unchecked") - public PEntityList transform(List arg) { + public PComponentList transform(List arg) { if (!arg.isEmpty()) { Class cl = arg.get(0).getClass(); - return new PEntityList(cl, cl.getSimpleName(), md()); + return new PComponentList(cl, md()); } else { - return new PEntityList(Object.class, "Object", md()); + return new PComponentList(Object.class, md()); } } }); - private final Map, PEntityMap> emToPath = new PathFactory, PEntityMap>( - new Transformer, PEntityMap>() { + private final Map, PComponentMap> emToPath = new PathFactory, PComponentMap>( + new Transformer, PComponentMap>() { @SuppressWarnings("unchecked") - public PEntityMap transform(Map arg) { + public PComponentMap transform(Map arg) { if (!arg.isEmpty()) { Map.Entry entry = arg.entrySet().iterator().next(); Class keyType = entry.getKey().getClass(); Class valueType = entry.getValue().getClass(); - return new PEntityMap(keyType, valueType, valueType.getSimpleName(), md()); + return new PComponentMap(keyType, valueType, md()); } else { - return new PEntityMap(Object.class, Object.class, "Object", md()); + return new PComponentMap(Object.class, Object.class, md()); } } @@ -224,13 +225,8 @@ class SimplePathFactory implements PathFactory { } @SuppressWarnings("unchecked") - public PEntityList createEntityList(List arg) { - return (PEntityList) elToPath.get(arg); - } - - @SuppressWarnings("unchecked") - public PEntityMap createEntityMap(Map arg) { - return (PEntityMap) emToPath.get(arg); + public PComponentMap createMap(Map arg) { + return (PComponentMap) emToPath.get(arg); } @SuppressWarnings("unchecked") @@ -256,4 +252,10 @@ class SimplePathFactory implements PathFactory { return PathMetadata.forVariable("v" + String.valueOf(++counter)); } + @SuppressWarnings("unchecked") + @Override + public PComponentList createList(List arg) { + return (PComponentList) elToPath.get(arg); + } + } diff --git a/querydsl-core/src/main/java/com/mysema/query/codegen/EntitySerializer.java b/querydsl-core/src/main/java/com/mysema/query/codegen/EntitySerializer.java index 825ac93a4..de14b9fe5 100644 --- a/querydsl-core/src/main/java/com/mysema/query/codegen/EntitySerializer.java +++ b/querydsl-core/src/main/java/com/mysema/query/codegen/EntitySerializer.java @@ -7,6 +7,8 @@ package com.mysema.query.codegen; import java.io.IOException; import java.io.Writer; +import java.util.ArrayList; +import java.util.List; import net.jcip.annotations.Immutable; @@ -133,7 +135,7 @@ public class EntitySerializer implements Serializer{ } protected void collectionOfEntity(PropertyModel field, Writer writer) throws IOException { - serialize(field, "PEntityCollection<" + field.getGenericTypeName()+">", writer, "createEntityCollection", field.getTypeName()+".class", "\"" + field.getSimpleTypeName()+"\""); + serialize(field, "PEntityCollection<" + field.getGenericTypeName()+">", writer, "createEntityCollection", field.getTypeName()+".class"); } protected void collectionOfSimple(PropertyModel field, Writer writer) throws IOException { @@ -167,7 +169,7 @@ public class EntitySerializer implements Serializer{ builder.append(");\n"); builder.append(" }\n\n"); - // 3 + // 3 builder.append(" public " + queryType + "(PathMetadata metadata) {\n"); if (hasEntityFields){ builder.append(" this(metadata, metadata.isRoot() ? __inits : PathInits.DEFAULT);\n"); @@ -286,13 +288,22 @@ public class EntitySerializer implements Serializer{ protected void introInits(StringBuilder builder, BeanModel model) { if (!model.getEntityProperties().isEmpty()){ - builder.append(" private static final PathInits __inits = new PathInits(\"*\""); + List inits = new ArrayList(); for (PropertyModel property : model.getEntityProperties()){ for (String init : property.getInits()){ - builder.append(", \"" + property.getEscapedName() + "." + init + "\""); + inits.add(property.getEscapedName() + "." + init); } - } - builder.append(");\n\n"); + } + if (!inits.isEmpty()){ + builder.append(" private static final PathInits __inits = new PathInits(\"*\""); + for (String init : inits){ + builder.append(", \"" + init + "\""); + } + builder.append(");\n\n"); + }else{ + builder.append(" private static final PathInits __inits = PathInits.DIRECT;\n\n"); + } + } } @@ -348,7 +359,9 @@ public class EntitySerializer implements Serializer{ } protected void listOfEntity(PropertyModel field, Writer writer) throws IOException { - serialize(field, "PEntityList<" + field.getGenericTypeName()+ ">", writer, "createEntityList", field.getTypeName()+".class", "\"" + field.getSimpleTypeName()+"\""); + serialize(field, "PEntityList<" + field.getGenericTypeName()+ "," + field.getQueryTypeName() + ">", writer, "createEntityList", + field.getTypeName()+".class", + field.getQueryTypeName() +".class"); } protected void listOfEntityAccessor(PropertyModel field, Writer writer) throws IOException { @@ -357,10 +370,10 @@ public class EntitySerializer implements Serializer{ StringBuilder builder = new StringBuilder(); builder.append(" public " + queryType + " " + escapedName + "(int index) {\n"); - builder.append(" return new " + queryType + "(PathMetadata.forListAccess(" + escapedName+", index));\n"); + builder.append(" return " + escapedName + ".get(index);\n"); builder.append(" }\n\n"); builder.append(" public " + queryType + " " + escapedName + "(com.mysema.query.types.expr.Expr index) {\n"); - builder.append(" return new " + queryType + "(PathMetadata.forListAccess(" + escapedName+", index));\n"); + builder.append(" return " + escapedName + ".get(index);\n"); builder.append(" }\n\n"); writer.append(builder.toString()); } @@ -371,10 +384,10 @@ public class EntitySerializer implements Serializer{ StringBuilder builder = new StringBuilder(); builder.append(" public PSimple<" + valueType + "> " + escapedName + "(int index) {\n"); - builder.append(" return new PSimple<" + valueType + ">("+valueType+".class, PathMetadata.forListAccess(" + escapedName+", index));\n"); + builder.append(" return " + escapedName + ".get(index);\n"); builder.append(" }\n\n"); builder.append(" public PSimple<" + valueType + "> " + escapedName + "(com.mysema.query.types.expr.Expr index) {\n"); - builder.append(" return new PSimple<" + valueType + ">("+valueType+".class, PathMetadata.forListAccess(" + escapedName+", index));\n"); + builder.append(" return " + escapedName + ".get(index);\n"); builder.append(" }\n\n"); writer.append(builder.toString()); @@ -387,12 +400,15 @@ public class EntitySerializer implements Serializer{ protected void mapOfEntity(PropertyModel field, Writer writer) throws IOException{ final String keyType = field.getParameterName(0); final String valueType = field.getParameterName(1); - final String simpleName = field.getSimpleTypeName(); +// final String simpleName = field.getSimpleTypeName(); final String genericKey = field.getGenericParameterName(0); final String genericValue = field.getGenericParameterName(1); - serialize(field, "PEntityMap<"+genericKey+","+genericValue+">", - writer, "createEntityMap", keyType+".class", valueType+".class", "\""+simpleName+"\""); + serialize(field, "PEntityMap<"+genericKey+","+genericValue+","+field.getQueryTypeName()+">", + writer, "createEntityMap", + keyType+".class", + valueType+".class", + field.getQueryTypeName()+".class"); } @@ -404,10 +420,10 @@ public class EntitySerializer implements Serializer{ StringBuilder builder = new StringBuilder(); builder.append(" public " + queryType + " " + escapedName + "(" + keyType+ " key) {\n"); - builder.append(" return new " + queryType + "(PathMetadata.forMapAccess(" + escapedName+", key));\n"); + builder.append(" return " + escapedName + ".get(key);\n"); builder.append(" }\n\n"); builder.append(" public " + queryType + " " + escapedName + "(com.mysema.query.types.expr.Expr<"+genericKey+"> key) {\n"); - builder.append(" return new " + queryType + "(PathMetadata.forMapAccess(" + escapedName+", key));\n"); + builder.append(" return " + escapedName + ".get(key);\n"); builder.append(" }\n\n"); writer.append(builder.toString()); @@ -427,17 +443,17 @@ public class EntitySerializer implements Serializer{ protected void mapOfSimpleAccessor(PropertyModel field, Writer writer) throws IOException { final String escapedName = field.getEscapedName(); // final String keyType = field.getParameterName(0); - final String valueType = field.getParameterName(1); +// final String valueType = field.getParameterName(1); final String genericKey = field.getGenericParameterName(0); final String genericValue = field.getGenericParameterName(1); StringBuilder builder = new StringBuilder(); builder.append(" public PSimple<" + genericValue + "> " + escapedName + "(" + genericKey + " key) {\n"); - builder.append(" return new PSimple<" + genericValue + ">("+valueType+".class, PathMetadata.forMapAccess(" + escapedName+", key));\n"); + builder.append(" return " + escapedName + ".get(key);\n"); builder.append(" }\n\n"); builder.append(" public PSimple<" + genericValue + "> " + escapedName + "(com.mysema.query.types.expr.Expr<"+genericKey+"> key) {\n"); - builder.append(" return new PSimple<" + valueType + ">("+valueType+".class, PathMetadata.forMapAccess(" + escapedName+", key));\n"); + builder.append(" return " + escapedName + ".get(key);\n"); builder.append(" }\n\n"); writer.append(builder.toString()); diff --git a/querydsl-core/src/main/java/com/mysema/query/codegen/SupertypeSerializer.java b/querydsl-core/src/main/java/com/mysema/query/codegen/SupertypeSerializer.java index 6d2451b10..4e44c475e 100644 --- a/querydsl-core/src/main/java/com/mysema/query/codegen/SupertypeSerializer.java +++ b/querydsl-core/src/main/java/com/mysema/query/codegen/SupertypeSerializer.java @@ -30,17 +30,15 @@ public class SupertypeSerializer extends EntitySerializer{ builder.append(" super(entity.getType(), entity.getEntityName(), entity.getMetadata());\n"); builder.append(" }\n\n"); - builder.append(" public "+queryType+"(Class type, @NotEmpty String entityName, PathMetadata metadata) {\n"); - builder.append(" super(type, entityName, metadata);\n"); - }else{ builder.append(" public "+queryType+"(Class type, @NotEmpty String entityName, PathMetadata metadata, PathInits inits) {\n"); builder.append(" super(type, entityName, metadata);\n"); if (!model.getEntityProperties().isEmpty()){ initEntityFields(builder, model); - } + } + builder.append(" }\n"); } - builder.append(" }\n"); + writer.append(builder.toString()); } @@ -56,7 +54,9 @@ public class SupertypeSerializer extends EntitySerializer{ @Override protected void introImports(StringBuilder builder, BeanModel model) { - builder.append("import com.mysema.query.util.*;\n"); + if (!model.getEntityProperties().isEmpty()){ + builder.append("import com.mysema.query.util.*;\n"); + } builder.append("import com.mysema.query.types.path.*;\n\n"); } diff --git a/querydsl-core/src/main/java/com/mysema/query/types/Visitor.java b/querydsl-core/src/main/java/com/mysema/query/types/Visitor.java index 8daae62f3..e8a739220 100644 --- a/querydsl-core/src/main/java/com/mysema/query/types/Visitor.java +++ b/querydsl-core/src/main/java/com/mysema/query/types/Visitor.java @@ -145,9 +145,9 @@ public interface Visitor { void visit(PEntityCollection expr); - void visit(PEntityList expr); + void visit(PEntityList expr); - void visit(PEntityMap expr); + void visit(PEntityMap expr); void visit(PList expr); diff --git a/querydsl-core/src/main/java/com/mysema/query/types/VisitorBase.java b/querydsl-core/src/main/java/com/mysema/query/types/VisitorBase.java index 162ade449..cf2655d29 100644 --- a/querydsl-core/src/main/java/com/mysema/query/types/VisitorBase.java +++ b/querydsl-core/src/main/java/com/mysema/query/types/VisitorBase.java @@ -254,12 +254,12 @@ public abstract class VisitorBase> implemen } @Override - public void visit(PEntityList expr) { + public void visit(PEntityList expr) { visit((PList) expr); } @Override - public void visit(PEntityMap expr) { + public void visit(PEntityMap expr) { visit((PMap) expr); } diff --git a/querydsl-core/src/main/java/com/mysema/query/types/path/PComponentList.java b/querydsl-core/src/main/java/com/mysema/query/types/path/PComponentList.java index 90ed5eb44..3ebd4ae54 100644 --- a/querydsl-core/src/main/java/com/mysema/query/types/path/PComponentList.java +++ b/querydsl-core/src/main/java/com/mysema/query/types/path/PComponentList.java @@ -6,7 +6,6 @@ package com.mysema.query.types.path; import com.mysema.query.types.expr.Expr; -import com.mysema.query.util.NotEmpty; /** * PComponentList represents component list paths @@ -22,21 +21,13 @@ public class PComponentList extends PComponentCollection implements PList< super(type, metadata); } - public PComponentList(Class type, @NotEmpty String var) { - super(type, PathMetadata.forVariable(var)); - } - - public PComponentList(Class type, Path path, @NotEmpty String property) { - super(type, PathMetadata.forProperty(path, property)); - } - @Override - public Expr get(Expr index) { + public PSimple get(Expr index) { return new PSimple(type, PathMetadata.forListAccess(this, index)); } @Override - public Expr get(int index) { + public PSimple get(int index) { return new PSimple(type, PathMetadata.forListAccess(this, index)); } } \ No newline at end of file diff --git a/querydsl-core/src/main/java/com/mysema/query/types/path/PComponentMap.java b/querydsl-core/src/main/java/com/mysema/query/types/path/PComponentMap.java index 59efcfd5c..34ca742b9 100644 --- a/querydsl-core/src/main/java/com/mysema/query/types/path/PComponentMap.java +++ b/querydsl-core/src/main/java/com/mysema/query/types/path/PComponentMap.java @@ -37,8 +37,7 @@ public class PComponentMap extends EMapBase implements PMap { private volatile EBoolean isnull, isnotnull; @SuppressWarnings("unchecked") - public PComponentMap(Class keyType, Class valueType, - PathMetadata metadata) { + public PComponentMap(Class keyType, Class valueType, PathMetadata metadata) { super((Class)Map.class); this.keyType = (Class)keyType; this.valueType = (Class)valueType; @@ -67,12 +66,12 @@ public class PComponentMap extends EMapBase implements PMap { } @Override - public Expr get(Expr key) { + public PSimple get(Expr key) { return new PSimple(valueType, PathMetadata.forMapAccess(this, key)); } @Override - public Expr get(K key) { + public PSimple get(K key) { return new PSimple(valueType, PathMetadata.forMapAccess(this, key)); } diff --git a/querydsl-core/src/main/java/com/mysema/query/types/path/PEntity.java b/querydsl-core/src/main/java/com/mysema/query/types/path/PEntity.java index 8dd518eef..080f6d858 100644 --- a/querydsl-core/src/main/java/com/mysema/query/types/path/PEntity.java +++ b/querydsl-core/src/main/java/com/mysema/query/types/path/PEntity.java @@ -42,32 +42,28 @@ public class PEntity extends EEntity implements Path { return new PBoolean(this, propertyName); } - protected > PComparable createComparable(@NotEmpty String propertyName, Class type) { - return new PComparable(type, this, propertyName); + protected > PComparable createComparable(@NotEmpty String property, Class type) { + return new PComparable(type, this, property); } - protected > PDate createDate(@NotEmpty String propertyName, Class type) { - return new PDate(type, PathMetadata.forProperty(this, propertyName)); + protected > PDate createDate(@NotEmpty String property, Class type) { + return new PDate(type, PathMetadata.forProperty(this, property)); } - protected > PDateTime createDateTime(@NotEmpty String propertyName, Class type) { - return new PDateTime(type, this, propertyName); + protected > PDateTime createDateTime(@NotEmpty String property, Class type) { + return new PDateTime(type, this, property); } - protected PEntity createEntity(@NotEmpty String property, @NotEmpty String entityName, Class type) { - return new PEntity(type, entityName, PathMetadata.forProperty(this, property)); + protected PEntityCollection createEntityCollection(@NotEmpty String property, Class type) { + return new PEntityCollection(type, type.getSimpleName(), this, property); } - protected PEntityCollection createEntityCollection(@NotEmpty String property, Class type, @NotEmpty String entityName) { - return new PEntityCollection(type, entityName, this, property); + protected > PEntityList createEntityList(@NotEmpty String property, Class type, Class queryType) { + return new PEntityList(type, queryType, PathMetadata.forProperty(this, property)); } - protected PEntityList createEntityList(@NotEmpty String property, Class type, @NotEmpty String entityName) { - return new PEntityList(type, entityName, this, property); - } - - protected PEntityMap createEntityMap(@NotEmpty String property, Class key, Class value, @NotEmpty String entityName) { - return new PEntityMap(key, value, entityName, this, property); + protected > PEntityMap createEntityMap(@NotEmpty String property, Class key, Class value, Class queryType) { + return new PEntityMap(key, value, queryType, PathMetadata.forProperty(this, property)); } protected > PNumber createNumber(@NotEmpty String property, Class type) { @@ -79,24 +75,24 @@ public class PEntity extends EEntity implements Path { return new PSimple((Class)type, this, path); } - protected PComponentCollection createSimpleCollection(@NotEmpty String path, Class type) { - return new PComponentCollection(type, this,path); + protected PComponentCollection createSimpleCollection(@NotEmpty String property, Class type) { + return new PComponentCollection(type, this, property); } - protected PComponentList createSimpleList(@NotEmpty String path, Class type) { - return new PComponentList(type, this, path); + protected PComponentList createSimpleList(@NotEmpty String property, Class type) { + return new PComponentList(type, PathMetadata.forProperty(this, property)); } - protected PComponentMap createSimpleMap(@NotEmpty String path, Class key, Class value) { - return new PComponentMap(key, value, this, path); + protected PComponentMap createSimpleMap(@NotEmpty String property, Class key, Class value) { + return new PComponentMap(key, value, this, property); } protected PString createString(@NotEmpty String property) { return new PString(this, property); } - protected > PTime createTime(@NotEmpty String propertyName, Class type) { - return new PTime(type, this, propertyName); + protected > PTime createTime(@NotEmpty String property, Class type) { + return new PTime(type, this, property); } @SuppressWarnings("unchecked") diff --git a/querydsl-core/src/main/java/com/mysema/query/types/path/PEntityList.java b/querydsl-core/src/main/java/com/mysema/query/types/path/PEntityList.java index 496817990..7f785f531 100644 --- a/querydsl-core/src/main/java/com/mysema/query/types/path/PEntityList.java +++ b/querydsl-core/src/main/java/com/mysema/query/types/path/PEntityList.java @@ -6,7 +6,6 @@ package com.mysema.query.types.path; import com.mysema.query.types.expr.Expr; -import com.mysema.query.util.NotEmpty; /** * PEntityList represents entity list paths @@ -16,29 +15,34 @@ import com.mysema.query.util.NotEmpty; * @param component type */ @SuppressWarnings("serial") -public class PEntityList extends PEntityCollection implements PList { +public class PEntityList> extends PEntityCollection implements PList { - public PEntityList(Class elementType, @NotEmpty String entityName, PathMetadata metadata) { - super(elementType, entityName, metadata); - } - - public PEntityList(Class elementType, @NotEmpty String entityName, @NotEmpty String var) { - super(elementType, entityName, PathMetadata.forVariable(var)); - } - - public PEntityList(Class elementType, @NotEmpty String entityName, Path parent, @NotEmpty String property) { - super(elementType, entityName, PathMetadata.forProperty(parent, property)); + private final Class queryType; + + public PEntityList(Class elementType, Class queryType, PathMetadata metadata) { + super(elementType, elementType.getSimpleName(), metadata); + this.queryType = queryType; } @Override - public PEntity get(Expr index) { - return new PEntity(elementType, entityName, PathMetadata.forListAccess(this, index)); + public E get(Expr index) { + PathMetadata md = PathMetadata.forListAccess(this, index); + try { + return queryType.getConstructor(PathMetadata.class).newInstance(md); + } catch (Exception e) { + throw new RuntimeException(e.getMessage(), e); + } } @Override - public PEntity get(int index) { + public E get(int index) { // TODO : cache - return new PEntity(elementType, entityName, PathMetadata.forListAccess(this, index)); + PathMetadata md = PathMetadata.forListAccess(this, index); + try { + return queryType.getConstructor(PathMetadata.class).newInstance(md); + } catch (Exception e) { + throw new RuntimeException(e.getMessage(), e); + } } } \ No newline at end of file diff --git a/querydsl-core/src/main/java/com/mysema/query/types/path/PEntityMap.java b/querydsl-core/src/main/java/com/mysema/query/types/path/PEntityMap.java index d56d86169..fef83ad31 100644 --- a/querydsl-core/src/main/java/com/mysema/query/types/path/PEntityMap.java +++ b/querydsl-core/src/main/java/com/mysema/query/types/path/PEntityMap.java @@ -24,40 +24,34 @@ import com.mysema.query.util.NotEmpty; * @param value type */ @SuppressWarnings("serial") -public class PEntityMap extends EMapBase implements PMap { +public class PEntityMap> extends EMapBase implements PMap { private volatile EBoolean isnull, isnotnull; private final Class keyType; - private final PathMetadata metadata; - private final Class valueType; + private final Class queryType; + + private final PathMetadata metadata; + @NotEmpty private final String entityName; private final Path root; @SuppressWarnings("unchecked") - public PEntityMap(Class keyType, Class valueType, @NotEmpty String entityName, - PathMetadata metadata) { + public PEntityMap(Class keyType, Class valueType, Class queryType, PathMetadata metadata) { super((Class)Map.class); this.keyType = (Class) keyType; this.valueType = (Class) valueType; - this.entityName = entityName; + this.queryType = queryType; + this.entityName = valueType.getSimpleName(); this.metadata = metadata; this.root = metadata.getRoot() != null ? metadata.getRoot() : this; } - public PEntityMap(Class keyType, Class valueType, @NotEmpty String entityName, @NotEmpty String var) { - this(keyType, valueType, entityName, PathMetadata.forVariable(var)); - } - - public PEntityMap(Class keyType, Class valueType, @NotEmpty String entityName, Path parent, @NotEmpty String var) { - this(keyType, valueType, entityName, PathMetadata.forProperty(parent, var)); - } - @Override public void accept(Visitor v) { v.visit(this); @@ -71,15 +65,23 @@ public class PEntityMap extends EMapBase implements PMap { } @Override - public PEntity get(Expr key) { - return new PEntity(valueType, entityName, PathMetadata.forMapAccess( - this, key)); + public E get(Expr key) { + PathMetadata md = PathMetadata.forMapAccess(this, key); + try { + return queryType.getConstructor(PathMetadata.class).newInstance(md); + } catch (Exception e) { + throw new RuntimeException(e.getMessage(), e); + } } @Override - public PEntity get(K key) { - return new PEntity(valueType, entityName, PathMetadata.forMapAccess( - this, key)); + public E get(K key) { + PathMetadata md = PathMetadata.forMapAccess(this, key); + try { + return queryType.getConstructor(PathMetadata.class).newInstance(md); + } catch (Exception e) { + throw new RuntimeException(e.getMessage(), e); + } } @Override diff --git a/querydsl-core/src/main/java/com/mysema/query/types/path/PathInits.java b/querydsl-core/src/main/java/com/mysema/query/types/path/PathInits.java index 4e5016829..b3f56d1b3 100644 --- a/querydsl-core/src/main/java/com/mysema/query/types/path/PathInits.java +++ b/querydsl-core/src/main/java/com/mysema/query/types/path/PathInits.java @@ -19,6 +19,8 @@ public class PathInits { public static final PathInits DEFAULT = new PathInits(); + public static final PathInits DIRECT = new PathInits("*"); + private final Map propertyToInits = new HashMap(); private boolean initAllProps = false; diff --git a/querydsl-hql/src/main/java/com/mysema/query/hql/HQLQuery.java b/querydsl-hql/src/main/java/com/mysema/query/hql/HQLQuery.java index 99619bcae..1f3c1ead9 100644 --- a/querydsl-hql/src/main/java/com/mysema/query/hql/HQLQuery.java +++ b/querydsl-hql/src/main/java/com/mysema/query/hql/HQLQuery.java @@ -8,8 +8,8 @@ package com.mysema.query.hql; import com.mysema.query.Projectable; import com.mysema.query.Query; import com.mysema.query.types.expr.EBoolean; +import com.mysema.query.types.path.PCollection; import com.mysema.query.types.path.PEntity; -import com.mysema.query.types.path.PEntityCollection; import com.mysema.query.types.path.PEntityMap; /** @@ -24,27 +24,27 @@ public interface HQLQuery extends Query, Projectable {

HQLQuery innerJoin(PEntity

target, PEntity

alias); -

HQLQuery innerJoin(PEntityCollection

target, PEntity

alias); +

HQLQuery innerJoin(PCollection

target, PEntity

alias); -

HQLQuery innerJoin(PEntityMap target, PEntity

alias); +

HQLQuery innerJoin(PEntityMap target, PEntity

alias);

HQLQuery join(PEntity

target, PEntity

alias); -

HQLQuery join(PEntityCollection

target, PEntity

alias); +

HQLQuery join(PCollection

target, PEntity

alias); -

HQLQuery join(PEntityMap target, PEntity

alias); +

HQLQuery join(PEntityMap target, PEntity

alias);

HQLQuery leftJoin(PEntity

target, PEntity

alias); -

HQLQuery leftJoin(PEntityCollection

target, PEntity

alias); +

HQLQuery leftJoin(PCollection

target, PEntity

alias); -

HQLQuery leftJoin(PEntityMap target, PEntity

alias); +

HQLQuery leftJoin(PEntityMap target, PEntity

alias);

HQLQuery fullJoin(PEntity

target, PEntity

alias); -

HQLQuery fullJoin(PEntityCollection

target, PEntity

alias); +

HQLQuery fullJoin(PCollection

target, PEntity

alias); -

HQLQuery fullJoin(PEntityMap target, PEntity

alias); +

HQLQuery fullJoin(PEntityMap target, PEntity

alias); HQLQuery with(EBoolean condition); diff --git a/querydsl-hql/src/main/java/com/mysema/query/hql/HQLQueryBase.java b/querydsl-hql/src/main/java/com/mysema/query/hql/HQLQueryBase.java index 1510b5d82..48628218d 100644 --- a/querydsl-hql/src/main/java/com/mysema/query/hql/HQLQueryBase.java +++ b/querydsl-hql/src/main/java/com/mysema/query/hql/HQLQueryBase.java @@ -16,12 +16,11 @@ import com.mysema.query.JoinType; import com.mysema.query.QueryMetadata; import com.mysema.query.support.QueryBaseWithProjection; import com.mysema.query.types.expr.EBoolean; -import com.mysema.query.types.expr.EEntity; import com.mysema.query.types.expr.Expr; import com.mysema.query.types.operation.OSimple; import com.mysema.query.types.operation.Ops; +import com.mysema.query.types.path.PCollection; import com.mysema.query.types.path.PEntity; -import com.mysema.query.types.path.PEntityCollection; import com.mysema.query.types.path.PEntityMap; import com.mysema.query.types.path.PSimple; import com.mysema.query.types.path.PathMetadata; @@ -57,12 +56,17 @@ public abstract class HQLQueryBase> extend } @SuppressWarnings("unchecked") - private Expr createAlias(EEntity target, PEntity alias){ + private Expr createAlias(PEntity target, PEntity alias){ return OSimple.create((Class)alias.getType(), Ops.ALIAS, target, alias); } @SuppressWarnings("unchecked") - private Expr createAlias(PEntityMap target, PEntity alias){ + private Expr createAlias(PCollection target, PEntity alias){ + return OSimple.create((Class)alias.getType(), Ops.ALIAS, target.asExpr(), alias); + } + + @SuppressWarnings("unchecked") + private Expr createAlias(PEntityMap target, PEntity alias){ return OSimple.create((Class)alias.getType(), Ops.ALIAS, target, alias); } @@ -98,12 +102,12 @@ public abstract class HQLQueryBase> extend return _this; } - public

SubType fullJoin(PEntityCollection

target, PEntity

alias) { + public

SubType fullJoin(PCollection

target, PEntity

alias) { getMetadata().addJoin(JoinType.FULLJOIN, createAlias(target, alias)); return _this; } - public

SubType fullJoin(PEntityMap target, PEntity

alias) { + public

SubType fullJoin(PEntityMap target, PEntity

alias) { getMetadata().addJoin(JoinType.FULLJOIN, createAlias(target, alias)); return _this; } @@ -117,12 +121,12 @@ public abstract class HQLQueryBase> extend return _this; } - public

SubType innerJoin(PEntityCollection

target, PEntity

alias) { + public

SubType innerJoin(PCollection

target, PEntity

alias) { getMetadata().addJoin(JoinType.INNERJOIN, createAlias(target, alias)); return _this; } - public

SubType innerJoin(PEntityMap target, PEntity

alias) { + public

SubType innerJoin(PEntityMap target, PEntity

alias) { getMetadata().addJoin(JoinType.INNERJOIN, createAlias(target, alias)); return _this; } @@ -132,12 +136,12 @@ public abstract class HQLQueryBase> extend return _this; } - public

SubType join(PEntityCollection

target, PEntity

alias) { + public

SubType join(PCollection

target, PEntity

alias) { getMetadata().addJoin(JoinType.JOIN, createAlias(target, alias)); return _this; } - public

SubType join(PEntityMap target, PEntity

alias) { + public

SubType join(PEntityMap target, PEntity

alias) { getMetadata().addJoin(JoinType.JOIN, createAlias(target, alias)); return _this; } @@ -147,12 +151,12 @@ public abstract class HQLQueryBase> extend return _this; } - public

SubType leftJoin(PEntityCollection

target, PEntity

alias) { + public

SubType leftJoin(PCollection

target, PEntity

alias) { getMetadata().addJoin(JoinType.LEFTJOIN, createAlias(target, alias)); return _this; } - public

SubType leftJoin(PEntityMap target, PEntity

alias) { + public

SubType leftJoin(PEntityMap target, PEntity

alias) { getMetadata().addJoin(JoinType.LEFTJOIN, createAlias(target, alias)); return _this; } diff --git a/querydsl-jdoql/src/test/java/com/mysema/query/jdoql/testdomain/QStore.java b/querydsl-jdoql/src/test/java/com/mysema/query/jdoql/testdomain/QStore.java index c483cf920..5452562e8 100644 --- a/querydsl-jdoql/src/test/java/com/mysema/query/jdoql/testdomain/QStore.java +++ b/querydsl-jdoql/src/test/java/com/mysema/query/jdoql/testdomain/QStore.java @@ -5,7 +5,11 @@ */ package com.mysema.query.jdoql.testdomain; -import com.mysema.query.types.path.*; +import com.mysema.query.types.path.PEntity; +import com.mysema.query.types.path.PEntityCollection; +import com.mysema.query.types.path.PEntityMap; +import com.mysema.query.types.path.PString; +import com.mysema.query.types.path.PathMetadata; /** * QStore is a Querydsl query type for Store @@ -18,9 +22,9 @@ public class QStore extends PEntity{ public final PString name = createString("name"); - public final PEntityMap productsByName = createEntityMap("productsByName",String.class,Product.class,"Product"); + public final PEntityMap productsByName = createEntityMap("productsByName",String.class,Product.class,QProduct.class); - public final PEntityCollection products = createEntityCollection("products",Product.class, "Product"); + public final PEntityCollection products = createEntityCollection("products",Product.class); public QProduct productsByName(String key) { return new QProduct(PathMetadata.forMapAccess(productsByName,key));