CrudRepository-基于成员List变量的参数属性过滤对象的查询方法名称

时间:2018-08-06 13:59:51

标签: hibernate spring-data-jpa jpa-2.0

我正在使用org.springframework.data.repository.CrudRepository。我有一个存储库和一些看起来像这样的嵌套实体。

@Repository
public interface CountryRepository extends CrudRepository<Country, Integer> {
    //Want to put something here like
    //Country country = findCountryWhereSomeCompanyHasId(String id)
}

@Entity
public class Country{
    @OneToMany(cascade=CascadeType.ALL)
    private List<Company> companies;

    //Get, set and id
}


@Entity
public class Company{
    private String uniqueInternationalOrgNr;

    //Get, set and id
}

假设我有一些uniqueInternationalOrgNr,并且我想查找该公司属于哪个国家。我想在CrudRepository接口中输入方法名称,以便Spring自动为我实现该方法。该方法名称是什么?乍一看,通过IntelliJ自动补全功能,似乎无法基于List<>参数的属性进行查询。

1 个答案:

答案 0 :(得分:0)

根据Spring,您可以使用嵌套属性。 Spring reference

@Repository
public interface CountryRepository extends CrudRepository<Country, Integer> {
    Country findByCompaniesUniqueInternationalOrgNr(String id);
}

当您具有多对多关系时,您可能需要将返回类型更改为List<Country>