org.hibernate.hql.internal.ast.querysyntaxception:尝试删除与其自身联接的表时出现意外标记:from

bnl4lu3b  于 2021-07-24  发布在  Java
关注(0)|答案(1)|浏览(337)

我正在尝试使用hibernate运行查询我是通过以下方法运行的:

private void insertQuery(String toDeleteNewOrOld) {

    String queryString = "";
    if("deleteNew".equals(toDeleteNewOrOld)){
         queryString =
          " delete dmpg2 \n"
        + "from DmPriceGroup dmpg1 \n"
        + " join DmPriceGroup dmpg2 on dmpg1.priceGroupId = dmpg2.priceGroupId \n"
        + " where dmpg1.uid <> dmpg2.uid \n"
        + " and dmpg1.priceGroupName <> dmpg2.priceGroupName \n"
        + " and dmpg1.dmCreateTime > dmpg2.dmCreateTime";
    }
    else if("deleteOld".equals(toDeleteNewOrOld)){
         queryString =
          "DELETE dmpg1\n"
        + "FROM DmPriceGroup dmpg1\n"
        + "join DmPriceGroup dmpg2 on dmpg1.priceGroupId = dmpg2.priceGroupId\n"
        + "where dmpg1.uid <> dmpg2.uid\n"
        + "and dmpg1.priceGroupName = dmpg2.priceGroupName\n"
        + "and dmpg1.dmCreateTime > dmpg2.dmCreateTime\n";
    }

    Map<String, Object> parameters = new HashMap<>();

    writerLogic.executeQuery(getDmEntityClass(), queryString, parameters);
}

当我执行它时,使用了deleteold条件querystring,但是它失败了,并出现以下错误

org.hibernate.hql.internal.ast.QuerySyntaxException: unexpected token: FROM near line 2, column 1 [DELETE dmpg1
FROM com.schantz.generallife.dm.model.DmPriceGroup dmpg1
join com.schantz.generallife.dm.model.DmPriceGroup dmpg2 on dmpg1.priceGroupId = dmpg2.priceGroupId
where dmpg1.uid <> dmpg2.uid
and dmpg1.priceGroupName = dmpg2.priceGroupName
and dmpg1.dmCreateTime > dmpg2.dmCreateTime
]

看看类似的问题,这似乎是由错误的语法造成的,但是当我在ssms上手动运行以下代码时,它运行良好

DELETE dmpg1
FROM DmPriceGroup dmpg1
join DmPriceGroup dmpg2 on dmpg1.priceGroupId = dmpg2.priceGroupId
where dmpg1.uid <> dmpg2.uid
and dmpg1.priceGroupName = dmpg2.priceGroupName
and dmpg1.dmCreateTime > dmpg2.dmCreateTime

在delete之后删除dmpg1,将使意外的令牌改为“join”。然而,我不相信如果不澄清是谁加入了dmpricegroup来删除,我就有可能从dmpricegroup中删除

s4chpxco

s4chpxco1#

据这位消息人士透露,这是不可能的。
您可以取而代之的是获取所有的id,可以对它们运行deleteall()(或者编写本机sql,但我不喜欢这样)。

相关问题