mirror of
https://github.com/querydsl/querydsl.git
synced 2026-06-13 21:01:01 +08:00
added supertype serialization
This commit is contained in:
parent
2de6902679
commit
6458747971
@ -71,6 +71,11 @@ public class Processor {
|
||||
ClassModel model = element.accept(entityVisitor, null);
|
||||
superTypes.put(model.getName(), model);
|
||||
}
|
||||
|
||||
// serialize supertypes
|
||||
if (!superTypes.isEmpty()){
|
||||
serialize(Serializers.SUPERTYPE, superTypes);
|
||||
}
|
||||
}
|
||||
|
||||
// ENTITIES
|
||||
@ -87,7 +92,7 @@ public class Processor {
|
||||
}
|
||||
// serialize entity types
|
||||
if (!entityTypes.isEmpty()) {
|
||||
serialize(Serializers.DOMAIN, entityTypes);
|
||||
serialize(Serializers.ENTITY, entityTypes);
|
||||
}
|
||||
|
||||
// EMBEDDABLES (optional)
|
||||
|
||||
@ -8,7 +8,6 @@ package com.mysema.query.collections.impl;
|
||||
import com.mysema.query.DefaultQueryMetadata;
|
||||
import com.mysema.query.QueryMetadata;
|
||||
import com.mysema.query.collections.ColQuery;
|
||||
import com.mysema.query.collections.ColQueryTemplates;
|
||||
|
||||
|
||||
/**
|
||||
@ -18,16 +17,6 @@ import com.mysema.query.collections.ColQueryTemplates;
|
||||
*
|
||||
*/
|
||||
public class ColQueryImpl extends AbstractColQuery<ColQueryImpl> implements ColQuery{
|
||||
|
||||
@Deprecated
|
||||
public ColQueryImpl(ColQueryTemplates templates) {
|
||||
super(new DefaultQueryMetadata(), new EvaluatorFactory(templates));
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
public ColQueryImpl(QueryMetadata metadata, ColQueryTemplates templates) {
|
||||
super(metadata, new EvaluatorFactory(templates));
|
||||
}
|
||||
|
||||
public ColQueryImpl(EvaluatorFactory evaluatorFactory) {
|
||||
super(new DefaultQueryMetadata(), evaluatorFactory);
|
||||
|
||||
@ -9,6 +9,7 @@ import com.mysema.query.annotations.Entity;
|
||||
|
||||
@Entity
|
||||
public class Animal {
|
||||
|
||||
protected boolean alive;
|
||||
protected java.util.Date birthdate = new java.util.Date();
|
||||
protected int bodyWeight, weight, toes;
|
||||
|
||||
@ -0,0 +1,25 @@
|
||||
package com.mysema.query.collections.domain;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import java.util.Collections;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import com.mysema.query.collections.ColQuery;
|
||||
import com.mysema.query.collections.MiniApi;
|
||||
import com.mysema.query.collections.impl.ColQueryImpl;
|
||||
|
||||
public class TypeCastTest {
|
||||
|
||||
@Test
|
||||
public void cast(){
|
||||
QAnimal animal = QAnimal.animal;
|
||||
QCat cat = new QCat(animal.getMetadata());
|
||||
System.out.println(cat);
|
||||
|
||||
ColQuery query = MiniApi.from(animal, Collections.<Animal>emptyList()).from(cat, Collections.<Cat>emptyList());
|
||||
assertEquals(1, ((ColQueryImpl)query).getMetadata().getJoins().size());
|
||||
}
|
||||
|
||||
}
|
||||
@ -149,6 +149,9 @@ public class EntitySerializer extends AbstractSerializer{
|
||||
builder.append(" }\n\n");
|
||||
}
|
||||
|
||||
builder.append(" public " + queryType + "(PEntity<?> entity) {\n");
|
||||
builder.append(" this(entity.getMetadata());\n");
|
||||
builder.append(" }\n\n");
|
||||
builder.append(" public " + queryType + "(PathMetadata<?> metadata) {\n");
|
||||
builder.append(" super("+ localName + ".class, \"" + simpleName + "\", metadata);\n");
|
||||
builder.append(" }\n\n");
|
||||
@ -233,7 +236,6 @@ public class EntitySerializer extends AbstractSerializer{
|
||||
final String simpleName = model.getSimpleName();
|
||||
final String queryType = model.getPrefix() + simpleName;
|
||||
final String localName = model.getLocalName();
|
||||
final String unscapSimpleName = model.getUncapSimpleName();
|
||||
|
||||
StringBuilder builder = new StringBuilder();
|
||||
// package
|
||||
@ -253,14 +255,21 @@ public class EntitySerializer extends AbstractSerializer{
|
||||
builder.append("@SuppressWarnings(\"all\")\n");
|
||||
builder.append("public class " + queryType + " extends PEntity<" + localName + "> {\n\n");
|
||||
|
||||
if (!embeddable){
|
||||
// default variable
|
||||
builder.append(" public static final " + queryType + " " + unscapSimpleName + " = new " + queryType + "(\"" + unscapSimpleName + "\");\n\n");
|
||||
}
|
||||
defaultInstance(model, builder);
|
||||
|
||||
writer.append(builder.toString());
|
||||
}
|
||||
|
||||
protected void defaultInstance(ClassModel model, StringBuilder builder) {
|
||||
final String simpleName = model.getSimpleName();
|
||||
final String unscapSimpleName = model.getUncapSimpleName();
|
||||
final String queryType = model.getPrefix() + simpleName;
|
||||
if (!embeddable){
|
||||
// default variable
|
||||
builder.append(" public static final " + queryType + " " + unscapSimpleName + " = new " + queryType + "(\"" + unscapSimpleName + "\");\n\n");
|
||||
}
|
||||
}
|
||||
|
||||
protected void numericField(FieldModel field, Writer writer) throws IOException {
|
||||
serialize(field, "PNumber<" + field.getTypeName() + ">", writer, "_number", field.getTypeName() +".class");
|
||||
}
|
||||
|
||||
@ -16,7 +16,9 @@ public final class Serializers {
|
||||
|
||||
private Serializers(){}
|
||||
|
||||
public static final Serializer DOMAIN = new EntitySerializer(false);
|
||||
public static final Serializer ENTITY = new EntitySerializer(false);
|
||||
|
||||
public static final Serializer SUPERTYPE = new SupertypeSerializer(false);
|
||||
|
||||
public static final Serializer EMBEDDABLE = new EntitySerializer(true);
|
||||
|
||||
|
||||
@ -0,0 +1,37 @@
|
||||
package com.mysema.query.codegen;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.Writer;
|
||||
|
||||
/**
|
||||
* @author tiwe
|
||||
*
|
||||
*/
|
||||
public class SupertypeSerializer extends EntitySerializer{
|
||||
|
||||
public SupertypeSerializer(boolean embeddable) {
|
||||
super(embeddable);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void defaultInstance(ClassModel model, StringBuilder builder) {
|
||||
// no default instance
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void constructors(ClassModel model, Writer writer) throws IOException {
|
||||
final String simpleName = model.getSimpleName();
|
||||
final String queryType = model.getPrefix() + simpleName;
|
||||
final String localName = model.getLocalName();
|
||||
|
||||
StringBuilder builder = new StringBuilder();
|
||||
builder.append(" public " + queryType + "(PEntity<? extends "+localName+"> entity) {\n");
|
||||
builder.append(" this(entity.getMetadata());\n");
|
||||
builder.append(" }\n\n");
|
||||
builder.append(" public " + queryType + "(PathMetadata<?> metadata) {\n");
|
||||
builder.append(" super("+ localName + ".class, \"" + simpleName + "\", metadata);\n");
|
||||
builder.append(" }\n\n");
|
||||
writer.append(builder.toString());
|
||||
}
|
||||
|
||||
}
|
||||
@ -50,7 +50,7 @@ public class SerializerTest {
|
||||
*/
|
||||
@Test
|
||||
public void testDomainTypesAsOuterClasses() throws Exception {
|
||||
Serializers.DOMAIN.serialize(type, writer);
|
||||
Serializers.ENTITY.serialize(type, writer);
|
||||
// System.out.println(writer);
|
||||
}
|
||||
|
||||
|
||||
@ -19,8 +19,14 @@ import com.mysema.query.hql.HQLTemplates;
|
||||
*/
|
||||
public class HibernateQuery extends AbstractHibernateQuery<HibernateQuery> implements HQLQuery{
|
||||
|
||||
public HibernateQuery(Session session, HQLTemplates patterns) {
|
||||
super(new DefaultQueryMetadata(), session, patterns);
|
||||
private static final HQLTemplates DEFAULT_TEMPLATES = new HQLTemplates();
|
||||
|
||||
public HibernateQuery(Session session, HQLTemplates templates) {
|
||||
super(new DefaultQueryMetadata(), session, templates);
|
||||
}
|
||||
|
||||
public HibernateQuery(Session session) {
|
||||
super(new DefaultQueryMetadata(), session, DEFAULT_TEMPLATES);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -19,8 +19,13 @@ import com.mysema.query.hql.HQLTemplates;
|
||||
*/
|
||||
public class JPAQuery extends AbstractJPAQuery<JPAQuery> implements HQLQuery{
|
||||
|
||||
private static final HQLTemplates DEFAULT_TEMPLATES = new HQLTemplates();
|
||||
|
||||
public JPAQuery(EntityManager em, HQLTemplates patterns) {
|
||||
super(new DefaultQueryMetadata(), em, patterns);
|
||||
}
|
||||
|
||||
public JPAQuery(EntityManager em) {
|
||||
super(new DefaultQueryMetadata(), em, DEFAULT_TEMPLATES);
|
||||
}
|
||||
}
|
||||
|
||||
@ -41,12 +41,6 @@ public abstract class AbstractJDOQLQuery<SubType extends AbstractJDOQLQuery<SubT
|
||||
@Nullable
|
||||
private final PersistenceManager pm;
|
||||
|
||||
public AbstractJDOQLQuery(QueryMetadata md, JDOQLTemplates templates){
|
||||
super(md);
|
||||
this.templates = templates;
|
||||
this.pm = null;
|
||||
}
|
||||
|
||||
public AbstractJDOQLQuery(QueryMetadata md, PersistenceManager pm, JDOQLTemplates templates) {
|
||||
super(md);
|
||||
this.templates = templates;
|
||||
|
||||
@ -19,12 +19,13 @@ import com.mysema.query.DefaultQueryMetadata;
|
||||
*/
|
||||
public class JDOQLQueryImpl extends AbstractJDOQLQuery<JDOQLQueryImpl> implements JDOQLQuery{
|
||||
|
||||
public JDOQLQueryImpl(JDOQLTemplates templates) {
|
||||
super(new DefaultQueryMetadata(), templates);
|
||||
}
|
||||
|
||||
private static final JDOQLTemplates DEFAULT_TEMPLATES = new JDOQLTemplates();
|
||||
|
||||
public JDOQLQueryImpl(PersistenceManager pm, JDOQLTemplates templates) {
|
||||
super(new DefaultQueryMetadata(), pm, templates);
|
||||
}
|
||||
|
||||
public JDOQLQueryImpl(PersistenceManager pm) {
|
||||
super(new DefaultQueryMetadata(), pm, DEFAULT_TEMPLATES);
|
||||
}
|
||||
}
|
||||
|
||||
@ -39,7 +39,7 @@ public class MetaDataExporter {
|
||||
|
||||
private final String schemaPattern, tableNamePattern;
|
||||
|
||||
private static final Serializer serializer = Serializers.DOMAIN;
|
||||
private static final Serializer serializer = Serializers.ENTITY;
|
||||
|
||||
public MetaDataExporter(String namePrefix, String packageName, String schemaPattern, String tableNamePattern, String targetFolder){
|
||||
this.namePrefix = namePrefix;
|
||||
|
||||
Loading…
Reference in New Issue
Block a user