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

后端 未结 9 1313
说谎
说谎 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:38

    Here is my solution for this task with Hibernate. I marked hibernate releation with @JsonIgnore and use custom field for jackson, in which I check if the field is loaded. If you need serialize collection to json then you should manualy call collection getter during hibernate transaciton.

    @JsonIgnore
    @OneToMany(mappedBy = "myorder")
    private List orderItems = new ArrayList<>();
    
    @JsonProperty(value = "order_items", access = JsonProperty.Access.READ_ONLY)
    private List getOrderItemsList() {
    
        if(Hibernate.isInitialized(this.relatedDictionary)){
            return this.relatedDictionary;
        } else{
            return new ArrayList<>();
        }
    }
    
    @JsonProperty(value = "order_items", access = JsonProperty.Access.WRITE_ONLY)
    private void setOrderItemsList(List orderItems) {
        this.orderItems = orderItems;
    }
    

提交回复
热议问题