我正在使用 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/