I have a OneToMany relationship:
public class Class1{
private String attr1;
private String attr2;
@OneToOne
private Class2 object1;
}
public class Class2{
private String attr3;
}
The relationship can store null objects (I mean that an instance of Class1 can have null in object1). I want to do a complete search in all attributes so I’ve created this HQL query:
SELECT p FROM Class1 p
WHERE
(
upper(p.attr1) LIKE :filter OR
upper(p.attr2) LIKE :filter OR
upper(p.object1.attr3) LIKE :filter
)
and I pass a String param in uppercase with “%” before and after:
query.setParameter("filter", "%"+filter.trim().toUpperCase()+"%");
It works OK but for the entities which have object1 = null. It never get those entities. Indeed if I remove the last condition (upper(p.object1.attr3) LIKE :filter) it works but I need to include it.
I’ve though I should do a LEFT JOIN with that relationship so I’ve tried to write it as :
SELECT p FROM Class1 p LEFT JOIN p.object1 AS p1
WHERE
(
upper(p.attr1) LIKE :filter OR
upper(p.attr2) LIKE :filter OR
upper(p1.attr3) LIKE :filter
)
or like
SELECT p FROM Class1 p LEFT JOIN p.object1 AS p1
WHERE
(
upper(p.attr1) LIKE :filter OR
upper(p.attr2) LIKE :filter OR
(p1 is not null AND upper(p1.attr3) LIKE :filter)
)
but without any success.
Any idea?
Thanks.
As I understand you need the values either
p1.attr3 is nulland when it isnot nullit should validate the condition. So, I am just fine tuning your last query;