gpt4 book ai didi

java - 制定 JPA 标准 'in' 表达式

转载 作者:太空宇宙 更新时间:2023-11-04 08:24:26 25 4
gpt4 key购买 nike

我请求帮助了解如何使用 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;
}
}

最佳答案

列出结果时,必须为查询设置参数值:

TypedQuery<Entity> q = this.entityManager.createQuery(criteriaQuery);
q.setParameter(ContactType.class, yourContactTypeValueToFilter);
q.getResultList();

什么

criteriaBuilder.parameter(ContactType.class);

作用是在查询中创建一个参数,您需要稍后绑定(bind)该参数。

关于java - 制定 JPA 标准 'in' 表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8884344/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com