gpt4 book ai didi

java - 我的 JPA Criteria API 代码(取自 JEE6 教程)有什么问题?

转载 作者:行者123 更新时间:2023-11-29 06:19:10 25 4
gpt4 key购买 nike

这是我的代码,我什至无法编译:

/**
* Find a project that has NO employee assigned yet.
*/
public Project findEmptyProject() {
// getting criteria builder
CriteriaBuilder cb = this.em.getCriteriaBuilder();
// gathering meta information about left-joined entity
Metamodel m = this.em.getMetamodel();
EntityType<Employee> Employee_ = m.entity(Employee.class);
// creating query
CriteriaQuery<Project> cq = cb.createQuery(Project.class);
// setting query root for the query
Root<Project> project = cq.from(Project.class);
// left-joining with another employees
Join<Employee, Project> projects = project.join(
Employee_.project,
JoinType.LEFT
);
// instructing the query to select only projects
// where we have NO employees
cq.select(project).where(Employee_.id.isNull());
// fetching real data from the database
return this.em.createQuery(cq).getSingleResult();
}

我的模型类是:

public class Employee {
@Id private Integer id;
@ManyToOne private Project project;
private String name;
}

public class Project {
@Id private Integer id;
private String name;
}

编译器说(作为编译器我会这么说):

Finder.java: cannot find symbol: variable project
location: interface javax.persistence.metamodel.EntityType<com.XXX.Employee>

我做错了什么?

最佳答案

当您使用 Employee_.project 语法时,Employee_ 必须是自动生成的元模型类,而不是 EntityType通过 Metamodel.entity() 获得。

Hibernate JPA 2 Metamodel Generator manual解释了如何在 Hibernate 中配置这些类的生成。

关于java - 我的 JPA Criteria API 代码(取自 JEE6 教程)有什么问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3857043/

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