QueryException:必须提供引用类

时间:2017-05-29 10:53:52

标签: java mysql jpa orm

我正在制作一个程序来组织我的无聊。我最近刚开始,所以我还没有开始使用GUI。该程序在视频/图书上运行良好,但现在我添加了Game我得到了queryException。有人知道我做错了吗?

错误:

  

引起:异常[EclipseLink-6029](Eclipse Persistence Services - 2.5.2.v20140319-9ad6abd):org.eclipse.persistence.exceptions.QueryException   例外描述:必须提供引用类。

发生错误的行:

public <E> List<E> getAll(Class<E> eClass){return manager.createQuery(manager.getCriteriaBuilder().createQuery(eClass)).getResultList(); }

为不同的实体类型调用该行

for (Class eClass: Project.getAllItemTypes())
            repos.put(eClass, new Repository(persistenceAdministrator, eClass));

实体类型

public static Class[] getAllItemTypes(){
        return new Class[]{Movie.class, TvShow.class, ComedyShow.class, Book.class, Comicbook.class, Manga.class, Game.class};
    }

GameTask

的create-sql
CREATE TABLE Game(
    id INT NOT NULL AUTO_INCREMENT,
    stateStr VARCHAR(50) NOT NULL,
    title VARCHAR(100) NOT NULL,
    releaseDate DATE NOT NULL,
    gameId INT NULL,
  PRIMARY KEY (id),
  CONSTRAINT FK_gameId_game
    FOREIGN KEY (gameId)
    REFERENCES Game (id));

CREATE TABLE Task(
    id INT NOT NULL AUTO_INCREMENT,
    title VARCHAR(255) NOT NULL,
    completed BOOLEAN NOT NULL,
    gameId INT NOT NULL,
  PRIMARY KEY (id),
  CONSTRAINT FK_gameId_task
    FOREIGN KEY (gameId)
    REFERENCES Game (id));

游戏类:

@Entity
public class Game extends Item implements Serializable{

  @OneToMany
  private List<Game> children = new ArrayList<>();
  @OneToMany
  private List<Task> tasks = new ArrayList<>();
}

项目类

@MappedSuperclass
public abstract class Item extends IEntity implements Serializable{

   @Transient
    private State state;
    @Column(nullable=false)
    private String stateStr;
    @Basic(optional = false)
    @Column(name = "releaseDate")
    @Temporal(TemporalType.DATE)
    private Calendar releaseDate;
    @Column(nullable=false)
    private String title;

IEntity类

@MappedSuperclass
public abstract class IEntity extends Object implements Serializable{

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int id;

的persistence.xml persistence.xml

2 个答案:

答案 0 :(得分:1)

通常,您定义自连接的方式如下:

<强>游戏

@Entity
public class Game extends Item implements Serializable{

  @ManyToOne
  @JoinColumn(name = "gameId")
  private Game parent;

  @OneToMany(mappedBy = "parent")
  private List<Game> children = new ArrayList<>();

  @OneToMany(mappedBy = "game")
  private List<Task> tasks = new ArrayList<>();
}

<强>任务

@Entity
public class Task{

    @ManyToOne
    @JoinColumn(name = "gameId")
    private Game game;
}

并且不要忘记@ManyToOne方面的Task映射。

答案 1 :(得分:0)

我找到了解决方案here

问题在于这个方法:

public boolean isCompleted(){
        //return tasks.stream().allMatch((task) -> task.isCompleted());
        for (Task task: tasks) if (!task.isCompleted()) return false;
        return true;
    }

显然,JPA实体类在java 8 lambdas上运行得不好。 我用旧学校取代了这个方法:P代码,它的工作原理。 无论如何,谢谢你的帮助。