gpt4 book ai didi

hibernate - EmbeddedId 上的 JPA 标准和谓词

转载 作者:行者123 更新时间:2023-12-04 00:08:11 24 4
gpt4 key购买 nike

我有点麻烦。我正在使用 JPA Criteria 进行动态选择(使用标准作为 where 子句具有可选变量...)但我的实体之一有一个 EmbeddedId ,其中包含其中的用户列并且需要检查用户 ID...

这是我的实体。

@Entity
@Table(name = "user_like")
public class UserLike extends AbstractTimestampEntity implements Serializable {

@EmbeddedId
private UserLike.userLikePK userLikePK= new UserLike.UserLikePK();
...
with all the setter and gets
...

@Embeddable
public static class UserLike implements Serializable{
@ManyToOne(cascade = CascadeType.DETACH, fetch = FetchType.LAZY)
@JoinColumn(name = "user_id")
private User user;

... (and an other column, not important for now=
}
}

用户是包含列 userId 的另一个实体

现在这就是我的查询的样子。
Root<Poi> p = cq.from(Poi.class);
Join<Poi,UserLike> ul = p.join("userLike");


List<Predicate> predicates = new ArrayList<Predicate>();
Predicate predicate = cb.like(cb.upper(p.<String>get("name")), "%" + poiName.toUpperCase() + "%");
predicates.add(predicate);

Predicate predicate2 = cb.equal(ul.get("userLikePK.user.idUser"), userId);
predicates.add(predicate);

//multi select because i need some info from p and ul as well....
cq.multiselect(p, ul);

现在 predicate2 显然给我一个错误“无法找到具有给定名称 [userPoiLikePK.user.idUser] 的属性”

有人能告诉我如何在 userID 上添加我的“平等”谓词吗?

ps:这是我要存档的 pa 查询:
SELECT p,ul FROM Poi p LEFT JOIN p.userLike ul WHERE p.name like :name AND ul.user.idUser=:userId

谢谢!!

最佳答案

您需要链接 get。

Predicate predicate2 = cb.equal(ul.get("userLike").get("user").get("idUser"), userId);

关于hibernate - EmbeddedId 上的 JPA 标准和谓词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27061754/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com