Maxim
Maxim

Reputation: 597

JPA Criteria API - LIKE / OR on relationship

I'm trying to implement a search function for an entity shoporder that has 0 to 1 invoice entities assigned to it. I tried the following:

root.fetch("invoice", JoinType.LEFT);
query.where(
        builder.or(
        builder.and(
                builder.isNotNull(root.get("invoice")), 
                builder.like(root.get("invoice").get("invoiceNumber"), "%"+filter+"%")),
        builder.like(root.get("buyerCheckoutMessage"), "%"+filter+"%"))
            );

I also tried it without the and but it still returns nothing although one of the ORs is true for some items. Any ideas?

Upvotes: 0

Views: 110

Answers (1)

tremendous7
tremendous7

Reputation: 751

try this

Join invoice = root.join("invoice", JoinType.LEFT);
query.where(
        builder.or(
            builder.and(
                builder.isNotNull(invoice), 
                builder.like(invoice.get("invoiceNumber"), "%"+filter+"%")),
            builder.like(root.get("buyerCheckoutMessage"), "%"+filter+"%")));

Upvotes: 1

Related Questions