2014-04-05 66 views
2

我使用下面的刪除查詢在JPA: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] 

什麼問題呢?

回答

1

您的查詢並不似乎是一個正確的JPA查詢,檢查此link
連接屬性已在映射中指定。

對於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 
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 
)