meGaMind
meGaMind

Reputation: 105

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?

Upvotes: 8

Views: 12715

Answers (1)

mrkernelpanic
mrkernelpanic

Reputation: 4451

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.

Upvotes: 6

Related Questions