使用JPA条件在另一个表中使用搜索条件构建搜索查询
我需要根据使用JPA条件在另一个表中使用搜索条件构建搜索查询,jpa,criteria-api,Jpa,Criteria Api,我需要根据country表中给定的国家名(而不是countryId)从StateTable中搜索状态,该表应与使用JPA标准API的likeSQL运算符匹配(countryId是StateTable中的外键,顾名思义) 使用JPQL是乏味的,因为需要为多个搜索条件构建查询。这只是一个演示/说明 国家实体: @Entity public class Country implements Serializable { @Id private Long countryId;
country
表中给定的国家名(而不是countryId
)从StateTable
中搜索状态,该表应与使用JPA标准API的like
SQL运算符匹配(countryId
是StateTable
中的外键,顾名思义)
使用JPQL是乏味的,因为需要为多个搜索条件构建查询。这只是一个演示/说明
国家
实体:
@Entity
public class Country implements Serializable {
@Id
private Long countryId; //<----------------------
@Column(name = "country_name")
private String countryName;
@Column(name = "country_code")
private String countryCode;
@OneToMany(mappedBy = "countryId", fetch = FetchType.LAZY)
private Set<StateTable> stateTableSet;
}
@Entity
public class StateTable implements Serializable {
@Id
private Long stateId;
@Column(name = "state_name")
private String stateName;
@JoinColumn(name = "country_id", referencedColumnName = "country_id")
@ManyToOne(fetch = FetchType.LAZY)
private Country countryId; //<-------------------------------
}
您可以扩展您的条件定义,如(假设状态实体中有一个属性国家/地区-换句话说,假设您在状态表中有一个国家/地区id作为外键):
Join country=root.Join(“country”,JoinType.LEFT);
//并用
add(cb.like(country.get(“countryName”),“%”+countryName+“%”);
这应该可以完成所有工作。您可以扩展您的标准定义,例如(假设状态实体中有一个属性国家/地区-换句话说,假设您在状态表中有一个国家/地区id作为外键):
Join country=root.Join(“country”,JoinType.LEFT);
//并用
add(cb.like(country.get(“countryName”),“%”+countryName+“%”);
这将完成所有工作。您需要执行连接:
Join<StateTable, Country> country = root.join("countryId");
predicates.add(criteriaBuilder.like(country.<String>get("countryName"), "%"+countryName+"%"));
Join country=root.Join(“countryId”);
add(criteriaBuilder.like(country.get(“countryName”),“%”+countryName+“%”);
您需要执行连接:
Join<StateTable, Country> country = root.join("countryId");
predicates.add(criteriaBuilder.like(country.<String>get("countryName"), "%"+countryName+"%"));
Join country=root.Join(“countryId”);
add(criteriaBuilder.like(country.get(“countryName”),“%”+countryName+“%”);
您是如何定义Country和StateTable之间的关系的?无论如何,解决方案似乎需要一个连接(都使用JPQL和Criteria API)。是的,它们都有关系StateTable
有一个外键,它引用了Country
的主键。为了给出正确答案,有必要了解JPA中如何定义关系。请编辑您的问题并添加此信息编辑后添加实体类。您如何定义国家和州表之间的关系?无论如何,解决方案似乎需要一个连接(都使用JPQL和Criteria API)。是的,它们都有关系StateTable
有一个外键,它引用了Country
的主键。为了给出正确答案,有必要了解JPA中如何定义关系。请编辑您的问题并添加此信息编辑后添加实体类。出现语法错误-在like()
方法上找不到symbol-symbol:method like(Path,String)location:Join类型的变量country
。这是criteriaBuilder.like(…)WithcriteriaBuilder.like(…)
,join完全未使用-join country=root.join(“country”,JoinType.LEFT)代码>引发异常-java.lang.IllegalArgumentException:无法根据路径
解析属性[countryName]。我只需要将根
替换为国家
,但我自己无法解决,抱歉。出现语法错误-找不到符号:类似于(路径,字符串)的方法位置:Join类型的变量country
,在like()
方法上。它是criteriaBuilder。like(…)与criteriaBuilder.like(…)
,Join完全未使用-Join country=root.Join(“country”,JoinType.LEFT)代码>引发异常-java.lang.IllegalArgumentException:无法根据路径
解析属性[countryName]。我只需要将根
替换为国家
,但我自己无法解决这个问题,抱歉。我指的是中间使用子查询的答案,但我无法理解它,因为我使用CriteriaAPI的频率较低(事实上,我必须对其进行更多研究)。通常,我使用JPQL或(HQL和Hibernate),但这里几乎必须使用CriteriaAPI。你的答案有效。非常感谢。我指的是中间使用子查询的答案,但我无法理解它,因为我很少使用CriteriaAPI(事实上,我必须对其进行更多研究)。通常,我使用JPQL或(HQL和Hibernate),但这里几乎必须使用CriteriaAPI。你的答案有效。非常感谢。
Join<StateTable, Country> country = root.join("country", JoinType.LEFT);
// and change predicate with
predicates.add(cb.like(country.<String>get("countryName"), "%"+countryName+"%"));
Join<StateTable, Country> country = root.join("countryId");
predicates.add(criteriaBuilder.like(country.<String>get("countryName"), "%"+countryName+"%"));