1

I have 2 entities in spring: Category and Position with a OneToMany relation:

Category.java:

@Table(name = "category")
public class Category {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    int id;

    int parentId;
    String name;

    @OneToMany(mappedBy = "category")
        @JsonManagedReference(value = "referenceCallInCategory")
    List<Position> positionList;

// getters and setters

Position.java:

@Entity
public class Position {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    int id;

    String position;
    String company;
    String description;

    @ManyToOne
//        @JsonBackReference (value = "referenceCallInPosition")
    @JsonIgnoreProperties({"parentId","name","positionList"})
    Category category;

// getters and setters

When I make a post request for a position through postman I get the following warning in my terminal: WARN 15904 --- [nio-8080-exec-2] .c.j.MappingJackson2HttpMessageConverter : Failed to evaluate Jackson deserialization for type [[simple type, class positionsSite.positions.mode l.Position]]: com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot handle managed/back reference 'referenceCallInCategory': no back reference property found from type [collection type; class java.util.List, contains [simple type, class positionsSite.positions.model.Position]]

The strange thing is when making a postrequest for a category the exact same warning occurs but after I make a postrequest for category, the postrequests for position work without a problem. Until I restart the application, then it fails again.

The reason I have @JsonIgnoreProperties instead of @JsonBackReference is that I need the Category.id when I make a get request for position. Given the warning no back reference property found I tried the @JsonBackReference as commented in the code, but this did not change anything.

Any help on how to solve this would be much appreciated

EBolt
  • 11
  • 2
  • Unrelated to the question, but have you been considering why you have setters for (if you have) private variables? If you can both get and set a private variable should it be private? – darclander Sep 08 '20 at 10:59
  • Also, does [this](https://stackoverflow.com/questions/37392733/difference-between-jsonignore-and-jsonbackreference-jsonmanagedreference) post help you or are you familiar with how the different `@Json` works? – darclander Sep 08 '20 at 11:05
  • @darclander it was quit some time since I last worked with the different ```@Json``` properties. I thought I still knew them well enough but the post you mentioned worked wonders for my memory, so thanks :). The ```@JsonManagedReference``` is unnecessary if I use the ```@JsonIgnoreProperties```. – EBolt Sep 08 '20 at 11:27
  • @darclander regarding the getters and setters: you are right to mention this. It is more of a convenience issue while developing. It's not the most neat way of working but some habit of mine to create all getters and setters first and delete the ones I do/should not use together with a larger safety check – EBolt Sep 08 '20 at 11:31
  • Take a look at similar questions: [Jackson serialize problem in @ManyToMany relationship](https://stackoverflow.com/questions/61392551/jackson-serialize-problem-in-manytomany-relationship/61398920#61398920), [Jackson/Hibernate, meta get methods and serialization](https://stackoverflow.com/questions/55383889/jackson-hibernate-meta-get-methods-and-serialization/55386877#55386877). – Michał Ziober Sep 08 '20 at 13:11

1 Answers1

0

As mentioned by @darclander, this post shows clear explanation of the different @Json properties. My mistake was that I still had a @JsonManagedReference which gave the problem. I deleted it and not everything works perfect.

EBolt
  • 11
  • 2
  • It's great to hear my comment helped! If you want to you can set this as the answer to help people with similar problems in the future :). – darclander Sep 08 '20 at 14:21