我们可以用复合键属性构建 Spring 数据 JPA 规范吗
Can we build Spring Data JPA specification out of a composite key attribute
我正在使用 Spring 数据 JPA 规范对我的实体进行一般查询。到目前为止它运作良好。现在,当我尝试在通过 embeddedId 注释使用的复合键上使用这些时,问题就发生了。所以在这里我需要使用嵌套的 属性 来查询哪个说对象是否为 Foo 并且我试图在 id 上写一个条件。例如id1。
@Entity
@Data
public class Foo implements Serializable {
private static final long serialVersionUID = 1L;
/** The key. */
@EmbeddedId
private FooKey id;
}
@Embeddable
@Data
public class FooKey implements Serializable {
private static final long serialVersionUID = 1L;
/** The key. */
private String id1;
private String id2;
}
在我试图做的规范中
@Override
public Predicate toPredicate(Root<T> root, CriteriaQuery<?> query, CriteriaBuilder builder) {
// get root type
root.get(property).getJavaType()
但这不适用于本例中的嵌套属性。有什么方法可以为复合键中的属性构建谓词。
相等示例:
@Override
public Predicate toPredicate(Root<Foo> root, CriteriaQuery<?> query, CriteriaBuilder builder) {
builder.equal(root.get("id").get("id1"),"my val");
如果你想使用多个级别,你可以像这样构建一个函数。根据我的经验,2 个级别绰绰有余。但这取决于你。
protected Path<Comparable> getPath(Root<EntityOrModel> root) {
Path<Comparable> path;
if (criteria.getKey().contains(".")) {
String[] split = criteria.getKey().split("\.");
int keyPosition = 0;
path = root.get(split[keyPosition]);
for (String criteriaKeys : split) {
if (keyPosition > 0) {
path = path.get(criteriaKeys);
}
keyPosition++;
}
} else {
path = root.get(criteria.getKey());
}
return path;
}
然后设置
@Override
public Predicate toPredicate(Root<EntityOrModel> root, CriteriaQuery<?> query, CriteriaBuilder builder) {
Path<Comparable> path = getPath(root);
// ...
return builder.equal(path, value)
}
我正在使用 Spring 数据 JPA 规范对我的实体进行一般查询。到目前为止它运作良好。现在,当我尝试在通过 embeddedId 注释使用的复合键上使用这些时,问题就发生了。所以在这里我需要使用嵌套的 属性 来查询哪个说对象是否为 Foo 并且我试图在 id 上写一个条件。例如id1。
@Entity
@Data
public class Foo implements Serializable {
private static final long serialVersionUID = 1L;
/** The key. */
@EmbeddedId
private FooKey id;
}
@Embeddable
@Data
public class FooKey implements Serializable {
private static final long serialVersionUID = 1L;
/** The key. */
private String id1;
private String id2;
}
在我试图做的规范中
@Override
public Predicate toPredicate(Root<T> root, CriteriaQuery<?> query, CriteriaBuilder builder) {
// get root type
root.get(property).getJavaType()
但这不适用于本例中的嵌套属性。有什么方法可以为复合键中的属性构建谓词。
相等示例:
@Override
public Predicate toPredicate(Root<Foo> root, CriteriaQuery<?> query, CriteriaBuilder builder) {
builder.equal(root.get("id").get("id1"),"my val");
如果你想使用多个级别,你可以像这样构建一个函数。根据我的经验,2 个级别绰绰有余。但这取决于你。
protected Path<Comparable> getPath(Root<EntityOrModel> root) {
Path<Comparable> path;
if (criteria.getKey().contains(".")) {
String[] split = criteria.getKey().split("\.");
int keyPosition = 0;
path = root.get(split[keyPosition]);
for (String criteriaKeys : split) {
if (keyPosition > 0) {
path = path.get(criteriaKeys);
}
keyPosition++;
}
} else {
path = root.get(criteria.getKey());
}
return path;
}
然后设置
@Override
public Predicate toPredicate(Root<EntityOrModel> root, CriteriaQuery<?> query, CriteriaBuilder builder) {
Path<Comparable> path = getPath(root);
// ...
return builder.equal(path, value)
}