0
我有一個oneToMany
電影和演員表之間的關聯和ManyToOne
關聯反向的方式。一個JOIN可以工作,但是當我在JOIN中有一個篩選子句時,它會失敗。請在上面的註釋中找到相關的代碼部分。INNER JOIN不在hibernate中工作?
我已閱讀此職位。但是,這並沒有幫助 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)
好抓。想知道你是怎麼想出來的......錯誤信息? – 2014-09-30 18:18:18
@brainstorm是的。如果你在消息的這一部分顯示「INNER JOIN m.actors actwhere act.name =:acname',你可以看到」act「和」where「連接。 – Jens 2014-09-30 18:20:00
謝謝你好!在相關說明中,是否有可能在沒有JOIN的情況下執行此查詢。即'session.createQuery(「從電影m選擇m.name,act.name,m.actors在其中」BRAD「IN act.names」).list()'。當然,這不起作用,但想知道如果沒有JOIN可能嗎?謝謝 – 2014-09-30 18:25:13