编译查询时出错:抽象架构类型“实体”未知

2022-09-02 14:24:47

我正在开发一个具有数据库连接的游戏,我使用JPA来保存我的数据。这是我的游戏实体:

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

为了获取存储在DB中的所有人类的列表,我使用了这个DAO,它运行得很好,并且还获得了Building实体:

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

[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

答案 1

你应该有

SELECT g FROM Game g//you have game

但是你有而不是.gameGame

该注释用于 DB。@Table

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

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


答案 2

在我的情况下,我忘记在持久性中注册它.xml。


推荐