• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Rob Spoor
  • Henry Wong
  • Liutauras Vilda
Saloon Keepers:
  • Tim Moores
  • Carey Brown
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
Bartenders:
  • Frits Walraven
  • Himai Minh
  • Jj Roberts

Spring Data JPA: Cannot Invoke Data Transfer Object Because it's null

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For some reason I'm getting null returned from my database through Spring data JPA and I'm not quite sure why because when I run the query in Postgres it works fine. I'd really appreciate someone taking a look at the code if they've a few minutes to spare.

The error is:

   

which implies that nothing is getting returned from the database. This is the controller:

     


This is the corresponding query in the repository:

 


The data transfer object I'm using

 





Let me know if you need more code or have any questions about it.

Thanks again
 
Bartender
Posts: 2061
13
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Maybe, your return type should be Collection<Friend> instead of FriendList?
Reference: https://www.baeldung.com/spring-data-jpa-query
 
Sheriff
Posts: 22116
114
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Himai Minh wrote:Maybe, your return type should be Collection<Friend> instead of FriendList?
Reference: https://www.baeldung.com/spring-data-jpa-query


Because the name is not descriptive, it's not a list but a simple POJO.

@Bryan, are you sure this user exists in the database? Because if it doesn't, then you'll definitely get a null from the repository.
 
Bryan Carty
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@Rob, first thanks for answering. I've been trying stackoverflow to no avail. To answer your question, yeah it's definetly in the database. If you've any more ideas about what is causing the error I'd really love to hear them.

Also, just to clarify. Despite it being called FriendList it doesn't return a list. Poor naming on my part. I have a different query that returns a list as shown below but in this case I just want to return a single row of data from the database. Also, I want to note that the below query that returns a list works fine.

 
Rob Spoor
Sheriff
Posts: 22116
114
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What happens if your method returns Optional<FriendList>?
 
Bryan Carty
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@Rob, I got it working. Thanks for the help anyway though it was really appreciated. I think it may have been to do with the SQL Query and the JOIN I was using. If I figure out what was causing the problem for sure I'll leave a reply here to make sure anyone else who runs into the same problem can see why it happened to me.

Thanks again
 
Rob Spoor
Sheriff
Posts: 22116
114
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ah yes, I see a possible issue with that query. If the user has no notes, then both queries will lead to zero results. I can think of two possible fixes:

1. Use a sub-select in the SELECT part:


2. Use a LEFT OUTER JOIN, and explicitly count on the right table (counting * will always have at least 1 result):
 
Bryan Carty
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yeah, doing a left join fixed it. Thanks again. Your help was really appreciated.
 
Rob Spoor
Sheriff
Posts: 22116
114
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're welcome
 
Do the next thing next. That’s a pretty good rule. Read the tiny ad, that’s a pretty good rule, too.
SKIP - a book about connecting industrious people with elderly land owners
https://coderanch.com/t/skip-book
reply
    Bookmark Topic Watch Topic
  • New Topic