简体   繁体   中英

Hibernate, Can't execute native SQL update query that has JOINS

public void do_UPDATE(Set<Long> customerIdSet) {

    final String query = 
        "UPDATE product_orders po " +
        "INNER join customer c " +
        "ON po.order_id = c.order_id " +
        "SET po.updated_on = now() " +
        "WHERE c.customer_id IN (:customerIdSet)";

        List<Long> ids = new ArrayList<Long>(customerIdSet.size());

        for(final Long id : customerIdSet) {
            ids.add(id);
        }

    _sessionFactory
        .getCurrentSession()
        .createSQLQuery(query)
        .setParameterList("customerIdSet", ids)
        .executeUpdate();

}

I'm getting a SQLGrammarException. This query actually runs in a real MySQL console, but why doesn't Hibernate think it is the right syntax?

我的平台不会执行包含INNER JOIN的语句,因此解决方案是使用子查询。

If the problem was with INNER JOIN I would try to modify query to:

"UPDATE product_orders po " +
"INNER join customer c " +
"ON po.order_id = c.order_id " +
"AND c.customer_id IN (:customerIdSet) "+
"SET po.updated_on = now() "

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM