0

I'd like to get an entity with collection and it's collections, however I'm facing some problems like 'cannot simultaneously fetch multiple bags' or empty result sets where I know it should contain entries. The whole thing is about diets, so I've got following entities:

public class Diet extends BaseEntity{
@NotEmpty
@Column(unique = true)
private String name;
@OneToMany(cascade=CascadeType.ALL, mappedBy = "diet")
@LazyCollection(LazyCollectionOption.FALSE)
private List<DietEntry> dietEntry = new ArrayList<>();

@Entity
@Table(name = "diet_entry")
public class DietEntry extends BaseEntity{
@ManyToOne(cascade=CascadeType.ALL)
@JoinColumn(name = "dietId")
private Diet diet;
@ManyToMany(cascade=CascadeType.ALL)
@LazyCollection(LazyCollectionOption.FALSE)
private List<Meal> meals = new ArrayList<>();
@ManyToMany(cascade=CascadeType.ALL)
@LazyCollection(LazyCollectionOption.FALSE)
private List<ProductQuantity> products = new ArrayList<>();

I'd like to get Diet with all it's DietEntries and all Products and Meals from DietEntry. I can get Diet easily, but I've got problem with getting the collections, so I've made following query:

    Query query = this.em.createQuery("SELECT distinct(de) FROM     DietEntry de join de.products dep join de.meals dem WHERE de.diet = :diet");
    query.setParameter("diet", diet);
    return query.getResultList();

However it returns empty result set. How to solve this?

ZZ 5
  • 1,744
  • 26
  • 41
  • 1
    See previous question/answer: http://stackoverflow.com/questions/1346181/using-distinct-in-jpa – Peter Jan 20 '15 at 16:25
  • I've removed the distinct constraint from the query, but it still gives empty result set. – ZZ 5 Jan 20 '15 at 17:37

1 Answers1

0

Correct answer: using sets instead of lists.

ZZ 5
  • 1,744
  • 26
  • 41