0

I am new to JPA.

I have found how to use JPA criteria API in the below link: JPA Criteria API with multiple parameters

The code for predicate is:

Root<CustomerEntity > customerEntity = cq.from(CustomerEntity.class);
List<Predicate> predicates = new ArrayList<Predicate>();
//Adding predicates in case of parameter not being null
    if (param1 != null) {
        predicates.add(
                qb.equal(customerEntity.get("fieldName"), param1));
    }

But my customer entity class has a embeddable id (composite primary key)

How can I use JPA criteria API in this scenario

Are all the embeddable id object values mandatory for searching as well(Select query)?

Community
  • 1
  • 1
firstpostcommenter
  • 2,328
  • 4
  • 30
  • 59

1 Answers1

1

You have to do 2 steps:

Implement equals and hashcode on your embeddable object

Create predicate like normal

  predicates.add(
            qb.equal(customerEntity.get("id"), embeddableId));

Update You cannot search by the whole object without filling all its values. To search for just one property, just go through the path and compare

predicates.add(
            qb.equal(customerEntity.get("id").get("property1"), embeddableId.getProperty1()));

Hope this will help!

thanh ngo
  • 834
  • 5
  • 9
  • Hi, I am not able to get any result-set from the Database if I do not set all the values of the embedded id object. I need the fields to be optional for select query (searching). Is this possible? – firstpostcommenter Aug 11 '16 at 12:14