bottabing
bottabing

Reputation: 13

Formulating a JPA Criteria 'in' Expression

I am requesting help in understanding how to formulate an 'in' condition using the javax.persistence.criteria package.

I am creating a CriteriaQuery based up search criteria for a Contacts class. A contact can belong to 0 to many contact types. The search criteria can include a last name value, a contact type or both.

When I try this:

Expression<ContactType> param = criteriaBuilder.parameter(ContactType.class);           
Expression<List<ContactType>> contactTypes = fromContact.get("contactTypes");   
Predicate newPredicate = param.in(this.getContactType(), contactTypes);

I get:

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>]

I haven't been able to find a good example of how to do this. Any assitance and guidance is greatly apprecited. Full code is below.

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;
    }
}

Upvotes: 1

Views: 6914

Answers (1)

reyiyo
reyiyo

Reputation: 593

You have to set the parameter value to the query when you list the results:

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

What

criteriaBuilder.parameter(ContactType.class);

does, is to create a parameter in the query, that you need to bind later.

Upvotes: 1

Related Questions