我在Movie和actor Table之间有一个oneToMany
关联,并且ManyToOne
关联反向。一个JOIN可以工作,但是当我在JOIN中有一个过滤子句时,它就会失败。请找到相关的代码部分,上面有评论说明它。
我看过这篇文章。但这没有帮助 Join Query not working in Hibernate
以下是我的实体中的相关部分:
@Entity
public class Movie {
......
@OneToMany(mappedBy="movie",fetch=FetchType.EAGER,cascade=CascadeType.ALL)
private Set<Actor> actors=new HashSet<>();
//getters and setters
}
@Entity
public class Actor {
.....
@ManyToOne(fetch=FetchType.EAGER)
@JoinColumn(name="ACTOR_MOVIE")
//getters and setters
}
这是我的HibernateTest类:
public class HibernateTest {
public static void main(String[] args) {
Movie movie1 = new Movie("Mr and Mrs.Smith", new Date(2005, 6, 5));
Actor actor3=new Actor("Brad");
actor3.setMovie(movie1);
Actor actor4=new Actor("Joe");
actor4.setMovie(movie1);
movie1.getActors().add(actor3);
movie1.getActors().add(actor4);
Movie movie = new Movie("Titanic", new Date(1997,12,19));
Actor actor1=new Actor("Leo");
Actor actor2=new Actor("Kate");
actor1.setMovie(movie);
actor2.setMovie(movie);
movie.getActors().add(actor1);
movie.getActors().add(actor2);
SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
Session session = sessionFactory.openSession();
session.beginTransaction();
session.persist(movie);
session.persist(movie1);
session.getTransaction().commit();
session.close();
session = sessionFactory.openSession();
session.beginTransaction();
//The following query works
//List<Object[]> actor_from_movies=session.createQuery("select m.name,act.name from Movie m INNER JOIN m.actors act").list();
//This throws error
List<Object[]> actor_from_movies=session.createQuery("select m.name,act.name from Movie m INNER JOIN m.actors act"
+ "where act.name=:acname").setParameter("acname", "Brad").list();
System.out.println("actor_from_movies "+ actor_from_movies);
session.getTransaction().commit();
session.close();
}
}
错误讯息:
Sep 30, 2014 11:08:42 AM org.hibernate.hql.internal.ast.ErrorCounter reportError
ERROR: line 1:92: unexpected token: act
Sep 30, 2014 11:08:42 AM org.hibernate.hql.internal.ast.ErrorCounter reportError
ERROR: line 1:92: unexpected token: act
line 1:92: unexpected token: act
at org.hibernate.hql.internal.antlr.HqlBaseParser.fromJoin(HqlBaseParser.java:1727)
at org.hibernate.hql.internal.antlr.HqlBaseParser.fromClause(HqlBaseParser.java:1380)
at org.hibernate.hql.internal.antlr.HqlBaseParser.selectFrom(HqlBaseParser.java:1086)
at org.hibernate.hql.internal.antlr.HqlBaseParser.queryRule(HqlBaseParser.java:731)
at org.hibernate.hql.internal.antlr.HqlBaseParser.selectStatement(HqlBaseParser.java:323)
at org.hibernate.hql.internal.antlr.HqlBaseParser.statement(HqlBaseParser.java:186)
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.parse(QueryTranslatorImpl.java:295)
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:203)
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:158)
at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:126)
at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:88)
at org.hibernate.engine.query.spi.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:167)
at org.hibernate.internal.AbstractSessionImpl.getHQLQueryPlan(AbstractSessionImpl.java:301)
at org.hibernate.internal.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:236)
at org.hibernate.internal.SessionImpl.createQuery(SessionImpl.java:1800)
at com.example.hibernate.HibernateTest.main(HibernateTest.java:142)
Exception in thread "main" org.hibernate.hql.internal.ast.QuerySyntaxException: unexpected token: act near line 1, column 92 [select m.name,act.name from com.example.hibernate.dto.Movie m INNER JOIN m.actors actwhere act.name=:acname]
at org.hibernate.hql.internal.ast.QuerySyntaxException.convert(QuerySyntaxException.java:91)
at org.hibernate.hql.internal.ast.ErrorCounter.throwQueryException(ErrorCounter.java:109)
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.parse(QueryTranslatorImpl.java:304)
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:203)
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:158)
at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:126)
at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:88)
at org.hibernate.engine.query.spi.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:167)
at org.hibernate.internal.AbstractSessionImpl.getHQLQueryPlan(AbstractSessionImpl.java:301)
at org.hibernate.internal.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:236)
at org.hibernate.internal.SessionImpl.createQuery(SessionImpl.java:1800)
at com.example.hibernate.HibernateTest.main(HibernateTest.java:142)
答案 0 :(得分:1)
在以下位置添加空格:
" where act.name=:acname").