我试图利用QueryDSL功能来执行一些操作,而我仍然在创建一个包含来自数据库的信息的特定DTO列表,这样我就可以避免以后的杂乱操作。下面的例子是我试图实现的目标的一个愚蠢的表述:
public class Product {
private Quantity total;
//more code here
}
public class Quantity {
//code here
public Quantity multiply(Quantity quantity) {
return Quantity.of(this.value.multiply(quantity.numberValue());
}
//more code here
}
public static Function<JPQLQuery, List<MyDTO>> someMethod() {
return q -> {
//some code here
q.from(...)
//more code here
.list(new QMyDTO(qProduct.total.multiply(qProduct.total).sum()));
};
}问题是,QueryDSL使用一个NumberExpression来提供一些操作,比如求和、乘等,这些操作在BigDecimal这样的类型中工作得很好,但是不能处理自定义类型,比如my对象Quantity。我试着想出一些办法,但我没有取得任何成功,也没有在那里找到任何类似的东西。
有什么措施可以帮助我做到这一点吗?
发布于 2014-11-16 19:23:45
您可以为这种情况创建委托方法。
@QueryDelegate(Quantity.class)
public static Expression<Quantity> multiply(QQuantity quantity, Quantity other){
return Expressions.operation(Quantity.class,
Ops.MULTI, quantity, ConstantImpl.create(other));
}在那之后你就可以快递了
qProduct.total.multiply(qProduct.total)https://stackoverflow.com/questions/26893598
复制相似问题