Query on parent's collection - Hibernate Specification - java

I have three tables created with Hibernate. Product is the parent while barcodes is a collection end price is a child (one to many) of products.
#NotBlank
private String ref;
#ElementCollection(fetch = FetchType.EAGER)
#CollectionTable(name = "products_barcodes")
#Fetch(FetchMode.SELECT)
private List<String> barcodes;
#OneToMany(mappedBy = "parent", fetch = FetchType.LAZY, cascade = CascadeType.ALL, orphanRemoval = true)
private List<PriceEntity> prices;
I'm trying to query starting from the child (Price). I was able to query on a string but now i would like to query on the collection s element.
Specifications<PriceEntity> specifications = where(hasTenant(tid));
if (isNotBlank(ref)) {
specifications = specifications.and(hasRef(ref));
}
if (isNotBlank(barcode)) {
specifications = specifications.and(hasBarcode(barcode));
}
/*********************************/
public static Specification<PriceEntity> hasRef(final String ref) {
return new Specification<PriceEntity>() {
#Override
public Predicate toPredicate(Root<PriceEntity> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
return criteriaBuilder.equal(root.<PriceEntity>get("parent").get("ref"), ref);
}
};
}
public static Specification<PriceEntity> hasBarcode(final String barcode) {
return new Specification<PriceEntity>() {
#Override
public Predicate toPredicate(Root<PriceEntity> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
return criteriaBuilder.equal(root.<PriceEntity>get("parent").get("barcodes"), barcode);
}
};
}
how would you write the specification? The one above is not working, i get this exception at runtime:
"IllegalArgumentException: Parameter value [8003921360408] did not match expected type [java.util.Collection (n/a)]"
Thanks

Question solved by Thomas s comment.
For collections criteriaBuilder.isMember should be used.

Related

Spring boot jpa specification filter records of child entity from parent entity

I have two entities A and B where A is the parent and B is the child. I want to filter records using JPA specifications like Specification.
Their relation is Entity B is having reference to A. I want to apply left join on Entity A using JPA Criteria Builder API. Is it possible to achieve that ?
Here is my Parent entity class
#Entity
public class Parent {
#Id
private Long parentId;
private String name;
private String description;
// constructor, getter and setters
}
Here is my child entity class
#Entity
public class Child {
#Id
private Long childId;
private String childName;
private String email;
#ManyToOne
#JoinColumn(name = "parent_id")
private Parent parent;
// constructor, getter and setters
}
I want to achieve something like this below code, just want to filter each record based on the childName of the Child entity from Parent Specification.
#Component
public class ParentSpecification {
public Specification<Parent> getParentSpecification(Map<String, String> filterValues) {
return (root, query, criteriaBuilder) -> {
Root<Child> from = query.from(Child.class);
from.join("parent", JoinType.LEFT);
List<Predicate> predicates = new ArrayList<>();
filterValues.forEach((attribute, value) -> {
if (attribute.equalsIgnoreCase("childName") && value != null) {
predicates.add(criteriaBuilder.like(from.get("childName"), "%" + value + "%"));
}
});
return query.where(criteriaBuilder.and(predicates.toArray(new Predicate[0]))).getRestriction();
};
}
}
There are many resources online on this topic.
Example:
CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<Tuple> cq = cb.createTupleQuery();
Root<Author> root = cq.from(Author.class);
Join<Object, Object> book = root.join(Author_.BOOKS, JoinType.LEFT);
cq.multiselect(root, book);
ParameterExpression<String> pLastName = cb.parameter(String.class);
cq.where(cb.equal(root.get(Author_.LAST_NAME), pLastName));
TypedQuery<Tuple> q = em.createQuery(cq);
q.setParameter(pLastName, "Janssen");
List<Tuple> authorBooks = q.getResultList();
Reference: https://thorben-janssen.com/hibernate-tip-left-join-fetch-join-criteriaquery/#Defining_a_LEFT_JOIN_or_RIGHT_JOIN_clause

Spring JPA Specification filtering

I'm trying to filter parent entity by intersection of children property and inputed list.
My entities:
#Entity
#Table(name = "recipes")
public class Recipe {
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
#OneToMany(cascade = CascadeType.ALL, orphanRemoval = true)
private List<Ingredient> ingredientsList = new ArrayList<>();
//other fields and methods
}
#Entity
#Table(name = "ingredients")
public class Ingredient {
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
#Column(name = "product")
private String product;
//other fields and methods
}
As result i want get list of Recipes. Each recipe must have all products from user's inputed list.
I was trying smth like this:
public static Specification<Recipe> containsProducts(List<String> products) {
return (root, query, criteriaBuilder) -> {
if (Objects.isNull(products) || products.isEmpty()) {
return criteriaBuilder.conjunction();
}
List<Predicate> predicates = new ArrayList<>();
Join<Recipe, Ingredient> ingredientRecipeJoin = root.join("ingredientsList", JoinType.INNER);
for (String product : products) {
if (product.isBlank()) {
continue;
}
predicates.add(criteriaBuilder.equal(ingredientRecipeJoin.get(Ingredient_.PRODUCT), product));
}
return criteriaBuilder.and(predicates.toArray(Predicate[]::new));
};
}
But it works only when products list size=1.
Finally i got answer. My miss is that i was trying to search recipes by only one join. For every item in user's inputed list i should create a new join and create on it a new prediction. Jpa logic prevent reusing columns from old joins.
So my containsProducts method now looks like:
public static Specification<Recipe> containsProducts(List<String> products) {
return (root, query, criteriaBuilder) -> {
if (Objects.isNull(products) || products.isEmpty()) {
return criteriaBuilder.conjunction();
}
List<Predicate> predicates = new ArrayList<>();
Join<Recipe, Ingredient> ingredientRecipeJoin;
for (String product : products) {
if (product.isBlank()) {
continue;
}
ingredientRecipeJoin = root.join(Recipe_.INGREDIENTS_LIST, JoinType.INNER);
predicates.add(criteriaBuilder.equal(ingredientRecipeJoin.get(Ingredient_.PRODUCT), product));
}
return criteriaBuilder.and(predicates.toArray(Predicate[]::new));
};
}

crudrepository findBy method signature for list of tuples

I have an Entity Class like this:
#Entity
#Table(name = "CUSTOMER")
class Customer{
#Id
#Column(name = "Id")
Long id;
#Column(name = "EMAIL_ID")
String emailId;
#Column(name = "MOBILE")
String mobile;
}
How to write findBy method for the below query using crudrepository spring data jpa?
select * from customer where (email, mobile) IN (("a#b.c","8971"), ("e#f.g", "8888"))
I'm expecting something like
List<Customer> findByEmailMobileIn(List<Tuple> tuples);
I want to get the list of customers from given pairs
I think this can be done with org.springframework.data.jpa.domain.Specification. You can pass a list of your tuples and proceed them this way (don't care that Tuple is not an entity, but you need to define this class):
public class CustomerSpecification implements Specification<Customer> {
// names of the fields in your Customer entity
private static final String CONST_EMAIL_ID = "emailId";
private static final String CONST_MOBILE = "mobile";
private List<MyTuple> tuples;
public ClaimSpecification(List<MyTuple> tuples) {
this.tuples = tuples;
}
#Override
public Predicate toPredicate(Root<Customer> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
// will be connected with logical OR
List<Predicate> predicates = new ArrayList<>();
tuples.forEach(tuple -> {
List<Predicate> innerPredicates = new ArrayList<>();
if (tuple.getEmail() != null) {
innerPredicates.add(cb.equal(root
.<String>get(CONST_EMAIL_ID), tuple.getEmail()));
}
if (tuple.getMobile() != null) {
innerPredicates.add(cb.equal(root
.<String>get(CONST_MOBILE), tuple.getMobile()));
}
// these predicates match a tuple, hence joined with AND
predicates.add(andTogether(innerPredicates, cb));
});
return orTogether(predicates, cb);
}
private Predicate orTogether(List<Predicate> predicates, CriteriaBuilder cb) {
return cb.or(predicates.toArray(new Predicate[0]));
}
private Predicate andTogether(List<Predicate> predicates, CriteriaBuilder cb) {
return cb.and(predicates.toArray(new Predicate[0]));
}
}
Your repo is supposed to extend interface JpaSpecificationExecutor<Customer>.
Then construct a specification with a list of tuples and pass it to the method customerRepo.findAll(Specification<Customer>) - it returns a list of customers.
It is maybe cleaner using a projection :
#Entity
#Table(name = "CUSTOMER")
class CustomerQueryData {
#Id
#Column(name = "Id")
Long id;
#OneToOne
#JoinColumns(#JoinColumn(name = "emailId"), #JoinColumn(name = "mobile"))
Contact contact;
}
The Contact Entity :
#Entity
#Table(name = "CUSTOMER")
class Contact{
#Column(name = "EMAIL_ID")
String emailId;
#Column(name = "MOBILE")
String mobile;
}
After specifying the entities, the repo :
CustomerJpaProjection extends Repository<CustomerQueryData, Long>, QueryDslPredicateExecutor<CustomerQueryData> {
#Override
List<CustomerQueryData> findAll(Predicate predicate);
}
And the repo call :
ArrayList<Contact> contacts = new ArrayList<>();
contacts.add(new Contact("a#b.c","8971"));
contacts.add(new Contact("e#f.g", "8888"));
customerJpaProjection.findAll(QCustomerQueryData.customerQueryData.contact.in(contacts));
Not tested code.

Spring Data Jpa and Specification - how to work with ManyToOne and ManyToMany relations?

I have a simple model in my project.
[UpdatePackage] >- (ManyToOne) - [Version] -< [UseCase] - (ManyToMany)
public class UpdatePackage implements Comparable<UpdatePackage> {
#ManyToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
#JoinColumn(name = COLUMN_ORIG_VERSION, nullable = true)
private Version origVersion;
// setters and getters
}
#Entity
#Table(name = Version.TABLE_NAME)
public class Version {
#ManyToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
#JoinTable(name = JVUC_TABLE, joinColumns = { #JoinColumn(name = JVUC_COLUMN_VERSION, referencedColumnName = COLUMN_ID) }, inverseJoinColumns = { #JoinColumn(name = JVUC_COLUMN_USECASE, referencedColumnName = UseCase.COLUMN_ID) })
private final Set<UseCase> useCases = new HashSet<UseCase>();
// setters and getters
}
#Entity
#Table(name = UseCase.TABLE_NAME)
public class UseCase {
#Column(name = COLUMN_NAME, nullable = false)
private String name;
// setters and getters
}
For implementation of filter I would like to use Spring Data Jpa and Specification from spring.data.jpa.domain
For instance I would like to find list of UpdatePackage with given usecase names.
I understand that for ManyToOne relation I need use Join and for ManyToMany I need to use Fetch.
My implementation of Specification interface looks like this:
public static Specification<UpdatePackage> useCaseNames(final List<String> useCaseNames) {
return new Specification<UpdatePackage>() {
#Override
public Predicate toPredicate(Root<UpdatePackage> root, CriteriaQuery<?> query,
CriteriaBuilder cb) {
final Join<UpdatePackage, Version> version = root.join(UpdatePackage_.destVersion,
JoinType.LEFT);
Fetch<Version, UseCase> useCase = version.fetch(Version_.useCases, JoinType.LEFT);
return null;
// return useCase.get(UseCase_.name).in(useCaseNames);
}
};
}
When I run a integration test I got NPException in line:
Fetch<Version, UseCase> useCase = version.fetch(Version_.useCases, JoinType.LEFT);
because fields joins and fetches of object version are null.
I don't know what I do in wrong way and I cannot find any answer in Internet.
Does anyone know what is wrong in this code?
Stack:
java.lang.NullPointerException
at org.hibernate.ejb.criteria.path.AbstractFromImpl.constructJoin(AbstractFromImpl.java:261)
at org.hibernate.ejb.criteria.path.AbstractFromImpl.fetch(AbstractFromImpl.java:549)
I've found a solution. I had a bug in static model.
static model
From:
public static volatile SingularAttribute<Version, UseCase> useCases;
To:
public static volatile SetAttribute<Version, UseCase> useCases;
and in implementation of specification:
public static Specification<UpdatePackage> useCaseNames(final List<String> useCaseNames) {
return new Specification<UpdatePackage>() {
#Override
public Predicate toPredicate(Root<UpdatePackage> root, CriteriaQuery<?> query,
CriteriaBuilder cb) {
final Join<UpdatePackage, Version> version = root.join(UpdatePackage_.destVersion,
JoinType.LEFT);
final Join<Version, UseCase> useCase = version.join(Version_.useCases);
return useCase.get(UseCase_.name).in(useCaseNames);
}
};
}

Eager fetching in a Spring Specification

We have a Media object:
public class Media implements Serializable {
#Id
#GeneratedValue(strategy = IDENTITY)
#Column(insertable = false, updatable = false)
private Long id;
// other attributes
#ManyToOne(fetch = FetchType.EAGER)
#JoinColumn(name = "channelId", referencedColumnName = "id")
private Channel channel;
// getters, setters, hashCode, equals, etc.
}
The eager fetch of the channel parent works in regular repository methods, but not when using a Specification.
Here's the Specification:
public class MediaSpecs {
public static Specification<Media> search(final Long partnerId, final Integer width, final Integer height,
final String channelType) {
return new Specification<Media>() {
#Override
public Predicate toPredicate(Root<Media> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
Predicate restrictions = cb.equal(root.get("archived"), false);
// other restrictions are and-ed together
if (channelType != null) {
Join<Media, ChannelType> join = root.join("channel").join("orgChannelType").join("type");
restrictions = cb.and(cb.equal(join.get("type"), channelType));
}
return restrictions;
}
};
}
The "search" spec works correctly when channelType is specified, so the join is working. How do I specify that the joins should be eagerly fetched?
I tried adding
Fetch<Media, ChannelType> fetch = root.fetch("channel").fetch("orgChannelType").fetch("type");
Then Hibernate throws an exception:
org.hibernate.QueryException: query specified join fetching, but the owner of the fetched association was not present in the select list [FromElement{explicit,not a collection join,fetch join,fetch non-lazy properties,classAlias=generatedAlias4 ...
How do I add the associations to the select list?
Thanks.
I think you have problem with count query. Usually the specification is use for data query a and count query. And for count query there is no "Media". I use this workaround :
Class<?> clazz = query.getResultType();
if (clazz.equals(Media.class)) {
root.fetch("channel");
}
This use fetch only for data query a not for count query.
For example:
public Predicate toPredicate(Root<Person> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
if (Long.class != query.getResultType()) {
root.fetch(Person_.addresses);
}
return cb.conjunction();
}

Categories

Resources