JPA 条件谓词查询结合 Integer 和 String 参数
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)
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;
}
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)
@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;
.....
if (countryId != null) {
countryPredicate = cb.equal(cont.<Integer>get("countryid"), countryId);
predicateList.add(countryPredicate);
}
cont.<MyType>get("typeName")
cont.get(_MyType.typeName)
@Column(name ="countryid", insertable = false, updatable = false)
private Integer countryId;
@JoinColumn(name ="countryid", referencedColumnName ="countryid")
@ManyToOne
private Country country;
if (countryId != null) {
Join<Contacts, Country> country = cont.join("countryid");
countryPredicate = cb.equal(country.<Integer>get("countryId"), countryId);
predicateList.add(countryPredicate);
}
如果仅使用字符串参数,则以下代码可以正常工作,以便用户可以根据需要输入尽可能多或尽可能少的搜索字符串(即:如果搜索框留空,则相当于选择 * 并且整个数据集是回)。
问题在于组合一个整数参数 (countryId),它是将国家添加到搜索条件的外键。经过一些研究,我仍然无法理解正确的方法来做到这一点。
是否需要在 searchContacts 方法中将整数转换为字符串表示形式?我收集到 JPA Criteria API 提供类型转换但不提供类型转换方法?如果是这样,在下面的方法中包含一个整数并将这个整数传递给 predicateArray 的最佳方法是什么?
提前致谢!
contactId(int Pk), firstName (String), surname(String), countryId(int Foreign key)
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;
}
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)
@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;
.....
if (countryId != null) {
countryPredicate = cb.equal(cont.<Integer>get("countryid"), countryId);
predicateList.add(countryPredicate);
}
cont.<MyType>get("typeName")
cont.get(_MyType.typeName)
@Column(name ="countryid", insertable = false, updatable = false)
private Integer countryId;
@JoinColumn(name ="countryid", referencedColumnName ="countryid")
@ManyToOne
private Country country;
if (countryId != null) {
Join<Contacts, Country> country = cont.join("countryid");
countryPredicate = cb.equal(country.<Integer>get("countryId"), countryId);
predicateList.add(countryPredicate);
}
}
上述导致以下 EJB 异常:
contactId(int Pk), firstName (String), surname(String), countryId(int Foreign key)
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;
}
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)
@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;
.....
if (countryId != null) {
countryPredicate = cb.equal(cont.<Integer>get("countryid"), countryId);
predicateList.add(countryPredicate);
}
cont.<MyType>get("typeName")
cont.get(_MyType.typeName)
@Column(name ="countryid", insertable = false, updatable = false)
private Integer countryId;
@JoinColumn(name ="countryid", referencedColumnName ="countryid")
@ManyToOne
private Country country;
if (countryId != null) {
Join<Contacts, Country> country = cont.join("countryid");
countryPredicate = cb.equal(country.<Integer>get("countryId"), countryId);
predicateList.add(countryPredicate);
}
countryid 是一个整数,在联系人实体到国家实体中具有 manyToOne 映射。联系人实体是:
contactId(int Pk), firstName (String), surname(String), countryId(int Foreign key)
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;
}
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)
@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;
.....
if (countryId != null) {
countryPredicate = cb.equal(cont.<Integer>get("countryid"), countryId);
predicateList.add(countryPredicate);
}
cont.<MyType>get("typeName")
cont.get(_MyType.typeName)
@Column(name ="countryid", insertable = false, updatable = false)
private Integer countryId;
@JoinColumn(name ="countryid", referencedColumnName ="countryid")
@ManyToOne
private Country country;
if (countryId != null) {
Join<Contacts, Country> country = cont.join("countryid");
countryPredicate = cb.equal(country.<Integer>get("countryId"), countryId);
predicateList.add(countryPredicate);
}
国家实体是:
contactId(int Pk), firstName (String), surname(String), countryId(int Foreign key)
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;
}
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)
@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;
.....
if (countryId != null) {
countryPredicate = cb.equal(cont.<Integer>get("countryid"), countryId);
predicateList.add(countryPredicate);
}
cont.<MyType>get("typeName")
cont.get(_MyType.typeName)
@Column(name ="countryid", insertable = false, updatable = false)
private Integer countryId;
@JoinColumn(name ="countryid", referencedColumnName ="countryid")
@ManyToOne
private Country country;
if (countryId != null) {
Join<Contacts, Country> country = cont.join("countryid");
countryPredicate = cb.equal(country.<Integer>get("countryId"), countryId);
predicateList.add(countryPredicate);
}
谓词的解决方案
Predicate 类没有类型(通用),因此您不需要以任何不同的方式收集不同的谓词。这应该可以正常工作:
contactId(int Pk), firstName (String), surname(String), countryId(int Foreign key)
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;
}
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)
@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;
.....
if (countryId != null) {
countryPredicate = cb.equal(cont.<Integer>get("countryid"), countryId);
predicateList.add(countryPredicate);
}
cont.<MyType>get("typeName")
cont.get(_MyType.typeName)
@Column(name ="countryid", insertable = false, updatable = false)
private Integer countryId;
@JoinColumn(name ="countryid", referencedColumnName ="countryid")
@ManyToOne
private Country country;
if (countryId != null) {
Join<Contacts, Country> country = cont.join("countryid");
countryPredicate = cb.equal(country.<Integer>get("countryId"), countryId);
predicateList.add(countryPredicate);
}
JPA 规范元模型
这只是一个建议,但您也可以考虑为您的实体构建规范元模型(例如使用 Apache Dali),这样您就可以以类型安全的方式 get 您的字段。
代替
contactId(int Pk), firstName (String), surname(String), countryId(int Foreign key)
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;
}
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)
@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;
.....
if (countryId != null) {
countryPredicate = cb.equal(cont.<Integer>get("countryid"), countryId);
predicateList.add(countryPredicate);
}
cont.<MyType>get("typeName")
cont.get(_MyType.typeName)
@Column(name ="countryid", insertable = false, updatable = false)
private Integer countryId;
@JoinColumn(name ="countryid", referencedColumnName ="countryid")
@ManyToOne
private Country country;
if (countryId != null) {
Join<Contacts, Country> country = cont.join("countryid");
countryPredicate = cb.equal(country.<Integer>get("countryId"), countryId);
predicateList.add(countryPredicate);
}
你可以写:
contactId(int Pk), firstName (String), surname(String), countryId(int Foreign key)
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;
}
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)
@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;
.....
if (countryId != null) {
countryPredicate = cb.equal(cont.<Integer>get("countryid"), countryId);
predicateList.add(countryPredicate);
}
cont.<MyType>get("typeName")
cont.get(_MyType.typeName)
@Column(name ="countryid", insertable = false, updatable = false)
private Integer countryId;
@JoinColumn(name ="countryid", referencedColumnName ="countryid")
@ManyToOne
private Country country;
if (countryId != null) {
Join<Contacts, Country> country = cont.join("countryid");
countryPredicate = cb.equal(country.<Integer>get("countryId"), countryId);
predicateList.add(countryPredicate);
}
如果您正在重构实体,这尤其有用,因为您可能会忘记更新字符串,但您不能忘记更新元模型,因为代码甚至无法编译。
更新
我遗漏了一些与您的实体有关的设计问题。有两种可能的解决方案。
创建一个新的实体字段
第一个是在 Contacts 中创建一个实际的 Integer 字段并将其映射到与 countryId 相同的字段。
所以你应该将 Country 字段重命名为 Country,并创建一个新字段。
contactId(int Pk), firstName (String), surname(String), countryId(int Foreign key)
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;
}
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)
@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;
.....
if (countryId != null) {
countryPredicate = cb.equal(cont.<Integer>get("countryid"), countryId);
predicateList.add(countryPredicate);
}
cont.<MyType>get("typeName")
cont.get(_MyType.typeName)
@Column(name ="countryid", insertable = false, updatable = false)
private Integer countryId;
@JoinColumn(name ="countryid", referencedColumnName ="countryid")
@ManyToOne
private Country country;
if (countryId != null) {
Join<Contacts, Country> country = cont.join("countryid");
countryPredicate = cb.equal(country.<Integer>get("countryId"), countryId);
predicateList.add(countryPredicate);
}
完成此操作后,您就可以使用我原来的解决方案了。
使用连接
第二种解决方案是在查询中使用 Join。
contactId(int Pk), firstName (String), surname(String), countryId(int Foreign key)
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;
}
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)
@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;
.....
if (countryId != null) {
countryPredicate = cb.equal(cont.<Integer>get("countryid"), countryId);
predicateList.add(countryPredicate);
}
cont.<MyType>get("typeName")
cont.get(_MyType.typeName)
@Column(name ="countryid", insertable = false, updatable = false)
private Integer countryId;
@JoinColumn(name ="countryid", referencedColumnName ="countryid")
@ManyToOne
private Country country;
if (countryId != null) {
Join<Contacts, Country> country = cont.join("countryid");
countryPredicate = cb.equal(country.<Integer>get("countryId"), countryId);
predicateList.add(countryPredicate);
}