question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

getResultList returning a list containing one object of null values rather than an empty list

See original GitHub issue

Description

We are querying for a list of sub views using the function EntityViewManager.applySetting(EntityViewSetting setting, CriteriaBuilder criteriaBuilder, String entityViewRoot). When these sub views exist in the db, we properly receive the results as a list. What we are having trouble with is when there aren’t any of these sub views in the db.

What we are doing:

 CriteriaBuilder<Link.class> cb = cbf.create(em, Link.class).from(ClientProfile.class);
 cb = cb.where("org.id").eq(orgId);
 CriteriaBuilder<LinkView.class> linkBuilder = evm.applySetting(EntityViewSetting.create(LinkView.class), cb, "links");
 return linkBuilder.getQuery().getResultList();

Expected behavior

Expected result when links exist on a client profile:

[{"url": "value", "name": "value"}]

Actual result when links exist on a client profile:

[{"url": "value", "name": "value"}]

Expected result when links do not exist on a client profile:

[]

Actual result when links do not exist on a client profile:

[{"url": "null", "name": "null"}]

Actual behavior

When there are no values in the db for the sub views we query for, we receive a result list with one element with null fields.

Steps to reproduce

As mentioned above, if we query for a list of sub views and none exist, we should see this behavior.

Provided the setup/query we are doing above.

Environment

Version: 1.6.5 JPA-Provider: Hibernate 5.4.32.Final (Spring Data JPA 2.4.13) DBMS: PostgreSQL 13.3 Application Server: Spring Boot 2.4.13

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:9 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
beikovcommented, Feb 7, 2022

We do use a list index for links using @OrderColumn and that doesn’t seem to help in this situation.

Oh, right, I was mixing up things. That won’t help when you use a sub path. Well, then your only way is to use the wrapper view for now.

1reaction
beikovcommented, Feb 7, 2022

Unfortunately, the only way to avoid the “empty object” for now, is to use a list index by annotating @OrderColumn on the entity collection. A possible alternative is to use a simple wrapper view:

@EntityView(ClientProfile.class)
public interface ClientProfileLinkViews {
    @IdMapping
    Long getId();
    List<LinkView> getLinks();
}
 CriteriaBuilder<Link.class> cb = cbf.create(em, Link.class).from(ClientProfile.class);
 cb = cb.where("org.id").eq(orgId);
 CriteriaBuilder<ClientProfileLinkViews> linkBuilder = evm.applySetting(EntityViewSetting.create(ClientProfileLinkViews.class), cb);
 return linkBuilder.getQuery().getResultList().get(0).getLinks();
Read more comments on GitHub >

github_iconTop Results From Across the Web

Can javax.persistence.Query.getResultList() return null?
I believe returning null instead of an empty list is not what is intended by the spec as it otherwise makes it quite...
Read more >
empty result return a list of size 1 with a null element - Bugs
The first query return a List of size 1 containing a null element The second return (as supposed) an empty List I think...
Read more >
JPA - Hibernate - query.getResultList() returns 3 null values.
I am new to JPA and developing my 1st JPA application with using MySQL DB and hibernate as persistence provider. I created a...
Read more >
getResultList returns empty list [SOLVED] - Hibernate Forums
Unable to find [jdbc]. The second problem is that getResultList returns an empty list after a query I'm sure should return 20 rows....
Read more >
Query returns no rows. In JPA would return empty List or Null
I am pretty positive that Hibernate's implementation of JPA returns an empty list.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found