尝试构建此查询,但我想我错过了一些东西。 以下是相关课程:
以下是我的实体的相关部分(非常简单):
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;
}
我想在特定的日子里简单地提取特定用户的日常饮食习惯:
public UserDiet findByUserNameAndDay(String userName, long day)
但似乎无法使其发挥作用。 我应该为此构建2个不同的CriteriaQuery对象吗? 如果我创建2个不同的谓词,我该如何使用它们?如果有人可以帮我解决这个简单的问题,那就不用多了(我是JPA标准API的新手)。
感谢!!!
答案 0 :(得分:0)
通过添加到DailyDiet类来解决它:
@ManyToOne
protected Diet userDiet;
然后在我的DailyDietDao中使用以下谓词:
Predicate userNameCondition = qb.equal(p.get(DailyDiet_.userDiet).get(UserDiet_.user).get(BaseUser_.userDetails).get(UserDetails_.userName),
userName);