Java 休眠:org.hibernate.hql.ast.QuerySyntaxException:意外的令牌
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4450844/
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
Hibernate: org.hibernate.hql.ast.QuerySyntaxException: unexpected token
提问by Tim
I am using Hibernate and I have this query:
我正在使用 Hibernate,我有这个查询:
List<Person> list = sess.createQuery("from Person").list();
With this statement, I get all persons from the database. But now, I only want some persons.
通过这个语句,我从数据库中获取了所有人。但现在,我只想要一些人。
My database scheme:
我的数据库方案:
Project <- Project_Person -> Person
项目 <- Project_Person -> 人员
So I only want Persons which are a member of a project.
所以我只想要作为项目成员的人员。
With the SQL statement on the database I get the desired result:
使用数据库上的 SQL 语句,我得到了想要的结果:
select * from Person inner join Project_Person
on person_id = id
where project_id = 1;
So I thought, I can write this with Hibernate:
所以我想,我可以用 Hibernate 写这个:
List<Person> list =
sess.createQuery(
"from Person inner join Project_Person
on person_id = id
where project_id = "+projectId).list();
But here I get an error:
但在这里我得到一个错误:
SERVE: Servlet.service() for servlet myproject3 threw exception
org.hibernate.hql.ast.QuerySyntaxException: unexpected token: on near line 1, column 65 [from com.mydomain.myproject.domain.Person inner join Project_Person on person_id = id where project_id = 1]
at org.hibernate.hql.ast.QuerySyntaxException.convert(QuerySyntaxException.java:54)
at org.hibernate.hql.ast.QuerySyntaxException.convert(QuerySyntaxException.java:47)
at org.hibernate.hql.ast.ErrorCounter.throwQueryException(ErrorCounter.java:82)
at org.hibernate.hql.ast.QueryTranslatorImpl.parse(QueryTranslatorImpl.java:284)
at org.hibernate.hql.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:182)
at org.hibernate.hql.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:136)
at org.hibernate.engine.query.HQLQueryPlan.<init>(HQLQueryPlan.java:101)
at org.hibernate.engine.query.HQLQueryPlan.<init>(HQLQueryPlan.java:80)
at org.hibernate.engine.query.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:124)
at org.hibernate.impl.AbstractSessionImpl.getHQLQueryPlan(AbstractSessionImpl.java:156)
at org.hibernate.impl.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:135)
at org.hibernate.impl.SessionImpl.createQuery(SessionImpl.java:1770)
at sun.reflect.GeneratedMethodAccessor33.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.hibernate.context.ThreadLocalSessionContext$TransactionProtectionWrapper.invoke(ThreadLocalSessionContext.java:344)
at $Proxy26.createQuery(Unknown Source)
...
Does anyone has an idea what's wrong here?
有谁知道这里有什么问题?
Best Regards.
此致。
New Error:
新错误:
SERVE: Servlet.service() for servlet myproject3 threw exception
org.hibernate.QueryException: could not resolve property: project of: com.mydomain.myproject.domain.Person [from com.mydomain.myproject.domain.Person p where p.project.id = :id]
n:m relation:
n:m 关系:
@ManyToMany(cascade = CascadeType.ALL)
@JoinTable(name = "Project_Person",
joinColumns = {@JoinColumn(name="project_id", referencedColumnName="id")},
inverseJoinColumns = {@JoinColumn(name="person_id", referencedColumnName="id")}
)
private Set<Person> persons = new HashSet<Person>();
@ManyToMany(mappedBy="persons")
private Set<Project> projects = new HashSet<Project>();
Full Error
完全错误
Hibernate: select project0_.id as id1_, project0_.createDate as create2_1_, project0_.description as descript3_1_, project0_.name as name1_ from Project project0_ where project0_.id=1
Hibernate: select person0_.id as id0_0_, project2_.id as id1_1_, person0_.email as email0_0_, person0_.firstName as firstName0_0_, person0_.lastName as lastName0_0_, project2_.createDate as create2_1_1_, project2_.description as descript3_1_1_, project2_.name as name1_1_ from Person person0_ inner join Project_Person projects1_ on person0_.id=projects1_.person_id inner join Project project2_ on projects1_.project_id=project2_.id where project2_.id=?
15.12.2010 16:42:26 org.apache.catalina.core.ApplicationDispatcher invoke
SERVE: Servlet.service() for servlet myproject3 threw exception
java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to com.mydomain.myproject.domain.Person
采纳答案by axtavt
HQL queries are written against the object model, not against the database schema.
HQL 查询是针对对象模型编写的,而不是针对数据库架构编写的。
Therefore your query depends on how you mapped the relationship between persons and projects. For example, in Person
has a many-to-one relationship to Project
via project
property, the query will look like this:
因此,您的查询取决于您如何映射人员和项目之间的关系。例如,inPerson
与Project
viaproject
属性是多对一的关系,查询将如下所示:
List<Person> list = sess.createQuery(
"from Person p where p.project.id = :id")
.setParameter("id", projectId)
.list();
EDIT:In the case of many-to-many relationship you need
编辑:在多对多关系的情况下,您需要
select p from Person p join p.projects proj where proj.id = :id
Also not that passing parameters via string concatenation is a bad practice, use setParameter()
instead.
也不是通过字符串连接传递参数是一种不好的做法,setParameter()
而是使用。