java – Hibernate:org. hibernate. hql. ast. Querysyntaxexception: unexpected token
I use hibernate. I have this query:
List<Person> list = sess.createQuery("from Person").list();
With this statement, I get everyone from @ R_ 301_ 2416@. But now I just want some people
My @ r_ 301_ 2416 @ scheme:
Project < - Project_ Person - > person
So I just want a member of the project
Use @ r_ 301_ The SQL statement on 2416 @ gives me the required results:
select * from Person inner join Project_Person on person_id = id where project_id = 1;
So I think I can write this in 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 a mistake:
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 have any ideas?
Best wishes.
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 relationship:
@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>();
Total 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
Solution
HQL queries are written for the object model, not @ R_ 301_ 2416 @ mode
Therefore, your query depends on how the relationship between people and projects is mapped For example, in person, there is a many to one relationship with project through project attributes. The query will be as follows:
List<Person> list = sess.createQuery( "from Person p where p.project.id = :id") .setParameter("id",projectId) .list();
Edit: when you need a many to many relationship
select p from Person p join p.projects proj where proj.id = :id
It is not a bad practice to pass parameters through string connection, but to use setparameter()