Java 编译查询时出错:抽象模式类型“实体”未知
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20193581/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Error on compiling query: The abstract schema type 'entity' is unknown
提问by Mohamed Amine
I'm developing a game with a database connection, and I use JPA to persist my data. Here is my Game entity :
我正在开发一个带有数据库连接的游戏,我使用 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;
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 :
为了获取存储在数据库中的所有人员的列表,我使用了这个 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();
}
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 :
问题是当我尝试使用 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.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
采纳答案by Andrei I
You should have
你应该有
SELECT g FROM Game g//you have game
but you have game
instead of Game
.
但你有game
而不是Game
.
The @Table
annotation is used for DB.
该@Table
注释用于DB。
If you need to change the name in your JPQL
, use the @Entity
annotation: @Entity(name="nameUsedInJPQL") => nameUsedInJPQL is used in your JPQL.
如果您需要更改 中的名称,请JPQL
使用@Entity
注释: @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.
如果您未在 中指定任何内容,@Entity
则使用区分大小写的实体类名称。
回答by kostja
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.
用于 JPQL 查询的名称被定义为实体类的简单名称 -Game
或者Human
在您的情况下。它可以被注释的name
属性覆盖 @Entity
。@Table
是物理映射注解,不影响查询中的实体名称。
It does work with human
because the query string is not case-sensitive.
它确实可以使用,human
因为查询字符串不区分大小写。
回答by leomeurer
In my case I forgot to register it in persistence.xml.
就我而言,我忘记在persistence.xml 中注册它。
回答by user1853859
I just had the very same situation but my JPQL query was correct ! It occured in Glassfish 4.1 (build 13) (with EclipseLink).
我只是遇到了同样的情况,但我的 JPQL 查询是正确的!它发生在 Glassfish 4.1(build 13)(使用 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.
经过一些谷歌搜索和一些代码注释后,我发现“抽象模式类型'MyEntity'未知”的根本原因是实体类中使用了Java 8 lambda代码。
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.
GF 附带的 EclipseLink 版本似乎(尚未)支持 Java 8 的任何功能。更多信息,请参阅错误报告。
Hope this helps.
希望这可以帮助。
回答by ulrich
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.
由于 org.eclipse.persistence.eclipselink 库从 2.4.0 更新到 2.5.1,我们遇到了这个问题。更新到 2.6.2 后,它又可以工作了。
回答by Manish Kr
class name should be there not the table name in your query SELECT g FROM Game g
类名应该不是查询中的表名 SELECT g FROM Game g