Reputation: 5404
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".
Null
instead of a List<Tag>
with size() 2?Upvotes: 33
Views: 86560
Reputation: 2482
I have experienced similar problem. The cause was that I was using Mockito and have not correctly mocked the data with when()
.
Upvotes: 7
Reputation: 5404
List<Object>
is the return value of the method in the defined interface, the method should never return Null
.Upvotes: 48