我使用JPA(EclipseLink风格),框架JSF 2.0,查询包含4个字段的MySQL联系人表:
contactId(int Pk), firstName (String), surname(String), countryId(int Foreign key)
如果仅使用字符串参数,以下代码可以正常工作,以便用户可以根据需要输入尽可能多的搜索字符串(即:如果搜索框保留为空白,则相当于select *,整个数据集为回)。
问题在于组合一个整数参数(countryId),它是将国家/地区添加到搜索条件的外键。经过一些研究,我仍然无法理解正确的方法。
整数是否需要在searchContacts方法中转换为字符串表示?我收集到JPA Criteria API提供了typcasting而不是类型转换方法?如果是这样,在下面的方法中包含整数并将此整数传递给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是一个整数,在联系人实体中向国家/地区实体提供了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;
.....
答案 0 :(得分:1)
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)
如果你要重构你的实体,这是特别有用的,因为你可能忘记更新字符串,但是你不能忘记更新你的元模型,因为代码甚至不会编译。
我错过了您的实体的一些设计问题。有两种可能的解决方案。
第一个是在Integer
中创建一个实际的Contacts
字段,并将其映射到与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);
}