Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hibernate returning PersistentBag instead of List

I have following relationship b.w two entities given below ,when I get OutletProductVariety object from repository ,the price is coming in PersistentBag and not as a List even after using fetchtype Eager.

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

  @ManyToOne
  @JoinColumn(name = "varietyId")
  Variety variety;

  @OneToMany(mappedBy = "outletVariety", fetch = FetchType.EAGER)
  List<Price> price;
}

And

@Entity
public class Price {

  @Id
  @GeneratedValue(strategy=GenerationType.AUTO)
  Long Id;

  @ManyToOne
  @JoinColumn(name="outletVareityId")
  private OutletProductVariety outletVariety;

  private Double price;
}

How can I get a List of prices rather then PersistentBag?

like image 938
meGaMind Avatar asked Aug 13 '18 13:08

meGaMind


1 Answers

Have a look at Hibernates PersistentBag

An unordered, unkeyed collection that can contain the same element multiple times. The Java collections API, curiously, has no Bag. Most developers seem to use Lists to represent bag semantics, so Hibernate follows this practice.

public class PersistentBag extends AbstractPersistentCollection implements List

protected List bag;

It implements java.util.List; so it is basically a List and wraps your List internally

It is just Hibernates way to represent your List.

like image 126
mrkernelpanic Avatar answered Nov 30 '22 12:11

mrkernelpanic