Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error on compiling query: The abstract schema type 'entity' is unknown

I'm developing a game with a database connection, and I use JPA to persist my data. Here is my Game entity :

@Entity
@Table(name = "game")
public class Game implements Serializable {
private static final long serialVersionUID = 1L;

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "game_id")
private int id;

@Column(name = "name")
private String name;

@Column(name = "nbTurns")
private int nbTurns;

@Column(name = "playedOn")
@Temporal(TemporalType.TIMESTAMP)
private Date playedOn;

@ElementCollection(fetch = FetchType.EAGER)
@CollectionTable(name = "game_humans", joinColumns = @JoinColumn(name = "game_id"))
@MapKeyColumn(name = "human_id")
@Column(name = "isDead")
private Map<Human, Boolean> humans;

And here is my Human entity :

@Entity
@Table(name = "human")
public class Human implements Serializable {

private static final long serialVersionUID = 1L;

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int id;
@Column(name = "name")
private String name;
@OneToOne
private Building building;

To get the list of all the humans stored in the DB, I use this DAO, which is working very well and gets also the Building entity :

public class HumanDAO implements DAO<Human> {

// ...
public List<Human> getAllHumans() {
    TypedQuery<Human> query = em.createQuery("SELECT h FROM human h ORDER BY h.name", Human.class);
    return query.getResultList();
}

The problem is when I try to do the same to get the list of all the games with the JPQL query SELECT g FROM game g, I get this error :

[EL Info]: 2013-11-25 13:40:27.761--ServerSession(1943119327)--EclipseLink, version: Eclipse Persistence Services - 2.5.0.v20130507-3faac2b
[EL Info]: connection: 2013-11-25 13:40:28.151--ServerSession(1943119327)--file:/Users/amine/Documents/workspace/ZombiesServer/target/classes/_ZombiesServer login successful
[WARNING] 
java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at org.codehaus.mojo.exec.ExecJavaMojo$1.run(ExecJavaMojo.java:297)
    at java.lang.Thread.run(Thread.java:724)
Caused by: java.lang.IllegalArgumentException: An exception occurred while creating a query in EntityManager: 
Exception Description: Problem compiling [SELECT g FROM game g]. 
[14, 18] The abstract schema type 'game' is unknown.
    at org.eclipse.persistence.internal.jpa.EntityManagerImpl.createQuery(EntityManagerImpl.java:1585)
    at org.eclipse.persistence.internal.jpa.EntityManagerImpl.createQuery(EntityManagerImpl.java:1605)
    at com.amine.zombies.DAO.GameDAO.getAllGames(GameDAO.java:80)
    at com.amine.zombies.application.Application.main(Application.java:21)
    ... 6 more
like image 559
Mohamed Amine Avatar asked Nov 25 '13 12:11

Mohamed Amine


4 Answers

You should have

SELECT g FROM Game g//you have game

but you have game instead of Game.

The @Table annotation is used for DB.

If you need to change the name in your JPQL, use the @Entity annotation: @Entity(name="nameUsedInJPQL") => nameUsedInJPQL is used in your JPQL.

If you do not specify anything in your @Entity, that the case-sensitive Entity class name is used.

like image 181
V G Avatar answered Nov 17 '22 00:11

V G


In my case I forgot to register it in persistence.xml.

like image 32
leomeurer Avatar answered Nov 17 '22 00:11

leomeurer


I just had the very same situation but my JPQL query was correct ! It occured in Glassfish 4.1 (build 13) (with EclipseLink).

After a few googling and some code commenting, I found out that the root cause of "The abstract schema type 'MyEntity' is unknown" was some use of Java 8 lambda code inside the entity class.

It seems that any feature of Java 8 is not (yet) supported in the version of EclipseLink that comes with GF. More info, see the bug report on that.

Hope this helps.

like image 5
user1853859 Avatar answered Nov 17 '22 02:11

user1853859


class name should be there not the table name in your query SELECT g FROM Game g

like image 4
Manish Kr Avatar answered Nov 17 '22 01:11

Manish Kr