1

I've seen this problem pop up everywhere, and while i followed their solutions and it fixed my problem, i don't really understand what is happening. My Entities:

@Entity
public class ItemInOrder {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    Long id;

    @ManyToOne
    Bill bill;

    String comment;
}

@Entity
@JsonIdentityInfo(generator=ObjectIdGenerators.UUIDGenerator.class, property="@id")
    public class Bill {
        @Id
        @GeneratedValue(strategy = GenerationType.IDENTITY)
        int id;

        @OneToOne
        Table table;



        @OneToMany(mappedBy = "bill")
        List<ItemInOrder> itemi_vo_naracka = new ArrayList<>();
    }

@Entity
@JsonIdentityInfo(generator=ObjectIdGenerators.UUIDGenerator.class, property="@id")
public class Table {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    int id;

    @OneToOne
    Bill order_na_ovaa_masa;
}

My error comes when i call a basic JPA repository method to find all the ItemsInOrder:

return itemInOrderService.getAllItemsInOrder();


The exception says that the infinite loop is happening between Table and Bill:
Infinite recursion (StackOverflowError) (through reference chain: bla.bla.domain.Bill["table"]->bla.bla.domain.Table["order_na_ovaa_masa"]-> and so and so on..
  • So can someone explain to me what was happening before i added the @JsonIdentityInfo(generator=ObjectIdGenerators.UUIDGenerator.class, property="@id") above my entites, and what is happening after that.

    I've read a few explanations but i don't really understand whats happening with all that serialization and deseralization. If the explanation is too long, a guide or link to a post where it is explained nicely would also do(for a complete beginner in all of this). Thanks!

Emperor
  • 335
  • 3
  • 11
  • Are you referring to this answer being confusing? https://stackoverflow.com/questions/45783753/spring-data-jpa-onetoone-annotation-infinite-recursion-error?rq=1 – Compass Feb 25 '19 at 21:24
  • I am referring mostly to the basic concept of serialization and how, in my situation, from ItemsInOrder it went and got stuck in between Table and Bill. – Emperor Feb 25 '19 at 21:32
  • This probably explains it better than I can in a comment. https://www.toptal.com/javascript/bidirectional-relationship-in-json – Compass Feb 25 '19 at 21:37
  • Basically, in Java, Bill has a reference to Table and vice versa.Two objects with references. When deserializing, Jackson sees a Bill with a Table, and creates a new Bill that's identical to the old Bill, and gives it to the Table, forever, because it's just following the logic of the JSON and doesn't see self-referencing by default. With the identity, Jackson can map it to that reference instead, so the Table in Bill is mapped back to the original Bill, rather than to a new Bill. Hopefully this one makes slightly more sense. – Compass Feb 25 '19 at 21:42
  • Also, from: https://stackoverflow.com/questions/37302816/how-to-use-jsonidentityinfo-with-circular-references `@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "id")` this could also replace your current @id as your objects do already have (hopefully unique) ids attached. – Compass Feb 25 '19 at 21:51
  • Hey the second link you sent me has been great and helped me fully understand what was going on. If you wanna put it in answer i will gladly give you accepted answer :) – Emperor Feb 26 '19 at 16:14
  • I will mark as a duplicate of the original question as the link came from in there, though it's hidden in the middle. – Compass Feb 26 '19 at 16:35
  • Possible duplicate of [Spring Data JPA @OneToOne annotation Infinite Recursion Error](https://stackoverflow.com/questions/45783753/spring-data-jpa-onetoone-annotation-infinite-recursion-error) – Compass Feb 26 '19 at 16:36

1 Answers1

0

Look here, you have to annotate your list with :

@OneToMany(mappedBy = "bill")
@JsonIgore
List<ItemInOrder> itemi_vo_naracka = new ArrayList<>();
AdagioDev
  • 556
  • 5
  • 12