Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JPA perform part of an query if a value is not null

Tags:

jpa

in Java the following is a common idiom:

if( null != obj && obj.getSomeNumber() > 0 ) { ... }

This will perform the length check only, if null != obj is true. However, in a JPA NamedQuery this does not work:

@NamedQuery(
name = "query"
query = "SELECT o FROM SomeObjectList o WHERE o.someObject is not null AND o.someObject.someNumber > 0")

(This is what I've expected, as this would also not work in SQL.)

o.someObject is either 'null' or an foreign key to a table where SomeObjects are stored. (Each column of the table corresponds to an attribute of SomeObject.)

-------------------------           ----------------------------
| Table: SomeObjectList |           |    Table: SomeObject     |
-------------------------           ----------------------------
|  id   |   someObject  |           |  id    |  number |  name |
-------------------------           ----------------------------
|   1   |        4      |           |   3    |    -4   | foo   |
-------------------------           ----------------------------
|   2   |       null    |           |   4    |     2   |  bar  |
-------------------------           ----------------------------

So I'd like to create a NamedQuery which will return all Objects from SomeObjectList which either have no object (someObject == null) or where SomeObject.number > 0. Currently, I obtain all objects and check myself if someObject is set.

But is there a way to get an similar behavior in JPA or does I have to perform the checks on the returned objects?

edit: Added graphics and clarified the problem. (Thanks to James.)

like image 851
Arvodan Avatar asked May 02 '11 10:05

Arvodan


People also ask

Does JPA return null or empty list?

The normal behavior is indeed returning an empty list if no results are found. If a List<Object> is the return value of the method in the defined interface, the method should never return Null . The problem is that a parameter is given to the method and is not used anywhere in the Query.

How does JPA handle null values?

The JPA specification defines that during ordering, NULL values shall be handled in the same way as determined by the SQL standard. The standard specifies that all null values shall be returned before or after all non-null values. It's up to the database to pick one of the two options.

What is the use of @query in JPA?

In order to define SQL to execute for a Spring Data repository method, we can annotate the method with the @Query annotation — its value attribute contains the JPQL or SQL to execute. The @Query annotation takes precedence over named queries, which are annotated with @NamedQuery or defined in an orm. xml file.

Is empty in JPA query?

The IS EMPTY operator is the logical equivalent of IS NULL, but for collections. Queries can use IS EMPTY operator or IS NOT EMPTY to check whether a collection association path resolves to an empty collection or has at least one value. We can use the EMPTY to check if a property is empty.


1 Answers

You need to use an outer join for this.

Note your query is wrong, you want seem to want an OR not an AND, otherwise just remove the null check.

SELECT o FROM SomeObjectList o left join o.someObject so WHERE o.someObject is null OR so.someNumber > 0
like image 188
James Avatar answered Sep 28 '22 18:09

James