Tried building this query but I guess I'm missing something. Here are the involved classes:
- User
- Diet
- DailyDiet
Here are the relevant parts of my entities (A pretty simple one):
public class User {
@Column(name = "User_Name", nullable = false, length = 15, unique = true)
public String userName;
@OneToOne(cascade = CascadeType.ALL, fetch = FetchType.EAGER, optional = true)
@PrimaryKeyJoinColumn
public Diet userDiet;
}
public class Diet {
@OneToOne(optional = false)
@JoinColumn(name = "User_Id")
protected User user;
@OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL, orphanRemoval = true)
@JoinTable(name = "UsersDiets_DailyDiets", joinColumns = @JoinColumn(name = "User_Id"), inverseJoinColumns = @JoinColumn(name = "DailyDiet_Id"))
protected Collection<DailyDiet> userDailyDiets;
}
public class DailyDiet {
@Column(name = "Day")
protected long day;
}
I would like to simply pull a user daily diet of a specific user in a specific day:
public UserDiet findByUserNameAndDay(String userName, long day)
But can't seem to make it work. Should I build 2 different CriteriaQuery objects for that? If I create 2 different Predicates, how do I use those? If someone can help me with this simple query ill be thankful (I'm quite new to JPA criteria API).
Thanks!!!