Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring JPA Query returns Null instead of List

Tags:

I have an @Entity Video having a one-to-many relation with a List<Tag> tags as one of its fields. I use the following @Repository using Spring Data to get the most popular tags:

@Repository
public interface TagRepository extends CrudRepository<Tag, Integer>{
    @Query("SELECT t FROM Tag t WHERE (SELECT SUM(v.views) FROM Video v WHERE t MEMBER OF v.tags) > 0")
    public List<Tag> findMostViewedTags(int maxTags);
}

The Query is processed and considered valid by Spring, I tested the generated SQL vs my database locally and it returned 2 Tags. In my Code however, I receive the value Null when I call the method findMostViewedTags(100).

The Query lookup strategy is the default "CREATE_IF_NOT_FOUND".

  1. If there are no results found, should the method return an empty list or Null? My desired behavior is to receive an empty list.
  2. Why does the method call return Null instead of a List<Tag> with size() 2?
like image 254
Vjeetje Avatar asked Jan 27 '16 18:01

Vjeetje


People also ask

Does Spring JPA return empty list or null?

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 .

How do I ignore null values in JPA?

You can ignore null fields at the class level by using @JsonInclude(Include. NON_NULL) to only include non-null fields, thus excluding any attribute whose value is null.

Does Crudrepository return null?

Returns: the saved entity; will never be null. Throws: IllegalArgumentException - in case the given entity is null.

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.

How do I use JPA with spring data JPA?

annotation to specify a custom JPQL or native SQL query. Spring Data JPA provides the required JPA code to execute the statement as a JPQL or native SQL query. Your preferred JPA implementation, such as, Hibernate or EclipseLink, will then execute the query and map the result.

How do I use @query in spring?

Select Query 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.

How can I perform CRUD operations in spring data JPA?

At runtime, Spring Data JPA will create your repository implementations with the common CRUD methods. You can then perform CRUD operations without writing a single line of data access code.

Why does list<object> return NULL in Java?

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. For some reason Spring decides to return a Null in that case.


Video Answer


2 Answers

  1. 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.
  2. The problem is that a parameter is given to the method and is not used anywhere in the Query. For some reason Spring decides to return a Null in that case. Solution: remove the unused parameter or use the parameter in the Query.
like image 106
Vjeetje Avatar answered Sep 17 '22 14:09

Vjeetje


I have experienced similar problem. The cause was that I was using Mockito and have not correctly mocked the data with when().

like image 40
user1747134 Avatar answered Sep 17 '22 14:09

user1747134