Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/hibernate/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 我的JPA标准API代码(取自JEE6教程)有什么问题?_Java_Hibernate_Jpa - Fatal编程技术网

Java 我的JPA标准API代码(取自JEE6教程)有什么问题?

Java 我的JPA标准API代码(取自JEE6教程)有什么问题?,java,hibernate,jpa,Java,Hibernate,Jpa,这是我的代码,我甚至无法编译: /** * 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 enti

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

/**
 * 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();
}
编译器说(作为一个编译器我也会这么说):

Finder.java:找不到符号:变量项目
位置:接口javax.persistence.metamodel.EntityType

我做错了什么?

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


解释如何在Hibernate中配置这些类的生成。

但这是我从教程()中获得的内容。教程错了?@Vincenzo:这很令人惊讶,但是教程错了。本教程的作者混淆了动态元模型(通过
getMetamodel()
获得)和静态元模型(由处理工具生成)。只有静态元模型可用于访问属性,例如
Employee.project
。顺便说一句,您的查询也是错误的——这种查询需要JPQL中的
右连接
,但CriteriaAPI不支持右连接。您可以使用子查询重写它,并且
不存在
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>