我们可以根据复合键属性构建Spring Data JPA规范吗? [英] Can we build Spring Data JPA specification out of a composite key attribute

查看:56
本文介绍了我们可以根据复合键属性构建Spring Data JPA规范吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Spring Data JPA规范对实体进行通用查询.到目前为止,它运行良好.现在,当我尝试在通过EmbeddedId注释使用的复合键上使用这些键时,发生了问题.所以在这里我需要使用一个嵌套的属性来查询它说对象是否是Foo,并且我试图在id上写一个条件.例如id1.

I am using Spring Data JPA specifications for generic queries to my entities. So far it has worked well. Now the problem happened when I try to use these on composite keys used via embeddedId annotation. So here I need to use a nested property to query which say if the object is Foo and I am trying to write a criteria over id. id1 for example.

@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()

但是,这种方法不适用于这种情况下的嵌套属性.有什么方法可以为组合键中的属性建立谓词.

But this doesn't work for nested attributes like in this case. Is there any way I can be able to build predicates for properties in the composite key.

推荐答案

等于示例:

     @Override
     public Predicate toPredicate(Root<Foo> root, CriteriaQuery<?> query, CriteriaBuilder builder) { 
     builder.equal(root.get("id").get("id1"),"my val");

这篇关于我们可以根据复合键属性构建Spring Data JPA规范吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆