java.sql.SQLException : You can't specify target table 'user_details' for update in FROM clause 异常

标签 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();

股权追踪:

  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

谁能帮我改正这个问题?

最佳答案

更新 UserDetails 设置 userStatus=:userStatus where department=:department

关于java.sql.SQLException : You can't specify target table 'user_details' for update in FROM clause 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27396000/

相关文章:

mysql - 让服务器拥有持久的 mySQL 连接或在需要时连接会更高效

mysql - Laravel 5数据库查询检索第一个表数据但不在其他表中

php - Laravel 5.3,似乎无法配置使用哪个数据库?

java - spring boot 无法连接到 mySql

java - 在 javafx 8 中设置和使用时间线之外的变量

java - 找不到基本名称的 bundle

java - 使用 JTable - 指定大小

java - 如果尝试在 RestHelper 类中使用相同路径添加 JsonPut 和 JsonPost 方法,则无法编译代码

java - Hibernate计数NamedQuery与同一实体的多对多关联

java - Single Table Inheritance WITHOUT Discriminator 列