OneToMany relationship is not working

前端 未结 5 447
Happy的楠姐
Happy的楠姐 2020-12-24 14:35

My Tables:

Product: id, name

Offer: id, value, product_id

Entities:

@Entity
@Table(name=\"product\"         


        
5条回答
  •  一个人的身影
    2020-12-24 15:03

    If you get {IndirectSet: not instantiated} when accessing product.getOffers() than most probably you're executing this code outside of the transaction.

    By default @OneToMany and @ManyToMany relationships are lazy loaded which means that, for better performance, you'll get data fetched only when you want to access it for the first time. This must happen within an active transaction.
    If you don't access this data within this scope than you cannot access this data no more. You should either put your invocation code within the active transaction or change the collection to be eager instead of lazy:

    @OneToMany(mappedBy="product", fetch=FetchType.EAGER)
    

提交回复
热议问题