Hibernate CrudRespository—根据成员列表变量的参数属性筛选对象的查询方法名称
我使用的是Hibernate CrudRespository—根据成员列表变量的参数属性筛选对象的查询方法名称,hibernate,spring-data-jpa,jpa-2.0,Hibernate,Spring Data Jpa,Jpa 2.0,我使用的是org.springframework.data.repository.crudepository。我有一个存储库和一些看起来像这样的嵌套实体 @Repository public interface CountryRepository extends CrudRepository<Country, Integer> { //Want to put something here like //Country country = findCountryWher
org.springframework.data.repository.crudepository
。我有一个存储库和一些看起来像这样的嵌套实体
@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
}
@存储库
公共接口CountryRepository扩展了Crudepository{
//想在这里放点什么吗
//Country Country=FindCountrywhere somecompanyhasid(字符串id)
}
@实体
公营国家{
@OneToMany(级联=级联类型.ALL)
私人上市公司;
//获取、设置和标识
}
@实体
公营公司{
专用字符串uniqueInternationalOrgNr;
//获取、设置和标识
}
假设我有一些
uniqueInternationalOrgNr
,我想找出这家公司属于哪个国家。我想在crudepository
界面中输入一个方法名,这样Spring就会自动为我实现这个方法。这个方法的名称是什么?乍一看,通过IntelliJ自动完成功能,我似乎无法根据列表
参数的属性进行查询。根据Spring,您可以使用嵌套属性。
@存储库
公共接口CountryRepository扩展了Crudepository{
国家/地区由公司UniqueInternationalOrgnr(字符串id)查找;
}
当您有多对多关系时,您可能需要将返回类型更改为
列表
您尝试了什么?你读过JPQL的文档了吗?特别是关于连接的部分?JPQL在这里直接相关吗?我试图弄清楚Spring是否会识别某个方法名,然后为我生成一个方法实现。不确定在最初的帖子中是否足够清晰。我已进行了编辑。对于除最简单的查询(如findByName)以外的所有查询,您最好为方法选择所需的最佳名称,并使用Query
注释该方法。当您知道JPQL(您应该知道),这不仅会产生更好、更易于维护的代码,而且会更快,因为您不必找出神奇的方法名称。
@Repository
public interface CountryRepository extends CrudRepository<Country, Integer> {
Country findByCompaniesUniqueInternationalOrgNr(String id);
}