我正在请求帮助,以了解如何使用javax.persistence.criteria包来表达'in'条件。
我正在为Contacts类创建基于CriteriaQuery的搜索条件。联系人可以属于0到多个联系人类型。搜索条件可以包括姓氏值,联系人类型或两者。
当我尝试这个时:
Expression<ContactType> param = criteriaBuilder.parameter(ContactType.class);
Expression<List<ContactType>> contactTypes = fromContact.get("contactTypes");
Predicate newPredicate = param.in(this.getContactType(), contactTypes);
我明白了:
org.apache.openjpa.persistence.ArgumentException: Cannot execute query; declared parameters "ParameterExpression<ContactType>" were not given values. You must supply a value for each of the following parameters, in the given order: [ParameterExpression<ContactType>]
我无法找到如何做到这一点的好例子。任何协助和指导都非常值得赞赏。完整代码如下。
public CriteriaQuery<Contact> getSearchCriteriaQuery(EntityManager entityManager) {
CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
CriteriaQuery<Contact> criteriaQuery = criteriaBuilder.createQuery(Contact.class);
Root<Contact> fromContact = criteriaQuery.from(Contact.class);
Predicate whereClause = criteriaBuilder.equal(fromContact.get("domain"), this.getDomain());
if (!StringUtils.isEmpty(this.getLastName())) {
Predicate newPredicate = criteriaBuilder.equal(fromContact.get("lastName"), this.getLastName());
whereClause = criteriaBuilder.and(whereClause, newPredicate);
}
if (this.getContactType() != null) {
Expression<ContactType> param = criteriaBuilder.parameter(ContactType.class);
Expression<List<ContactType>> contactTypes = fromContact.get("contactTypes");
Predicate newPredicate = param.in(this.getContactType(), contactTypes);
whereClause = criteriaBuilder.and(whereClause, newPredicate);
}
return criteriaQuery.where(whereClause);
}
@Entity
@Table(name = "contact")
public class Contact implements Serializable {
private static final long serialVersionUID = -2139645102271977237L;
private Long id;
private String firstName;
private String lastName;
private Domain domain;
private List<ContactType> contactTypes;
public Contact() {
}
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(unique = true, nullable = false)
public Long getId() {
return this.id;
}
public void setId(Long id) {
this.id = id;
}
@Column(name = "FIRST_NAME", length = 20)
public String getFirstName() {
return this.firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
@Column(name = "LAST_NAME", length = 50)
public String getLastName() {
return this.lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
//bi-directional many-to-one association to Domain
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "DOMAIN")
public Domain getDomain() {
return this.domain;
}
public void setDomain(Domain domain) {
this.domain = domain;
}
@ManyToMany
@JoinTable(name = "CONTACT_CNTTYPE",
joinColumns = {
@JoinColumn(name = "CONTACT", referencedColumnName = "ID")},
inverseJoinColumns = {
@JoinColumn(name = "CONTACT_TYPE", referencedColumnName = "ID")})
public List<ContactType> getContactTypes() {
return this.contactTypes;
}
public void setContactTypes(List<ContactType> contactTypes) {
this.contactTypes = contactTypes;
}
}
答案 0 :(得分:1)
列出结果时,必须将参数值设置为查询:
TypedQuery<Entity> q = this.entityManager.createQuery(criteriaQuery);
q.setParameter(ContactType.class, yourContactTypeValueToFilter);
q.getResultList();
什么
criteriaBuilder.parameter(ContactType.class);
确实是在查询中创建一个您需要稍后绑定的参数。