简体   繁体   English

spring data jpa 通过示例嵌套集合属性查找所有内容

[英]spring data jpa find all by example nested collection property

I have two objects.我有两个对象。 The company that can have multiple nested addresses.可以有多个嵌套地址的公司。

@Entity
@Data
@Table(name = "company")
public class Company {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "id")
    private Long id;

    @Column(name = "name")
    private String name;

    @Column(name = "phone")
    private String phone;

    @OneToMany(mappedBy = "company", cascade = CascadeType.ALL, orphanRemoval = true, fetch = FetchType.LAZY)
    private List<Address> addresses;
}

Address class looks like this:地址类如下所示:

@Data
@Entity
@Table(name = "address")
@ToString(exclude = "company")
public class Address {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "id")
    private Long id;

    @Column(name = "postal_code")
    private String postalCode;

    @Column(name = "city")
    private String city;

    @Column(name = "street")
    private String street;

    @JsonIgnore
    @ManyToOne(fetch = FetchType.EAGER)
    @JoinColumn(name = "company_id")
    private Company company;
}

I want somehow if it's possible, make a dynamic query that searches through the nested collection property.如果可能的话,我想以某种方式进行动态查询,搜索嵌套的集合属性。 I made a search method which uses example matcher but the result is wrong.我做了一个使用示例匹配器的搜索方法,但结果是错误的。 Every time I got everything from DB, not only company with address postal code that I'm looking for.每次我从 DB 获得所有信息时,不仅仅是我正在寻找的具有地址邮政编码的公司。

My search method looks like this:我的搜索方法如下所示:

@PostMapping("/search")
    public List<Company> search(@RequestBody final Company company){
        return companyRepository.findAll(Example.of(company,
                ExampleMatcher.matchingAny()
                        .withIgnoreNullValues()
                        .withIgnorePaths("id")
                        .withStringMatcher(ExampleMatcher.StringMatcher.STARTING)));
    }

In my database, I have two objects and this is the result of the search:在我的数据库中,我有两个对象,这是搜索的结果: 在此处输入图片说明

As you can see I received everything from DB instead of the only first company which address postal code starts with 1.如您所见,我从 DB 收到了所有内容,而不是唯一一家地址邮政编码以 1 开头的公司。

Hi you can use Specification<T>嗨,您可以使用Specification<T>

https://spring.io/blog/2011/04/26/advanced-spring-data-jpa-specifications-and-querydsl/ https://spring.io/blog/2011/04/26/advanced-spring-data-jpa-specifications-and-querydsl/

For this you need to extend from interface JpaSpecificationExecutor:为此,您需要从接口 JpaSpecificationExecutor 扩展:

public interface UserRepository extends JpaRepository<User> ,JpaSpecificationExecutor<User>{
}

And you also need to implement your custom Specification<T>而且您还需要实现您的自定义Specification<T>

And then you can use repository.findAll(your impleneted Specification);然后你可以使用 repository.findAll(your impleneted Specification);

Spring docs :春季文档:

https://docs.spring.io/spring-data/jpa/docs/current/api/org/springframework/data/jpa/repository/JpaSpecificationExecutor.html https://docs.spring.io/spring-data/jpa/docs/current/api/org/springframework/data/jpa/repository/JpaSpecificationExecutor.html

I think this is helpful.我认为这很有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM