编译查询时出错:抽象模式类型“实体"是未知的 [英] Error on compiling query: The abstract schema type 'entity' is unknown

查看:96
本文介绍了编译查询时出错:抽象模式类型“实体"是未知的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发具有数据库连接的游戏,并且使用JPA保留数据.这是我的游戏实体:

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;

这是我的人类实体:

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

要获取存储在数据库中的所有人员的列表,请使用此DAO,该DAO运行得很好,并且还获得了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();
}

问题是,当我尝试通过JPQL查询SELECT g FROM game g进行同样的操作以获取所有游戏的列表时,出现此错误:

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

推荐答案

您应该拥有

SELECT g FROM Game g//you have game

但您使用的是game而不是Game.

@Table批注用于DB.

如果需要在JPQL中更改名称,请使用@Entity批注:@Entity(name="nameUsedInJPQL") => nameUsedInJPQL is used in your JPQL.

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

如果您未在@Entity中指定任何内容,则使用区分大小写的Entity类名称.

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

这篇关于编译查询时出错:抽象模式类型“实体"是未知的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆