java.sql.SQLException:您无法指定目标表' user_details'用于FROM子句中的更新

时间:2014-12-10 08:06:38

标签: java mysql hibernate

我正在使用MySql进行hibernate工作并尝试更新记录,这会给我错误。

代码

    Query query = session.createQuery("update UserDetails set userStatus=:userStatus where id =(select max(id) from UserDetails where  department=:department)");
    query.setParameter("userStatus", "ACTIVE");
    query.setParameter("department",55);
    int result=query.executeUpdate();

StakeTrace:

  org.hibernate.exception.GenericJDBCException: could not execute update query at org.hibernate.exception.SQLStateConverter.handledNonSpecificException(SQLStateConverter.java:140)
  at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:128)
  at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66)
  at org.hibernate.hql.ast.exec.BasicExecutor.execute(BasicExecutor.java:110)
  at org.hibernate.hql.ast.QueryTranslatorImpl.executeUpdate(QueryTranslatorImpl.java:421)
  at org.hibernate.engine.query.HQLQueryPlan.performExecuteUpdate(HQLQueryPlan.java:283)
  at org.hibernate.impl.SessionImpl.executeUpdate(SessionImpl.java:1278)
  at org.hibernate.impl.QueryImpl.executeUpdate(QueryImpl.java:117)
  at com.nextenders.dao.TestDao.checkmysql(TestDao.java:58)
  at com.nextenders.facadeimplementation.facade.JUnitFacade.main(JUnitFacade.java:17)
Caused by: java.sql.SQLException: You can't specify target table 'user_details' for update in FROM clause
  at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1055)
  at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:956)
  at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3491)
  at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3423)
  at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1936)
  at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2060)
  at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2542)
  at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1734)
  at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2019)
  at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1937)
  at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1922)
  at com.mchange.v2.c3p0.impl.NewProxyPreparedStatement.executeUpdate(NewProxyPreparedStatement.java:175)
  at org.hibernate.hql.ast.exec.BasicExecutor.execute(BasicExecutor.java:101)
... 6 more

任何人都可以帮我纠正这个问题吗?

4 个答案:

答案 0 :(得分:1)

更新UserDetails set userStatus =:userStatus where department =:department

答案 1 :(得分:1)

我正确你的sql是复杂的尝试:

update UserDetails set a.userStatus=:userStatus where department=:department

答案 2 :(得分:1)

我不认为HQL支持这样的更新语句。有关详细信息,请参阅https://docs.jboss.org/hibernate/orm/3.3/reference/en/html/queryhql.html

有"批量"更新语句,但我认为它们不支持子查询。

但是,由于您的更新语句似乎很简单,因此您可以使用直接SQL语句进行更新。

Query query = session.createSQLQuery("update UserDetails set userStatus=:userStatus where id in(select id from UserDetails where  department=:department)");
    query.setParameter("userStatus", "ACTIVE");
    query.setParameter("department",55);
    int result=query.executeUpdate();

编辑:

感谢我发现的其他评论,你的子查询引用了同一个表。这是不可能的,所以你需要简单地按照其他答案中的说明:

Query query = session.createQuery("update UserDetails set userStatus=:userStatus where department=:department");
    query.setParameter("userStatus", "ACTIVE");
    query.setParameter("department",55);
    int result=query.executeUpdate();

答案 3 :(得分:0)

只需快速思考一下即可尝试 - 无法保证:为您的表命名,以便子查询有自己的方式来处理表。即。

"更新UserDetails set userStatus =:userStatus where id in(从UserDetails选择u2.id为u2,其中u2.department =:department)"

我认为它让您感到困惑,因为您在子查询中使用的表格与您正在更新的表格相同。