结合整数和字符串参数的 JPA 条件谓词查询

JPA criteria predicate query combining Integer and String arguments

我正在使用 JPA(EclipseLink 风格),框架 JSF 2.0,查询包含 4 个字段的 MySQL 联系人 table:

contactId(int Pk), firstName (String), surname(String), countryId(int Foreign key)

下面的代码如果只使用字符串参数就可以正常工作,这样用户可以根据需要输入尽可能多或尽可能少的搜索字符串(即:如果搜索框留空,这相当于 select * 并返回整个数据集)。

问题在于组合一个整数参数 (countryId),它是将国家/地区添加到搜索条件的外键。经过一些研究,我仍然无法理解执行此操作的正确方法。

是否需要在 searchContacts 方法中将整数转换为字符串表示形式?我收集到 JPA Criteria API 提供类型转换但不提供类型转换方法?如果是这样,在下面的方法中包含一个整数并将该整数传递给 predicateArray 的最佳方法是什么?

提前致谢!

public ListDataModel<Contacts> searchContacts(String firstname, String surName, int countryId) {

    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<Contacts> query = cb.createQuery(Contacts.class);
    Root<Contacts> cont = query.from(Contacts.class);
    query.select(cont);

    List<Predicate> predicateList = new ArrayList<Predicate>();
    Predicate firstnamePredicate, surnamePredicate, countryPredicate;


    if ((firstname != null) && (!(firstname.isEmpty()))) {
        firstnamePredicate = cb.like(cb.upper(cont.<String>get("firstname")), "%" + firstname.toUpperCase() + "%");
        predicateList.add(firstnamePredicate);
    }

    if ((surName != null) && (!(surName.isEmpty()))) {
        surnamePredicate = cb.like(cb.upper(cont.<String>get("surname")), "%" + surName.toUpperCase() + "%");
        predicateList.add(surnamePredicate);
    }
// here is where I am stuck and trying the solution suggested     by     meskobalazs, except I changed null to 0 since countryId is an integer
    if (countryId != 0) {
    countryPredicate = cb.equal(cont.<Integer>get("countryid"), countryId);
    predicateList.add(countryPredicate);
   }
    Predicate[] predicateArray = new Predicate[predicateList.size()];
    predicateList.toArray(predicateArray);
    query.where(predicateArray);
    ListDataModel<Contacts> contactList = new ListDataModel<Contacts>(em.createQuery(query).getResultList());

    return contactList;
}

}

以上导致了以下 EJB 异常:

Caused by: Exception [EclipseLink-6078] (Eclipse Persistence Services -     2.5.0.v20130507-3faac2b): org.eclipse.persistence.exceptions.QueryException
Exception Description: The class of the argument for the object comparison is incorrect. 
Expression: [
Base com.manaar.domains.Contacts] 
Mapping: [org.eclipse.persistence.mappings.ManyToOneMapping[countryid]] 
Argument: [1]
Query: ReadAllQuery(referenceClass=Contacts )
at    org.eclipse.persistence.exceptions.QueryException.incorrectClassForObjectComparison(QueryException.java:595)
at org.eclipse.persistence.mappings.OneToOneMapping.buildObjectJoinExpression(OneToOneMapping.java:287)
at org.eclipse.persistence.internal.expressions.RelationExpression.normalize(RelationExpression.java:803)

countryid 是一个整数,在联系人实体到国家/地区实体中具有多对一映射。联系人实体是:

@Entity
@Table(name = "contacts")
@XmlRootElement
public class Contacts implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@Basic(optional = false)
@NotNull
@Column(name = "CONTACTSID")
private Integer contactsid;
@JoinColumn(name = "countryid", referencedColumnName = "COUNTRYID")
@ManyToOne
private Country countryid;

国家实体是:

@Entity
@Table(name = "country")
@XmlRootElement

public class Country implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@Basic(optional = false)
@NotNull
@Column(name = "COUNTRYID")
private Integer countryid;
@Size(max = 255)
@Column(name = "COUNTRYNAME")
private String countryname;
.....

你的谓词的解决方案

Predicate class 没有类型化(泛型),因此您不需要以任何不同方式收集不同的谓词。这应该可以正常工作:

if (countryId != null) {
    countryPredicate = cb.equal(cont.<Integer>get("countryid"), countryId);
    predicateList.add(countryPredicate);
}

JPA 规范元模型

这只是一个建议,但您也可以考虑为您的实体构建规范元模型(例如使用 Apache Dali),这样您就可以 get 您的字段以类型安全的方式。

而不是

cont.<MyType>get("typeName")

你可以这样写:

cont.get(_MyType.typeName)

这在重构实体时特别有用,因为您可能会忘记更新字符串,但不能忘记更新元模型,否则代码甚至无法编译。


更新

我遗漏了一些关于您的实体的设计问题。有两种可能的解决方案。

创建新的实体字段

第一个是在 Contacts 中创建一个实际的 Integer 字段并将其映射到与 countryId 相同的字段。

因此您应该将 Country 字段重命名为 country,并创建一个新字段。

@Column(name = "countryid", insertable = false, updatable = false)
private Integer countryId;

@JoinColumn(name = "countryid", referencedColumnName = "countryid")
@ManyToOne
private Country country;

执行此操作后,您可以使用我的原始解决方案。

使用连接

第二种解决方案是在您的查询中使用 Join

if (countryId != null) {
    Join<Contacts, Country> country = cont.join("countryid");
    countryPredicate = cb.equal(country.<Integer>get("countryId"), countryId);
    predicateList.add(countryPredicate);
}