jpa 编译查询时出错:抽象结构描述型别'entity'不明

62lalag4  于 2022-11-14  发布在  其他
关注(0)|答案(8)|浏览(125)

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
wwwo4jvm

wwwo4jvm1#

你应该有

SELECT g FROM Game g//you have game

但是你有game而不是Game
@Table注解用于DB。
如果需要更改JPQL中的名称,请使用@Entity注解:@Entity(name="nameUsedInJPQL") => nameUsedInJPQL is used in your JPQL.
如果未在@Entity中指定任何内容,则使用区分大小写的实体类名。

kuarbcqp

kuarbcqp2#

在我的例子中,我忘记了在persistence.xml中注册它。

gblwokeq

gblwokeq3#

我刚刚遇到了非常相同的情况,但是我的JPQL查询是正确的!它发生在Glassfish 4. 1(build 13)(带有EclipseLink)中。
经过几次Google搜索和一些代码注解,我发现“* 抽象模式类型'MyEntity'未知 *”的根本原因是在实体类中使用了一些Java 8 lambda代码
GF附带的EclipseLink版本似乎还不支持Java8的任何特性。更多信息,请参见the bug report on that.
希望这对你有帮助。

s5a0g9ez

s5a0g9ez4#

查询中的类名应该在那里而不是表名SELECT g FROM Game g

6yoyoihd

6yoyoihd5#

用于JPQL查询的名称定义为实体类的简单名称-在您的情况下为GameHuman。它可以被@Entity注解的name属性覆盖。@Table是物理Map注解,不会影响查询中的实体名称。
它可以与human一起使用,因为查询字符串不区分大小写。

kadbb459

kadbb4596#

我们遇到这个问题是由于org.eclipse.persistence.eclipselink库从2.4.0更新到2.5.1。更新到2.6.2后,它又能工作了。

umuewwlo

umuewwlo7#

如果你在一个包中调用persistence.xml,那么org.eclipse.persistence.jpa必须在你的包之前启动。否则,你会得到同样的异常。你还会看到表还没有在你的数据库中创建。因此,总是在你的清单中添加org.eclipse.persistence作为一个导入的包是一个好主意。

iecba09b

iecba09b8#

我的JPA项目使用Java 14和EclipseLink 3.0.0作为提供者,看起来EclipseLink不支持新的Java版本,切换回Java 1.7就成功了。

相关问题