@QueryInit in child class fails if @QueryInit is not null in his parent. #336

Closed
lhwaisman opened this Issue Jan 31, 2013 · 4 comments

Comments

Projects
None yet
2 participants
@lhwaisman

The problem exists in method "as" when the child class is initialized from a parent class.Example:

class Parent {
private int x;
@QueryInit("")
private OtherClass z;
}
class Child extends Parent {
@QueryInit("
")
private OtherClassTwo y;
}

When child class is created "QChild c = QParent.parent.as(QChild.class)" his QueryInit fails because the method "as" inject (if INITS of Parent is not null) the INITS of Parent without check if INITS of child class is not null.

public <U extends BeanPath<? extends T>> U as(Class clazz) {
try {
if (!casts.containsKey(clazz)) {
U rv;
if (inits != null) {
rv = clazz.getConstructor(PathMetadata.class, PathInits.class).newInstance(this.getMetadata(), inits);
} else {
rv = clazz.getConstructor(PathMetadata.class).newInstance(this.getMetadata());
}
casts.put(clazz, rv);
return rv;
} else {
return (U) casts.get(clazz);
}
.....

Sorry by my English.
Thanks
Version of QueryDsl : 2.7.3

timowest added a commit that referenced this issue Feb 1, 2013

@timowest

This comment has been minimized.

Show comment
Hide comment
@timowest

timowest Feb 1, 2013

Member

It doesn't fail, if just takes the inits of the left hands side path, that's all.

What kind of change do you suggest?

You need to be aware that there are cases where the left hand side inits are broader than the right hand side, so always picking the left hand side inits is not an option.

How do you use the path.as(QType) functionality?

Member

timowest commented Feb 1, 2013

It doesn't fail, if just takes the inits of the left hands side path, that's all.

What kind of change do you suggest?

You need to be aware that there are cases where the left hand side inits are broader than the right hand side, so always picking the left hand side inits is not an option.

How do you use the path.as(QType) functionality?

@lhwaisman

This comment has been minimized.

Show comment
Hide comment
@lhwaisman

lhwaisman Feb 1, 2013

My suggestion is that you can combine the properties of the parent class to the child class properties. This was the solution I had to implement to solve the problem described. The solution was:

private static final QOrden o = QOrden.orden;

public static BooleanExpression byZona(Zona zona) {
// WARN: INITS harcodeado. Cuando se trata de que clase hija con @QueryInit no
// funciona si su padre tiene @QueryInit (porque no se combinan).
QOrden o2 = new QOrden(o.getMetadata(),
new PathInits("cuota.zona", "cuota.socio", "total.*"));
QRecibo r3 = o2.as(QRecibo.class);
return r3.cuota.zona.eq(zona);
}

Where is the child class Receipt of Order. Both classes have @ QueryInit defined in various properties, as shown in PathInits new ("cuota.zona" "cuota.socio", "total. *")); Being "cuota" Receipt own property and "total" Order (and inherited by Receipt)

@entity
public class Recibo extends Orden {

@QueryInit(value={"zona", "socio"})
@NotNull
@ManyToOne(cascade= CascadeType.MERGE)    
private Cuota cuota;

....
}

@entity
public class Orden {
@Valid
@QueryInit("*")
private Dinero total;

 ....

}

Thanks

My suggestion is that you can combine the properties of the parent class to the child class properties. This was the solution I had to implement to solve the problem described. The solution was:

private static final QOrden o = QOrden.orden;

public static BooleanExpression byZona(Zona zona) {
// WARN: INITS harcodeado. Cuando se trata de que clase hija con @QueryInit no
// funciona si su padre tiene @QueryInit (porque no se combinan).
QOrden o2 = new QOrden(o.getMetadata(),
new PathInits("cuota.zona", "cuota.socio", "total.*"));
QRecibo r3 = o2.as(QRecibo.class);
return r3.cuota.zona.eq(zona);
}

Where is the child class Receipt of Order. Both classes have @ QueryInit defined in various properties, as shown in PathInits new ("cuota.zona" "cuota.socio", "total. *")); Being "cuota" Receipt own property and "total" Order (and inherited by Receipt)

@entity
public class Recibo extends Orden {

@QueryInit(value={"zona", "socio"})
@NotNull
@ManyToOne(cascade= CascadeType.MERGE)    
private Cuota cuota;

....
}

@entity
public class Orden {
@Valid
@QueryInit("*")
private Dinero total;

 ....

}

Thanks

@timowest

This comment has been minimized.

Show comment
Hide comment
@timowest

timowest Feb 2, 2013

Member

My suggestion is that you can combine the properties of the parent class to the child class properties.

I implemented now the following rules in the as method

  • if the original path is a root path then the new path will have the top level inits of the child which are guaranteed to include the parent inits
  • otherwise the parent inits are taken

PathMetadata.isRoot() returns now true for direct variable paths and paths that are delegates to variable paths.

Member

timowest commented Feb 2, 2013

My suggestion is that you can combine the properties of the parent class to the child class properties.

I implemented now the following rules in the as method

  • if the original path is a root path then the new path will have the top level inits of the child which are guaranteed to include the parent inits
  • otherwise the parent inits are taken

PathMetadata.isRoot() returns now true for direct variable paths and paths that are delegates to variable paths.

@timowest

This comment has been minimized.

Show comment
Hide comment
@timowest

timowest Feb 20, 2013

Member

Released in 3.0.0.BETA2

Member

timowest commented Feb 20, 2013

Released in 3.0.0.BETA2

@timowest timowest closed this Feb 20, 2013

@timowest timowest added this to the 3.0.0 milestone Apr 14, 2014

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment