Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring data jpa Specification: How to filter a parent object by its children object property

My entity classes are following

@Entity
@table
public class User {

    @OneToOne
    private UserProfile userProfile;

    // others
}


@Entity
@Table
public class UserProfile {

    @OneToOne
    private Country country;
}

@Entity
@Table
public class Country {
    @OneToMany
    private List<Region> regions;
}

Now I want to get all the user in a particular region. I know the sql but I want to do it by spring data jpa Specification. Following code should not work, because regions is a list and I am trying to match with a single value. How to fetch regions list and compare with single object?

 public static Specification<User> userFilterByRegion(String region){


        return new Specification<User>() {
            @Override
            public Predicate toPredicate(Root<User> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {

                return criteriaBuilder.equal(root.get("userProfile").get("country").get("regions").get("name"), regionalEntity);
            }
        };
    }

Edit: Thanks for the help. Actually I am looking for the equivalent criteria query for the following JPQL

SELECT u FROM User u JOIN FETCH u.userProfile.country.regions ur WHERE ur.name=:<region_name>
like image 951
maruf571 Avatar asked Dec 18 '17 11:12

maruf571


2 Answers

Try this. This should work

criteriaBuilder.isMember(regionalEntity, root.get("userProfile").get("country").get("regions"))

You can define the condition for equality by overriding Equals method(also Hashcode) in Region class

like image 57
pvpkiran Avatar answered Nov 14 '22 21:11

pvpkiran


Snippet from my code

// string constants make maintenance easier if they are mentioned in several lines
private static final String CONST_CLIENT = "client";
private static final String CONST_CLIENT_TYPE = "clientType";
private static final String CONST_ID = "id";
private static final String CONST_POST_OFFICE = "postOffice";
private static final String CONST_INDEX = "index";
...

@Override
public Predicate toPredicate(Root<Claim> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
    List<Predicate> predicates = new ArrayList<Predicate>();
    // we get list of clients and compare client's type
    predicates.add(cb.equal(root
                .<Client>get(CONST_CLIENT)
                .<ClientType>get(CONST_CLIENT_TYPE)
                .<Long>get(CONST_ID), clientTypeId));
    // Set<String> indexes = new HashSet<>();
    predicates.add(root
                .<PostOffice>get(CONST_POST_OFFICE)
                .<String>get(CONST_INDEX).in(indexes));
    // more predicates added
    return return andTogether(predicates, cb);
}

private Predicate andTogether(List<Predicate> predicates, CriteriaBuilder cb) {
    return cb.and(predicates.toArray(new Predicate[0]));
}

If you are sure, that you need only one predicate, usage of List may be an overkill.

like image 20
dimirsen Z Avatar answered Nov 14 '22 21:11

dimirsen Z