Win a copy of Secure Financial Transactions with Ansible, Terraform, and OpenSCAP this week in the Cloud/Virtualization forum!
  • 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
  • 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

@JsonIgnore is stopping me from getting and posting foreign keys, but program crashes without it

 
Ranch Hand
Posts: 442
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I've been using the @JsonIgnore because when I get a list of all users, there is a foreign key of company to match the company of the user, and when I do a get request, without @JsonIgnore, it creates an infinite loop that crashes the program.

However, when doing a post request, I cannot post the company to the user with the @JsonIgnore in there, because it comes up null.  

Is there a solution to this?   I cannot find it.
 
Nathan Milota
Ranch Hand
Posts: 442
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I solved part of the problem by including



However, I still need to be able to read the company, because when trying to get values from it in Angular, it is coming back undefined because it can't read it.   I don't know what to do.
 
Marshal
Posts: 3460
493
Android Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What is the relationship between Users and Companies?
 
Nathan Milota
Ranch Hand
Posts: 442
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The User class has a ManyToOne and the Company class has a OneToMany.   I showed the classes in my other thread related to Angular and Typescript that's causing an undefined error.
 
Ron McLeod
Marshal
Posts: 3460
493
Android Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Assuming that a Company that the User will be a member of already exists, wouldn't you just want to provide an identifier for the Company, and not all of the company's attributes when creating a new User?

Also, why does Company have a collection of the Users?  That is going to create a kind of circular reference which is probably the source of your infinite loop.
 
Nathan Milota
Ranch Hand
Posts: 442
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What are you saying?   Ditch the entire OneToMany and ManyToOne relationship, and just give a company number to a user?
 
Nathan Milota
Ranch Hand
Posts: 442
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I just don't get it, if the OneToMany relationship always behaves this way, why would anyone ever use it?
 
Saloon Keeper
Posts: 12826
279
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You've run into the classic trap of using the same class to model your database entry and your service response.

Don't do this.

It may seem like you're duplicating code, but the responsibilities of your web service layer and your persistence layer should be kept completely separate.
 
Stephan van Hulst
Saloon Keeper
Posts: 12826
279
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Nathan Milota wrote:I just don't get it, if the OneToMany relationship always behaves this way, why would anyone ever use it?


That's not what Ron is saying. He's saying you shouldn't have a circular reference in your requests/responses.

The solution, as I posted above, is to create one set of classes for your requests/responses, a different set of classes for your database, and one more set of classes for your business layer.

This may seem like overkill. Trust me, it's not.
 
Nathan Milota
Ranch Hand
Posts: 442
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't understand what that means.   Do you have an example?
 
Stephan van Hulst
Saloon Keeper
Posts: 12826
279
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Business layer:
Persistence layer:
Web service layer:
The User class from the business layer has no idea about the web service layer or the persistence layer. It is the primary class that models a user, and if you want to perform logic on a user, you will want to do it with this class. It may contain circular references: A User references a Company and the referenced Company may refer back to the User.

The UserEntity class from the persistence layer is very similar to the User class from the business layer. The main reason it's separate is because it's not easy to enforce class invariants on JPA entities. Therefore you only use it when you want to persist your user to the database, or when you want to retrieve it from the database. Immediately convert it to a User object, and don't perform logic on it directly.

The UserData class from the web service layer is a flattened object model that you can use to (de)serialize requests and responses to the web service. There are two major differences with the other two classes: First, it doesn't contain a reference to a company instance, because you should not have circular references in your requests and responses. Secondly, it doesn't contain an ID that was generated by the DBMS. You must not expose such IDs to the outside world.
 
A "dutch baby" is not a baby. But this tiny ad is baby sized:
SKIP - a book about connecting industrious people with elderly land owners
https://coderanch.com/t/skip-book
reply
    Bookmark Topic Watch Topic
  • New Topic