18

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
Dave Jarvis
  • 30,436
  • 41
  • 178
  • 315
Mohamed Amine
  • 2,264
  • 1
  • 23
  • 36

8 Answers8

31

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.

Raul Cacacho
  • 267
  • 1
  • 4
  • 15
V G
  • 18,822
  • 6
  • 51
  • 89
  • This is working, but I don't understand why... I added @Table(name="game"), so my table in the database is named game, not Game. Moreover, for the humans, it is working with human. Can you please explain why ? – Mohamed Amine Nov 25 '13 at 12:54
  • 2
    Changed the answer with a response to your question. – V G Nov 25 '13 at 12:55
  • 1
    Also, ensure you have the class registered in the persistence.xml file or Spring equivalent. – leroneb Sep 10 '19 at 12:48
  • In my case, I missed @Table annotation on the java entity class. – Janet Jan 28 '22 at 19:51
24

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

Opal
  • 81,889
  • 28
  • 189
  • 210
leomeurer
  • 742
  • 1
  • 8
  • 24
  • 11
    @PauloFreitas, I arrived here searching for the title of this question. Maybe this answer can be useful for someone else. In my case this was the solution. – leomeurer Jul 12 '14 at 21:06
  • Indeed, this doesn't have any relation with the question, in the persistence.xml file I only have declared the information to connect to the database :) – Mohamed Amine Jul 15 '14 at 15:07
  • 5
    @PauloFreitas, don't have relation? I had this kind of error "The abstract schema type 'entity' is unknown" and the problem was that i had forgot to declare the table in persistence.xml. For me this was the solution.I'm sorry for fix my error with this solution. – leomeurer Jul 15 '14 at 16:38
  • 1
    I had working JPA entity without it being defined in `persistence.xml` but then I renamed it and it stopped working until I added it to `persistence.xml`. So this is a good answer, while it may not be the only one. – Piro Aug 07 '18 at 06:12
5

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.

user1853859
  • 181
  • 2
  • 6
5

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

Manish Kr
  • 51
  • 1
  • 1
2

The name to be used for JPQL queries is defined as the simple name of the entity class - Game or Human in your case. It can be overridden by the name attribute of the @Entity annotation. @Table is a physical mapping annotation and does not influence the entity name in the query.

It does work with human because the query string is not case-sensitive.

kostja
  • 60,521
  • 48
  • 179
  • 224
1

We got the problem due to an update of org.eclipse.persistence.eclipselink library from 2.4.0 to 2.5.1. After updating to 2.6.2 it works again.

ulrich
  • 1,431
  • 3
  • 17
  • 46
0

If you are calling the persistence.xml in a bundle, then org.eclipse.persistence.jpa must be started before your bundle (or bundles). If not, you will get the same exception. You will also see that the tables have not been made in your database. It is therefore a good idea to always add org.eclipse.persistence as an imported package in your manifest.

keesp
  • 301
  • 2
  • 13
0

In my case no answer help me. My JPA project was using Java 14 with EclipseLink 3.0.0 as a provider and seems as EclipseLink doesn´t support new Java versions. Switching back to Java 1.7 did the trick.

lm2a
  • 835
  • 1
  • 10
  • 19