OneToMany relationship is not working

前端 未结 5 438
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)
    
    0 讨论(0)
  • 2020-12-24 15:14

    This not an error message. The print instruction results in toString() being invoked on the underlying IndirectSet.

    TopLink will place an IndirectSet in the instance variable when the containing domain object is read from the datatabase. With the first message sent to the IndirectSet, the contents are fetched from the database and normal Set behavior is resumed.

    IndirectCollection types are specifically implemented not to instantiate on toString():

    For debugging purposes, #toString() will not trigger a database read.

    However, any other call on the indirect collection, e.g., size() or isEmpty() will instantiate the object.

    The database read is ultimately triggered when one of the "delegated" methods makes the first call to getDelegate(), which in turn calls buildDelegate(), which sends the message getValue() to the value holder. The value holder performs the database read. With the first message sent to the IndirectSet, the contents are fetched from the database and normal Set behavior is resumed.

    See also IndirectList: not instantiated

    0 讨论(0)
  • 2020-12-24 15:17

    Here's what I did

    // force load of the set.
    entity.getSecrets().isEmpty();
    System.out.println(entity.getSecrets());
    
    0 讨论(0)
  • 2020-12-24 15:23

    This solved my issue

    @OneToMany(mappedBy = "columnName", cascade = { CascadeType.ALL}, fetch=FetchType.EAGER)

    0 讨论(0)
  • 2020-12-24 15:24

    I am using Eclipselink 2.5.2 version as my ORM vendor and I have faced this issue in one-to-many JPA mapping while lazy loading the containing entities. The workaround that is working for me is:-

    final List<ContainingEntity> list = Collections.unmodifiableList(new ArrayList<> 
                                        (containerEntity.getContainingEntityList());
    

    The Mapping from Container Entity side is:

    @OneToMany(mappedBy = containerEntity, cascade = CascadeType.ALL, fetchType = FetchType.LAZY)
    private List<ContainingEntity> containingEntityList;
    

    Mapping from Containing Entity side is:

    @ManyToOne
    @JoinColumn(name = "container_entity_id")
    private ContainerEntity containerEntity;
    
    0 讨论(0)
提交回复
热议问题