Could not write content: failed to lazily initialize a collection of role

后端 未结 9 1321
说谎
说谎 2021-02-08 02:48

I have One-To-Many relationship, here is my code

@Entity
@Table(name = \"catalog\")
public class Catalog {

    @Id
    @GeneratedValue(strategy = GenerationType         


        
9条回答
  •  鱼传尺愫
    2021-02-08 03:27

    This is the normal Hibernate behaviour

    In one to many relations, hibernate loads the father entity (Catalog in your case) but it will load the children entities List (List items and List orders in your case) in a LAZY mode

    This means you can't access to these objects because they are just proxies and not real objects

    This is usefull in order to avoid to load the full DB when you execute a query

    You have 2 solution:

    1. Load children entities in EAGER mode (I strongly suggest to you to not do it because you can load the full DB.... but it is something related to your scenario
    2. You don't serialize in your JSON the children entities by using the com.fasterxml.jackson.annotation.JsonIgnore property

    Angelo

提交回复
热议问题