Specification OR clause, in and isEmpty/isNull - spring-boot

I have workers that have competences (driving licenses and such) and then there are mechanisms that require certain competences. Sometimes the mechanisms require no competences at all.
Currently I have a Specification with an in clause that works fine, but I would like it to also send out mechanisms that require no competences to operate.
public static Specification<Mechanism> hasCompetences(String searchTerm) {
return (root, query, criteriaBuilder) -> {
query.distinct(true);
List<String> list = new ArrayList<>(Arrays.asList(searchTerm.split(",")));
return root.join("competences").get("name").in(list);
};
}
If I have 3 mechanisms with competences like
Car | B-Category |
Van | C-Category |
Bicycle |(no data here) |
After requesting mechanisms?competences=B-Category it returns Car as expected, but I would like to get the Bicycle too.
Or is there a way to get all all mechanisms that don't require competences? I tried mechanisms?competences= but that returned [].
Edit:
This is where I'm at right now:
public static Specification<Mechanism> hasCompetences(List<String> list) {
return (root, query, cb) -> {
query.distinct(true);
return cb.or(
cb.isEmpty(root.join("competences")),
root.join("competences").get("name").in(list)
);
};
}
But the isEmpty is giving me this error:
java.lang.IllegalArgumentException: unknown collection expression type [org.hibernate.query.criteria.internal.path.SetAttributeJoin]
Edit2:
public static Specification<Mechanism> hasCompetences(List<String> list) {
return (root, query, cb) -> {
query.distinct(true);
Join<Mechanism, Set<Competence>> competences = root.join("competences", JoinType.LEFT);
return cb.or(
root.join("competences").get("name").in(list),
cb.isEmpty(competences)
);
};
}
Error:
unknown collection expression type [org.hibernate.query.criteria.internal.path.SetAttributeJoin];

You have 2 errors:
The criteria to match empty collection is cb.isEmpty(root.get("competences"))
You need to specify left join. root.join("competences", JoinType.LEFT)
Without the second amendment, you make an inner join, so you will never retrieve Mechanisms with empty competences.
Update
You proposed
Join<Mechanism, Set<Competence>> competences = root.join("competences", JoinType.LEFT);
return cb.or(
root.join("competences").get("name").in(list),
cb.isEmpty(competences)
);
isEmpty won't work on SetAttributeJoin (the result of root.join) - look point 1. above
Try
Join<Mechanism, Set<Competence>> competences = root.join("competences", JoinType.LEFT);
return cb.or(
competences.get("name").in(list),
cb.isEmpty(root.get("competences"))
);

Related

JPA CriteriaSepcification IN clause

I'm using SpringBoot 2.2.6 with JPA and I need to do query with IN clause as mentioned in Title. I have try with:
#Override
public Predicate toPredicate(Root<Distinta> root, CriteriaQuery<?> query, CriteriaBuilder builder) {
List<Predicate> predicates = new ArrayList<>();
.....
.....
for (DistintaCriteria criteria : list) {
switch(criteria.getOperation()) {
case TEST:
Join<Entity, JoinEntity> join = root.join("joinEntity");
predicates.add(join.<Integer>get("id").in(criteria.getValue()));
}
}
where criteria.getValue() is a Integer[] array but it doesn't work. Can you help me?
Thank you all.
UPDATE
If I try the same Query with List<String> it works! With Integer I had this error:
Unaware how to convert value [[2, 3, 4, 5] : java.util.ArrayList] to requested type [java.lang.Integer]
I have solved as follows:
Join<Entity, JoinEntity> join = root.join("joinEntity");
Predicate in = join.get("id").in((List<Integer>)criteria.getValue());
predicates.add(in);
I don't know why with List<String> I don't need to cast.
Hope helps.
For in clause we need to pass a list always.
You need to convert your Integer array to Integer list using Java-8 like
List<Integer> values = Arrays.asList(criteria.getValue())
#Override
public Predicate toPredicate(Root<Distinta> root, CriteriaQuery<?> query, CriteriaBuilder builder) {
List<Predicate> predicates = new ArrayList<>();
.....
.....
for (DistintaCriteria criteria : list) {
List<Integer> values = Arrays.asList(criteria.getValue());
switch(criteria.getOperation()) {
case TEST:
Join<Entity, JoinEntity> join = root.join("joinEntity");
predicates.add(join.<Integer>get("id").in(values));
}
}
In eclipse, we will get the warning like if we pass an array
Type Integer[] of the last argument to a method in(Object...) doesn't exactly match the vararg parameter type. Cast to Object[] to confirm the non-varargs invocation, or pass individual arguments of type Object for a varargs invocation.

CriteraQuery in toPredicate method is not working

I wanted to fetch only select column from the DB using multiple where clause below is my simple implementation. Am getting the result but am getting data for all the column instead of my requested fileName column alone.
List<ImageSiloVO> queryResult = imageSiloRepo.findAll(new Specification<ImageSiloVO>() {
#Override
public Predicate toPredicate(Root<ImageSiloVO> root, CriteriaQuery<?>query, CriteriaBuilder criteriaBuilder) {
query.select(root.get("fileName"));
List<Predicate> predicates = new ArrayList<>();
if(StringUtils.isNoneBlank(imageSiloVO.getEntryNumber())) {
predicates.add(criteriaBuilder.and(criteriaBuilder.equal(root.get("entryNumber"), imageSiloVO.getEntryNumber())));
}
return criteriaBuilder.and(predicates.toArray(new Predicate[predicates.size()]));
}
});

Merge specifications of different types in Criteria Query Specifications

I'm having an Activity entity which is in #ManyToOne relationship with Event entity and their corresponding metamodels - Activity_ and Event_ were generated by JPA model generator.
I've created specialized classes ActivitySpecifications and EventSpecifications. Those classes contain only static methods whose return Specification. For example:
public interface EventSpecifications {
static Specification<Event> newerThan(LocalDateTime date) {
return (root, cq, cb) -> cb.gt(Event_.date, date);
}
...
}
so when I want to build query matching multiple specifications, I can execute following statement using findAll on JpaSpecificationExecutor<Event> repository.
EventSpecifications.newerThan(date).and(EventSpecifications.somethingElse())
and ActivitySpecifications example:
static Specification<Activity> forActivityStatus(int status) { ... }
How do I use EventSpecifications from ActivitySpecifications ? I mean like merge specifications of different type. I'm sorry, but I don't even know how to ask it properly, but theres simple example:
I want to select all activities with status = :status and where activity.event.date is greater than :date
static Specification<Activity> forStatusAndNewerThan(int status, LocalDateTime date) {
return forActivityStatus(status)
.and((root, cq, cb) -> root.get(Activity_.event) ....
// use EventSpecifications.newerThan(date) somehow up there
}
Is something like this possible?
The closest thing that comes to my mind is using the following:
return forActivityStatus(status)
.and((root, cq, cb) -> cb.isTrue(EventSpecifications.newerThan(date).toPredicate(???, cq, cb));
where ??? requires Root<Event>, but I can only get Path<Event> using root.get(Activity_.event).
In its basic form, specifications are designed to be composable only if they refer to the same root.
However, it shouldn't be too difficult to introduce your own interface which is easily convertible to Specification and which allows for specifications refering to arbitrary entities to be composed.
First, you add the following interface:
#FunctionalInterface
public interface PathSpecification<T> {
default Specification<T> atRoot() {
return this::toPredicate;
}
default <S> Specification<S> atPath(final SetAttribute<S, T> pathAttribute) {
// you'll need a couple more methods like this one for all flavors of attribute types in order to make it fully workable
return (root, query, cb) -> {
return toPredicate(root.join(pathAttribute), query, cb);
};
}
#Nullable
Predicate toPredicate(Path<T> path, CriteriaQuery<?> query, CriteriaBuilder criteriaBuilder);
}
You then rewrite the specifications as follows:
public class ActivitySpecifications {
public static PathSpecification<Activity> forActivityStatus(ActivityStatus status) {
return (path, query, cb) -> cb.equal(path.get(Activity_.status), cb.literal(status));
}
}
public class EventSpecifications {
public static PathSpecification<Event> newerThan(LocalDateTime date) {
return (path, cq, cb) -> cb.greaterThanOrEqualTo(path.get(Event_.createdDate), date);
}
}
Once you've done that, you should be able to compose specifications in the following manner:
activityRepository.findAll(
forActivityStatus(ActivityStatus.IN_PROGRESS).atRoot()
.and(newerThan(LocalDateTime.of(2019, Month.AUGUST, 1, 0, 0)).atPath(Activity_.events))
)
The above solution has the additional advantage in that specifying WHERE criteria is decoupled from specifying paths, so if you have multiple associations between Activity and Event, you can reuse Event specifications for all of them.
Consider the following :
ClassA {
id;
}
ClassB {
foreignId; //id of A
}
For combining Specification<ClassA> specA, Specification<ClassB> specB
specB = specB.and(combineSpecs(specA);
private static Specification<ClassB> combineSpecs(Specification<ClassA> specA) {
return (root_b,query,builder) {
Subquery<ClassA> sub = query.subquery(ClassA.class);
Root<ClassA> root_a = sub.from(ClassA.class);
Predicate p1 = specA.toPredicate(root_a,query,builder);
Predicate p2 = builder.equal(root_a.get("id"),root_b.get("foreignId"));
Predicate predicate = builder.and(p1,p2);
sub.select(root_a).where(predicate);
return builder.exists(sub);
};
}

Linq2SQL "Local sequence cannot be used in LINQ to SQL" error

I have a piece of code which combines an in-memory list with some data held in a database. This works just fine in my unit tests (using a mocked Linq2SqlRepository which uses List).
public IRepository<OrderItem> orderItems { get; set; }
private List<OrderHeld> _releasedOrders = null;
private List<OrderHeld> releasedOrders
{
get
{
if (_releasedOrders == null)
{
_releasedOrders = new List<nOrderHeld>();
}
return _releasedOrders;
}
}
.....
public int GetReleasedCount(OrderItem orderItem)
{
int? total =
(
from item in orderItems.All
join releasedOrder in releasedOrders
on item.OrderID equals releasedOrder.OrderID
where item.ProductID == orderItem.ProductID
select new
{
item.Quantity,
}
).Sum(x => (int?)x.Quantity);
return total.HasValue ? total.Value : 0;
}
I am getting an error I don't really understand when I run it against a database.
Exception information:
Exception type: System.NotSupportedException
Exception message: Local sequence cannot be used in LINQ to SQL
implementation of query operators
except the Contains() operator.
What am I doing wrong?
I'm guessing it's to do with the fact that orderItems is on the database and releasedItems is in memory.
EDIT
I have changed my code based on the answers given (thanks all)
public int GetReleasedCount(OrderItem orderItem)
{
var releasedOrderIDs = releasedOrders.Select(x => x.OrderID);
int? total =
(
from item in orderItems.All
where releasedOrderIDs.Contains(item.OrderID)
&& item.ProductID == orderItem.ProductID
select new
{
item.Quantity,
}
).Sum(x => (int?)x.Quantity);
return total.HasValue ? total.Value : 0;
}
I'm guessing it's to do with the fact
that orderItems is on the database
and releasedItems is in memory.
You are correct, you can't join a table to a List using LINQ.
Take a look at this link:
http://flatlinerdoa.spaces.live.com/Blog/cns!17124D03A9A052B0!455.entry
He suggests using the Contains() method but you'll have to play around with it to see if it will work for your needs.
It looks like you need to formulate the db query first, because it can't create the correct SQL representation of the expression tree for objects that are in memory. It might be down to the join, so is it possible to get a value from the in-memory query that can be used as a simple primitive? For example using Contains() as the error suggests.
You unit tests work because your comparing a memory list to a memory list.
For memory list to database, you will either need to use the memoryVariable.Contains(...) or make the db call first and return a list(), so you can compare memory list to memory list as before. The 2nd option would return too much data, so your forced down the Contains() route.
public int GetReleasedCount(OrderItem orderItem)
{
int? total =
(
from item in orderItems.All
where item.ProductID == orderItem.ProductID
&& releasedOrders.Contains(item.OrderID)
select new
{
item.Quantity,
}
).Sum(x => (int?)x.Quantity);
return total.HasValue ? total.Value : 0;
}

Trouble with a LINQ 'filter' code throwing an error

I've got the following code in my Services project, which is trying to grab a list of posts based on the tag ... just like what we have here at SO (without making this a meta.stackoverflow.com question, with all due respect....)
This service code creates a linq query, passes it to the repository and then returns the result. Nothing too complicated. My LINQ filter method is failing with the following error :-
Method 'Boolean
Contains(System.String)' has no
supported translation to SQL.
I'm not sure how i should be changing my linq filter method :( Here's the code...
public IPagedList<Post> GetPosts(string tag, int index, int pageSize)
{
var query = _postRepository.GetPosts()
.WithMostRecent();
if (!string.IsNullOrEmpty(tag))
{
query = from q in query
.WithTag(tag) // <--- HERE'S THE FILTER
select q;
}
return query.ToPagedListOrNull(index, pageSize);
}
and the Filter method...
public static IQueryable<Post> WithTag(this IQueryable<Post> query,
string tag)
{
// 'TagList' (property) is an IList<string>
return from p in query
where p.TagList.Contains(tag)
select p;
}
Any ideas? I'm at a loss :(
Try with Any:
public static IQueryable<Post> WithTag(this IQueryable<Post> query,
string tag)
{
// 'TagList' (property) is an IList<string>
return from p in query
where p.TagList.Any(t => t == tag)
select p;
}
.
UPDATE (by PureKrome)
Another suggestion by Ahmad (in a comment below). This uses the Contains method so it will return all posts that contain the tag 'Test', eg. Post with Tag 'Testicle' :-
public static IQueryable<Post> WithTag(this IQueryable<Post> query,
string tag)
{
// 'TagList' (property) is an IList<string>
return from p in query
where p.TagList.Any(t => t.Contains(tag))
select p;
}
In WithTag try changing the query to use a List rather than an IList:
return from p in query
let taglist = p.TagList as List<string>
where taglist.Contains(tag)
select p;
Also check out this answer, which is similar to my suggestion: Stack overflow in LINQ to SQL and the Contains keyword

Resources