2

I'm working on a program to organize my boredom. I started recently so I haven't began on the GUI yet. The program was working fine with videos/book but now that I added Game I get a queryException. Does someone know what I did wrong?

The error:

Caused by: Exception [EclipseLink-6029] (Eclipse Persistence Services - 2.5.2.v20140319-9ad6abd): org.eclipse.persistence.exceptions.QueryException Exception Description: A reference class must be provided.

The line where the error takes place:

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

Where the line gets called for the different entity types

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

The Entity types

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

The create-sql for Game and Task

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));

Game Class:

@Entity
public class Game extends Item implements Serializable{

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

Item class

@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 class

@MappedSuperclass
public abstract class IEntity extends Object implements Serializable{

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

persistence.xml persistence.xml

Lennert Hofman
  • 585
  • 6
  • 21

2 Answers2

1

Normally the way you define the self-join is something like:

Game

@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<>();
}

Task

@Entity
public class Task{

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

and dont forget that @ManyToOne mapping on the Task side.

Maciej Kowalski
  • 25,605
  • 12
  • 54
  • 63
0

I found the solution here

The problem was this method:

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

Apparently JPA entity classes don't work so well with java 8 lambdas. I replaced the method with old school :P code and it works. Thanks anyway for helping me.

Lennert Hofman
  • 585
  • 6
  • 21