具有抽象 class 类型定义的谓词之间的 Hibernate 条件构建器
Hibernate criteria builder between predicate with abstract class type definition
我想在定义为以下方法的方法之间使用 Hibernate 的 CriteriaBuilder:
<Y extends Comparable<? super Y>> Predicate between(Expression<? extends Y> v, Y x, Y y)
我有下面的摘要class,重要的方法是toPredicate
。
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.Path;
import javax.persistence.criteria.Predicate;
abstract public class RangeObject<T> {
protected T from;
protected T to;
public RangeObject(T from, T to) {
this.from = from;
this.to = to;
}
public T getFrom() {
return from;
}
public T getTo() {
return to;
}
/**
* Create a predicate for testing whether the second argument is
* between the `from` and `to` values
*
* @param builder Criteria Builder instance
* @param path Attribute path from a bound type
* @return between predicate
*/
public Predicate toPredicate(
CriteriaBuilder builder,
Path<T> path
) {
if (isSingleValue(from, to)) {
return builder.equal(path, from);
} else {
return builder.between(path, from, to);
}
}
/**
* @param from From value
* @param to To value
* @return Whether parameters to this method match
*/
abstract public boolean isSingleValue(T from, T to);
}
它有一个参数Path<T>
。
路径接口定义为 interface Path<X> extends Expression<X>
据我了解应该与 Expression<? extends Y>
.
兼容
我收到以下错误:
no suitable method found for between(javax.persistence.criteria.Path<T>,T,T)
我什至尝试将 toPredicate
方法更改为:
public Predicate toPredicate(
CriteriaBuilder builder,
Expression<? extends T> expression
) {
if (isSingleValue(from, to)) {
return builder.equal(expression, from);
} else {
return builder.between(expression, from, to);
}
}
我仍然得到同样的错误:
no suitable method found for between(javax.persistence.criteria.Expression<capture#1 of ? extends T>,T,T)
如果我对类型进行硬编码,它就可以工作(这是一个工作示例:)
import ...
abstract public class RangeObject {
protected LocalDateTime from;
protected LocalDateTime to;
...
public Predicate toPredicate(
CriteriaBuilder builder,
Path<LocalDateTime> path
) {
if (isSingleValue(from, to)) {
return builder.equal(path, from);
} else {
return builder.between(path, from, to);
}
}
...
}
我做错了什么?
根据方法规范,between
方法中的参数应扩展 Comparable
<Y extends Comparable<? super Y>> Predicate between(Expression<? extends Y> v, Y x, Y y);
如果您在 class 规范中使 T
扩展 Comparable
,它将同时适用于路径和表达式。
public abstract class RangeObject<T extends Comparable<T>> {
protected T from;
protected T to;
.......
}
我想在定义为以下方法的方法之间使用 Hibernate 的 CriteriaBuilder:
<Y extends Comparable<? super Y>> Predicate between(Expression<? extends Y> v, Y x, Y y)
我有下面的摘要class,重要的方法是toPredicate
。
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.Path;
import javax.persistence.criteria.Predicate;
abstract public class RangeObject<T> {
protected T from;
protected T to;
public RangeObject(T from, T to) {
this.from = from;
this.to = to;
}
public T getFrom() {
return from;
}
public T getTo() {
return to;
}
/**
* Create a predicate for testing whether the second argument is
* between the `from` and `to` values
*
* @param builder Criteria Builder instance
* @param path Attribute path from a bound type
* @return between predicate
*/
public Predicate toPredicate(
CriteriaBuilder builder,
Path<T> path
) {
if (isSingleValue(from, to)) {
return builder.equal(path, from);
} else {
return builder.between(path, from, to);
}
}
/**
* @param from From value
* @param to To value
* @return Whether parameters to this method match
*/
abstract public boolean isSingleValue(T from, T to);
}
它有一个参数Path<T>
。
路径接口定义为 interface Path<X> extends Expression<X>
据我了解应该与 Expression<? extends Y>
.
我收到以下错误:
no suitable method found for between(javax.persistence.criteria.Path<T>,T,T)
我什至尝试将 toPredicate
方法更改为:
public Predicate toPredicate(
CriteriaBuilder builder,
Expression<? extends T> expression
) {
if (isSingleValue(from, to)) {
return builder.equal(expression, from);
} else {
return builder.between(expression, from, to);
}
}
我仍然得到同样的错误:
no suitable method found for between(javax.persistence.criteria.Expression<capture#1 of ? extends T>,T,T)
如果我对类型进行硬编码,它就可以工作(这是一个工作示例:)
import ...
abstract public class RangeObject {
protected LocalDateTime from;
protected LocalDateTime to;
...
public Predicate toPredicate(
CriteriaBuilder builder,
Path<LocalDateTime> path
) {
if (isSingleValue(from, to)) {
return builder.equal(path, from);
} else {
return builder.between(path, from, to);
}
}
...
}
我做错了什么?
根据方法规范,between
方法中的参数应扩展 Comparable
<Y extends Comparable<? super Y>> Predicate between(Expression<? extends Y> v, Y x, Y y);
如果您在 class 规范中使 T
扩展 Comparable
,它将同时适用于路径和表达式。
public abstract class RangeObject<T extends Comparable<T>> {
protected T from;
protected T to;
.......
}