• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Get Request is creating infinite results on List- same item showing multiple times

 
Ranch Hand
Posts: 462
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a Spring Boot project that is a parking pass app where one owner can have multiple vehicles.   However, when I do a post request of a vehicle and add the owner to the vehicle through the foreign key, once I do a get request for that owner, the list of vehicles is infinite.  The same vehicle shows up on the same list an infinite amount of times until the project times out.  

Here is what I'm talking about.  

This is the owner class.  





This is the vehicle class.  







I create an owner through the post request with my name.   I create a vehicle and reference the owner id.   When I get the owner ID through the get request, this comes up.





{
   "id": 22,
   "firstName": "Nathan",
   "lastName": "Milota",
   "vehicles": [
       {
           "id": 23,
           "plateNum": "CTQ1057",
           "passNum": "HF5678",
           "owner": {
               "id": 22,
               "firstName": "Nathan",
               "lastName": "Milota",
               "vehicles": [
                   {
                       "id": 23,
                       "plateNum": "CTQ1057",
                       "passNum": "HF5678",
                       "owner": {
                           "id": 22,
                           "firstName": "Nathan",
                           "lastName": "Milota",
                           "vehicles": [
                               {
                                   "id": 23,
                                   "plateNum": "CTQ1057",
                                   "passNum": "HF5678",
                                   "owner": {
                                       "id": 22,
                                       "firstName": "Nathan",
                                       "lastName": "Milota",
                                       "vehicles": [
                                           {
                                               "id": 23,
                                               "plateNum": "CTQ1057",
                                               "passNum": "HF5678",
                                               "owner": {
                                                   "id": 22,
                                                   "firstName": "Nathan",
                                                   "lastName": "Milota",
                                                   "vehicles": [
                                                       {
                                                           "id": 23,
                                                           "plateNum": "CTQ1057",
                                                           "passNum": "HF5678",
                                                           "owner": {
                                                               "id": 22,
                                                               "firstName": "Nathan",
                                                               "lastName": "Milota",
                                                               "vehicles": [
                                                                   {


and then it goes on and on and doesn't stop.
 
Rancher
Posts: 4801
50
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is why it's usually not a good idea to use your data model as your view model.
As you can see in your classes, an Owner has a List<Vehicle> and each Vehicle has an Owner.
So when this is turned into JSON, the transformer walks down the tree, taking the List<Vehicle>, getting the first Vehicle, seeing an Owner, grabbing the List<Vehicle>...and on and on.

It's a lot neater to remove the connection and having a view model on its own.
 
Nathan Milota
Ranch Hand
Posts: 462
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When I try just printing the list of vehicles to the console, it just shows an empty list no matter how many vehicles are in the list.
 
Dave Tolls
Rancher
Posts: 4801
50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How are you trying to print it?
 
Nathan Milota
Ranch Hand
Posts: 462
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Dave Tolls wrote:How are you trying to print it?



When I did a put request to update the owner, I was having it print the owners vehicles at the same time.  It only prints [] every time, but it prints the endless list when I do a get request.  
 
Nathan Milota
Ranch Hand
Posts: 462
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Dave Tolls wrote:

It's a lot neater to remove the connection and having a view model on its own.



Are you saying I did the hibernate wrong?   I thought that was how the classes are supposed to be made.
 
Dave Tolls
Rancher
Posts: 4801
50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Nathan Milota wrote:
When I did a put request to update the owner, I was having it print the owners vehicles at the same time.  It only prints [] every time, but it prints the endless list when I do a get request.  



It is quite possible that the List is lazy loaded, and was not populated when you printed it out during the PUT request.

Nathan Milota wrote:Are you saying I did the hibernate wrong?   I thought that was how the classes are supposed to be made.



No, the Hibernate is fine.
But you essentially have two models, and you're trying to squeeze them as a single model.

There's the view model (the mvc bit) and the domain model (the database bit).
Since they generally have different needs it can cause issues trying to use a single model to represent both, or in your case use the domain model for your view.

There are (I think) annotations you can supply to many JSON engines that can tell them what to skip (eg not converting the Owner entry in Vehicle), but then you would be adding view information to your domain classes.
And, of course, should you choose to have a page where you can show a Vehicle, and have a field for their Owner, then the above annotation would be wrong, and you'd then have to jump through hoops to try and get the information you need.

So, to summarise, it's generally simpler to build a model to represent your view, without reference to the underlying domain (database) model.  Then a simple transformer in between to turn one into the other.

Hopefully that makes some sense?
 
Nathan Milota
Ranch Hand
Posts: 462
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Do you have an example of how to build a model to represent the view?  
 
Nathan Milota
Ranch Hand
Posts: 462
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can someone at least help me get started?
 
Bartender
Posts: 2419
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Nathan,
How do you add your vehicles to the owner?
 
Nathan Milota
Ranch Hand
Posts: 462
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I was adding them either through the database or a JSON post request by creating a vehicle and adding the owner id.
 
Himai Minh
Bartender
Posts: 2419
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How about setting up the bi-directional relationship using the setter in your Java code?
 
Nathan Milota
Ranch Hand
Posts: 462
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Himai Minh wrote:How about setting up the bi-directional relationship using the setter in your Java code?



Which class would I do that in?  How does that fix the problem?
 
Himai Minh
Bartender
Posts: 2419
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
We usually have a class that perform saving the entities and building relationship among entities.
Here is an example : https://dzone.com/articles/introduction-to-spring-data-jpa-part-4-bidirection
You may want to do something like the RoleService's addRole method.
 
Nathan Milota
Ranch Hand
Posts: 462
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Himai Minh wrote:We usually have a class that perform saving the entities and building relationship among entities.
Here is an example : https://dzone.com/articles/introduction-to-spring-data-jpa-part-4-bidirection
You may want to do something like the RoleService's addRole method.



The @JsonIgnore seemed to take away the error I was getting.
 
Nathan Milota
Ranch Hand
Posts: 462
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I added the following to my Vehicle class.  



However, it is causing a 500 Internal Server Error when trying to get all vehicles.
 
Dave Tolls
Rancher
Posts: 4801
50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And what is the exception?
There should be something in your logs.

I would also say, yes @ignoreJson works, but you now have a model that is trying to be all things to all people.
 
Nathan Milota
Ranch Hand
Posts: 462
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Dave Tolls wrote:And what is the exception?
There should be something in your logs.

I would also say, yes @ignoreJson works, but you now have a model that is trying to be all things to all people.




One of my vehicles had a null owner so it threw an exception.

Do you have a better suggestion than the @JsonIgnore?
 
Marshal
Posts: 28226
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Nathan Milota wrote:One of my vehicles had a null owner so it threw an exception.



That was my guess. I guessed that because your getOwnerName and setOwnerName methods are (to me) distressingly asymmetric. If getOwnerName returns data extracted from the Owner, then surely setOwnerName should set data which belongs to the Owner? Having setOwnerData set data in the Vehicle which is apparently not used, that seems weird to me too.
 
Dave Tolls
Rancher
Posts: 4801
50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Nathan Milota wrote:

Do you have a better suggestion than the @JsonIgnore?



In my first post.
You have two models.
One for the database (your current model) and one for the various ways you display that data.

They really are two different models.
 
Nathan Milota
Ranch Hand
Posts: 462
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Dave Tolls wrote:

Nathan Milota wrote:

Do you have a better suggestion than the @JsonIgnore?



In my first post.
You have two models.
One for the database (your current model) and one for the various ways you display that data.

They really are two different models.



Are you talking about the front end then?  Would that be a model to display information?  You'll have to forgive me.  I'm new to web development.
 
Dave Tolls
Rancher
Posts: 4801
50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes.

This is because the database model is rarely a good fit for what you want to display on the front end.

So in your current situation you've stuck a JsonIgnore on the Owner in the Vehicle class (at a guess).
What if you wanted a screen that showed the Vehicle, as well as the Owner details. You can't just use the Vehicle class as json will ignore the Owner.
So you're already having to work around the database model.

Indeed, it will also ignore the Owner when you parse any json representing that Vehicle, should you end up having to do that.

It works in some limited cases, but it really is a bit of sticky tape over the problem rather than an actual fix.
 
What's wrong? Where are you going? Stop! Read this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic