我在JPA中使用以下删除查询:
DELETE FROM TeamInfo team WHERE team.id IN (SELECT t.id FROM TeamInfo t JOIN Manager m ON m.teamInfo = t WHERE m.localUserId IS NULL)
但我收到了错误:
ERROR o.h.hql.internal.ast.ErrorCounter - line 1:173: unexpected token: ON
antlr.NoViableAltException: unexpected token: ON
at org.hibernate.hql.internal.antlr.HqlBaseParser.fromJoin(HqlBaseParser.java:1694) [HqlBaseParser.class:4.2.8.Final]
at org.hibernate.hql.internal.antlr.HqlBaseParser.fromClause(HqlBaseParser.java:1349) [HqlBaseParser.class:4.2.8.Final]
at org.hibernate.hql.internal.antlr.HqlBaseParser.selectFrom(HqlBaseParser.java:1055) [HqlBaseParser.class:4.2.8.Final]
at org.hibernate.hql.internal.antlr.HqlBaseParser.queryRule(HqlBaseParser.java:701) [HqlBaseParser.class:4.2.8.Final]
at org.hibernate.hql.internal.antlr.HqlBaseParser.union(HqlBaseParser.java:990) [HqlBaseParser.class:4.2.8.Final]
at org.hibernate.hql.internal.antlr.HqlBaseParser.subQuery(HqlBaseParser.java:3719) [HqlBaseParser.class:4.2.8.Final]
at org.hibernate.hql.internal.antlr.HqlBaseParser.compoundExpr(HqlBaseParser.java:3089) [HqlBaseParser.class:4.2.8.Final]
at org.hibernate.hql.internal.antlr.HqlBaseParser.inList(HqlBaseParser.java:2884) [HqlBaseParser.class:4.2.8.Final]
at org.hibernate.hql.internal.antlr.HqlBaseParser.relationalExpression(HqlBaseParser.java:2730) [HqlBaseParser.class:4.2.8.Final]
at org.hibernate.hql.internal.antlr.HqlBaseParser.equalityExpression(HqlBaseParser.java:2456) [HqlBaseParser.class:4.2.8.Final]
at org.hibernate.hql.internal.antlr.HqlBaseParser.negatedExpression(HqlBaseParser.java:2420) [HqlBaseParser.class:4.2.8.Final]
at org.hibernate.hql.internal.antlr.HqlBaseParser.logicalAndExpression(HqlBaseParser.java:2336) [HqlBaseParser.class:4.2.8.Final]
at org.hibernate.hql.internal.antlr.HqlBaseParser.logicalOrExpression(HqlBaseParser.java:2301) [HqlBaseParser.class:4.2.8.Final]
at org.hibernate.hql.internal.antlr.HqlBaseParser.expression(HqlBaseParser.java:2011) [HqlBaseParser.class:4.2.8.Final]
at org.hibernate.hql.internal.antlr.HqlBaseParser.logicalExpression(HqlBaseParser.java:1787) [HqlBaseParser.class:4.2.8.Final]
at org.hibernate.hql.internal.antlr.HqlBaseParser.whereClause(HqlBaseParser.java:452) [HqlBaseParser.class:4.2.8.Final]
at org.hibernate.hql.internal.antlr.HqlBaseParser.deleteStatement(HqlBaseParser.java:264) [HqlBaseParser.class:4.2.8.Final]
at org.hibernate.hql.internal.antlr.HqlBaseParser.statement(HqlBaseParser.java:146) [HqlBaseParser.class:4.2.8.Final]
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.parse(QueryTranslatorImpl.java:268) [QueryTranslatorImpl.class:4.2.8.Final]
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:182) [QueryTranslatorImpl.class:4.2.8.Final]
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:138) [QueryTranslatorImpl.class:4.2.8.Final]
at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:105) [HQLQueryPlan.class:4.2.8.Final]
at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:80) [HQLQueryPlan.class:4.2.8.Final]
at org.hibernate.engine.query.spi.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:168) [QueryPlanCache.class:4.2.8.Final]
at org.hibernate.internal.SessionFactoryImpl.checkNamedQueries(SessionFactoryImpl.java:1085) [SessionFactoryImpl.class:4.2.8.Final]
at org.hibernate.internal.SessionFactoryImpl.<init>(SessionFactoryImpl.java:522) [SessionFactoryImpl.class:4.2.8.Final]
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1797) [Configuration.class:4.2.8.Final]
at org.hibernate.ejb.EntityManagerFactoryImpl.<init>(EntityManagerFactoryImpl.java:96) [EntityManagerFactoryImpl.class:4.2.8.Final]
at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:914) [Ejb3Configuration.class:4.2.8.Final]
at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:899) [Ejb3Configuration.class:4.2.8.Final]
at org.hibernate.ejb.HibernatePersistence.createEntityManagerFactory(HibernatePersistence.java:59) [HibernatePersistence.class:4.2.8.Final]
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:63) [Persistence.class:1.0.1.Final]
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:47) [Persistence.class:1.0.1.Final]
at com.magnifi.pennantrace.dao.hibernate.HibernateUtil.getEntityManagerFactory(HibernateUtil.java:18) [HibernateUtil.class:na]
at com.magnifi.pennantrace.dao.hibernate.HibernateUtil.currentEntityManager(HibernateUtil.java:27) [HibernateUtil.class:na]
at com.magnifi.schedserv.threads.SchedulerThread.run(SchedulerThread.java:32) [SchedulerThread.class:na]
那里有什么问题?
答案 0 :(得分:2)
问题是jpql中没有ON。以下是如何表达联接:
DELETE FROM TeamInfo team
WHERE team.id IN (
SELECT t.id
FROM Manager m JOIN m.teamInfo t
WHERE m.localUserId IS NULL
)
答案 1 :(得分:1)
您的查询似乎不是正确的JPA查询,请查看此link。
join属性已在映射中指定。
对于intance,如果你有一对多的双向关系,如下所示:
class TeamInfo{
//...
@OneToMany(mappedBy="teamInfo")
private List<Manager> managers;
//...
}
class Manager {
//...
@ManyToOne(fetch=FetchType.LAZY)
@JoinColumn(name="TEAM_INFO_ID")
private TeamInfo teamInfo;
//...
}
正确的连接查询如下:
select t.id FROM TeamInfo t join t.managers m where m.localUserId is null
或相反的情况:
class TeamInfo{
//...
@ManyToOne(fetch=FetchType.LAZY)
@JoinColumn(name="MANAGER_ID")
private Manager manager;
//...
}
class Manager {
//...
@OneToMany(mappedBy="manager")
private List<TeamInfo> teamInfo;
//...
}
正确的连接查询如下:
select t.id FROM Manager m join m.teamInfo t where m.localUserId is null