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

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>

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

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.

Related

Spring JPA CriteriaQuery groupBy based on only one key in a Composite Primary Key (#EmbeddedId)

I am trying to write a criteriaQuery and group results based on emailId, which is one of the keys of a composite PK, embedded into one of my Entity classes.
The method that returns the specification is as follows :
public static Specification < User > getSpecification(Integer id) {
return (root, query, criteriaBuilder) - > {
var predicates = new ArrayList<Predicate()>;
predicates.add(criteriaBuilder.equal(root.get("indexId"), id));
query.groupBy(root.get("details").get("emailId"));
return criteriaBuilder.and(predicates.toArray(new Predicate[0]));
};
}
The entity class:
public class User {
#EmbeddedId private Details details;
private String name;
private String status;
}
#Embeddable
public class Details {
private String emailId;
private Integer branchId;
}
I have skipped some annotations.
I want to group the results in a way where emailId remains unique, even if branchId changes. In essence for data where there are 3 rows of same emailId but different branchId, I should only fetch 1 result.
It seems it throws an error and asks for both components of the composite key to be passed in the query.groupBy statement.
If you could please help me figure out the issue.

Order results based on count using spring boot specification API

Consider the entity below.
PS: The model has more fields but for the question to be short I have posted only the relevant fields
Class Employee {
private String name;
private String country;
private String region;
private String department
#OneToMany
private Set<Skill> skills;
}
Class Skill {
private name;
}
I am using spring boot Specification API to filter employees on different fields like region, country, and so on.
public class EmployeeSpec implements Specification<Employee> {
#Override
public Predicate toPredicate(Root<Employee> root, CriteriaQuery<?> cq, CriteriaBuilder cb) {
String fielName = //some field name
String fieldValue = //some field value
switch (fielName ) {
case "country":
return cb.equal(root.get("country"), fieldValue);
case "department":
return cb.equal(root.get("department"), fieldValue);
case "region":
return cb.equal(root.get("region"), fieldValue);
}
}
I want to order the results such that employee with maximum skills comes first. I am not sure how to implement this using Specification.
You can use CriteriaBuilder.size(..). For your case, the code will look like:
cq.orderBy(cb.desc(cb.size(root.get("skills"))));

Spring Specification Criteria Multiple Joins ? How?

I got stuck using a Spring Project with Spring Data + specification + criteria api.
I will try to simulate the situation with general entities we used write to get easy example.
The Entities:
Consider all attributes of the each entity is passed on the constructor showed below
Country(Long id, String name, String iso)
State(Long id, String name, String iso)
City(Long id, String name, String iso)
This is my repository:
public interface CityRepository extends PagingAndSortingRepository<City, Integer>, JpaSpecificationExecutor<City> {
}
As you can see, I don't need to implement anything on the repository
This is my service
#Service
#Transactional
public class CityService {
#Autowired
private CityRepository cityRepository;
#Transactional(readOnly = true)
public CityListVO findByNameLike(String name, PageRequest pageRequest) {
name = "%" + name + "%";
if (pageRequest == null) {
List<City> result = cityRepository.findAll(fillGridCriteria(name));
return new CityListVO(1, result.size(), result);
} else {
Page<City> result = cityRepository. findAll(fillGridCriteria(name), pageRequest);
return new CityListVO(result.getTotalPages(), result.getTotalElements(), result.getContent());
}
}
private static Specification<City> fillGridCriteria(String name) {
return new Specification<City>() {
#Override
public Predicate toPredicate(
Root<City> root,
CriteriaQuery<?> query,
CriteriaBuilder builder) {
/*
The current return I can do a like by name, and it works fine.
My problem is if for any reason I need to do multiple joins like the folow jpql:
select ci FROM City ci, State st, Country co where ci.st = st AND st.co = co AND co.name = 'Canada';
How to do this from here ? Inside this method.
How is gonna be the return for this method ?
*/
return builder.like(root.get("name"), name.trim());
}
};
}
}
Let's assume you want all the cities that their country's name like name and you have a relational Model in which :
Country(Long id, String name, String iso)
State(Long id,Long country, String name, String iso)
City(Long id, Long state, String name, String iso)
Predicate:
private static Specification<City> fillGridCriteria(String name) {
return new Specification<City>() {
#Override
public Predicate toPredicate(
Root<City> root,
CriteriaQuery<?> query,
CriteriaBuilder builder) {
return
builder.like(root.get("state").get("country").get("name"), name.trim());
}
};
}

Can we build Spring Data JPA specification out of a composite key attribute

I am using Spring Data JPA specifications for generic queries to my entities. So far it has worked well. Now the problem happened when I try to use these on composite keys used via embeddedId annotation. So here I need to use a nested property to query which say if the object is Foo and I am trying to write a criteria over id. id1 for example.
#Entity
#Data
public class Foo implements Serializable {
private static final long serialVersionUID = 1L;
/** The key. */
#EmbeddedId
private FooKey id;
}
#Embeddable
#Data
public class FooKey implements Serializable {
private static final long serialVersionUID = 1L;
/** The key. */
private String id1;
private String id2;
}
In the specification I am trying to do
#Override
public Predicate toPredicate(Root<T> root, CriteriaQuery<?> query, CriteriaBuilder builder) {
// get root type
root.get(property).getJavaType()
But this doesn't work for nested attributes like in this case. Is there any way I can be able to build predicates for properties in the composite key.
Example of Equal:
#Override
public Predicate toPredicate(Root<Foo> root, CriteriaQuery<?> query, CriteriaBuilder builder) {
builder.equal(root.get("id").get("id1"),"my val");
You can build a function like this if you want to use multiple levels. In my experience, 2 levels are more than sufficient. But depends on you.
protected Path<Comparable> getPath(Root<EntityOrModel> root) {
Path<Comparable> path;
if (criteria.getKey().contains(".")) {
String[] split = criteria.getKey().split("\\.");
int keyPosition = 0;
path = root.get(split[keyPosition]);
for (String criteriaKeys : split) {
if (keyPosition > 0) {
path = path.get(criteriaKeys);
}
keyPosition++;
}
} else {
path = root.get(criteria.getKey());
}
return path;
}
Then set
#Override
public Predicate toPredicate(Root<EntityOrModel> root, CriteriaQuery<?> query, CriteriaBuilder builder) {
Path<Comparable> path = getPath(root);
// ...
return builder.equal(path, value)
}

Generic search with spring-data

I use spring data which I found very interesting but there is a problem I want a generic way to search the field of entity.
I got a entity which have many field
public class Lostcard implements java.io.Serializable {
private Integer id;
private String nom;
private String prenom;
private String cin;
#DateTimeFormat(pattern = "MM/dd/yyyy")
private Date dateDeclaration;
#DateTimeFormat(pattern = "MM/dd/yyyy")
private Date dateDuplicata;
private String annexeAdmin;
[...]
So I want to do this:
public interface LostcardRepository extends JpaRepository<Lostcard, Integer> {
List<Lostcard> findByNom(String nom);
List<Lostcard> findByPrenom(String prenom);
List<Lostcard> findByCin(String cin);
[...]
}
There is not a generic way like findByProperty(String property, Object value) ?
The easiest way in my opinion is to use Specification. You have to make your interface extends also JpaSpecificationExecutor and then you can use your own Specification to execute query.
public interface LostcardRepository extends JpaRepository<Lostcard, Integer>, JpaSpecificationExecutor<Lostcard> {
...
}
Then implement class similar to the one below:
public class PropertySpecifications {
public static Specification<Lostcard> byProperty(final String propertyName, final Object propertyValue) {
return new Specification<Lostcard>() {
#Override
public Predicate toPredicate(Root<Lostcard> candidateRoot, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
return criteriaBuilder.equal(candidateRoot.get(propertyName), propertyValue);
}
};
}
}
Then you can execute query:
lostcardRepository.findAll(Specifications.where(PropertySpecifications.byProperty("property", "value")));
You can declare a query with parameters. You can get more complex with that, but using JpaRepository query methods you can only query for existing entity fields.
#Query("SELECT p FROM Lostcard p WHERE p.yourfield = (:field)")
public Lostcard findByProperty(#Param("field") String property);

Resources