[英]JPA criteria predicate query combining Integer and String arguments
我正在使用JPA(EclipseLink風格)框架JSF 2.0,查詢包含4個字段的MySQL聯系人表:
contactId(int Pk), firstName (String), surname(String), countryId(int Foreign key)
如果僅使用字符串參數,則以下代碼可以正常工作,以便用戶可以根據需要輸入任意數量的搜索字符串(即:如果搜索框為空,則相當於選擇*,並返回整個數據集)。
問題在於結合使用整數參數(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是一個整數,在contact實體與country實體之間具有manyToOne映射。 聯系人實體為:
@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
類沒有類型化(泛型),因此您不需要以不同的方式收集不同的謂詞。 這應該可以正常工作:
if (countryId != null) {
countryPredicate = cb.equal(cont.<Integer>get("countryid"), countryId);
predicateList.add(countryPredicate);
}
這只是一個建議,但您也可以考慮為您的實體構建規范的元模型(例如,使用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);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.