小编Tom*_*Tom的帖子

使用嵌入式 id 进行 Hibernate 标准查询获取

我有这Entity门课:

@Entity
public class Registered implements Serializable {

    @Id
    public RegisteredId id;
}
Run Code Online (Sandbox Code Playgroud)

有了这个EmbeddedId

@Embeddable
public class RegisteredId implements Serializable {
    @ManyToOne
    public User user;
    @ManyToOne
    public Tournament tournament;

}
Run Code Online (Sandbox Code Playgroud)

我正在尝试获取Criteria queryUserTournament因为我需要读取它们的一些属性:

CriteriaBuilder builder = em.getCriteriaBuilder();
CriteriaQuery<Registered> criteria = builder.createQuery(Registered.class);
Root<Registered> root = criteria.from(Registered.class);
root.fetch("id.user", JoinType.LEFT);
root.fetch("id.tournament", JoinType.LEFT);
criteria.where(builder.equal(root.get("id.tournament.id"), idTournament));
List<Registered> registereds = em.createQuery(criteria).getResultList();
Run Code Online (Sandbox Code Playgroud)

但我收到一个错误:

Execution exception[[CompletionException: java.lang.IllegalArgumentException: Unable to locate Attribute  with the the given name [id.user] on this …
Run Code Online (Sandbox Code Playgroud)

java hibernate jpa hibernate-criteria

5
推荐指数
1
解决办法
1343
查看次数

标签 统计

hibernate ×1

hibernate-criteria ×1

java ×1

jpa ×1