mybaties-Mybaties Update 不允许的操作

问题描述

Mybaties Update 不允许的操作

环境:Spring+Mybaties3.X

 /**
     * @param entity
     * 使用CRUDTemplate操作
     */
    @UpdateProvider(type = CRUDTemplate.class, method = "update")
    public int update(T entity);
 public String update(Object obj) throws Exception {
        BEGIN();

        UPDATE(getEntityTable(obj).getName());
        SET(getUpdateSetStr(obj));
        WHERE("id=#{id}");
        String sql = SQL();

        logger.debug("sql : "+sql);
        return sql;
    }

org.springframework.jdbc.UncategorizedSQLException: Error getting generated key or setting result to parameter object. Cause: java.sql.SQLException: 不允许的操作
; uncategorized SQLException for SQL []; SQL state [null]; error code [17090]; 不允许的操作; nested exception is java.sql.SQLException: 不允许的操作
at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:83)
at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:80)
at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:80)
at org.mybatis.spring.MyBatisExceptionTranslator.translateExceptionIfPossible(MyBatisExceptionTranslator.java:71)
at org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:365)
at $Proxy18.update(Unknown Source)
at org.mybatis.spring.SqlSessionTemplate.update(SqlSessionTemplate.java:251)
at org.apache.ibatis.binding.MapperMethod.execute(MapperMethod.java:54)
at org.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java:52)
at $Proxy24.update(Unknown Source)
at com.tuban.framework.service.impl.BaseServiceImpl.update(BaseServiceImpl.java:69)
at com.tuban.framework.service.impl.BaseServiceImpl$$FastClassByCGLIB$$cb0b41cc.invoke()
at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204)
at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:698)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150)
at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:90)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:631)
at com.tuban.core.role.service.impl.RoleServiceImpl$$EnhancerByCGLIB$$70da1b82.update()
at mybatis.MybatisTest.testAdd(MybatisTest.java:103)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:45)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:74)
at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:83)
at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:72)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:231)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:88)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:71)
at org.junit.runners.ParentRunner.run(ParentRunner.java:300)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:174)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:46)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
Caused by: java.sql.SQLException: 不允许的操作
at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:112)
at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:146)
at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:208)
at oracle.jdbc.driver.OracleStatement.getGeneratedKeys(OracleStatement.java:4568)
at org.apache.commons.dbcp.DelegatingStatement.getGeneratedKeys(DelegatingStatement.java:315)
at org.apache.commons.dbcp.DelegatingStatement.getGeneratedKeys(DelegatingStatement.java:315)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.apache.ibatis.logging.jdbc.PreparedStatementLogger.invoke(PreparedStatementLogger.java:85)
at $Proxy45.getGeneratedKeys(Unknown Source)
at org.apache.ibatis.executor.keygen.Jdbc3KeyGenerator.processBatch(Jdbc3KeyGenerator.java:51)
at org.apache.ibatis.executor.keygen.Jdbc3KeyGenerator.processAfter(Jdbc3KeyGenerator.java:45)
at org.apache.ibatis.executor.statement.PreparedStatementHandler.update(PreparedStatementHandler.java:48)
at org.apache.ibatis.executor.statement.RoutingStatementHandler.update(RoutingStatementHandler.java:69)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.apache.ibatis.plugin.Plugin.invoke(Plugin.java:62)
at $Proxy43.update(Unknown Source)
at org.apache.ibatis.executor.ReuseExecutor.doUpdate(ReuseExecutor.java:50)
at org.apache.ibatis.executor.BaseExecutor.update(BaseExecutor.java:105)
at org.apache.ibatis.executor.CachingExecutor.update(CachingExecutor.java:71)
at org.apache.ibatis.session.defaults.DefaultSqlSession.update(DefaultSqlSession.java:152)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:355)
... 43 more

有遇到过的吗?费解

解决方案

已解决,太变态了,由于MYbaties默认useGeneratedKeys=true,所以在insert和update的时候,都生成KEY,然后注入。所以这就是为什么update的时候会失败,同理,在insert的时候,插入成功后,getId()也会发现,这个Id并不是我们注入的ID,而是mybaties帮我们生成的ID,所以在使用代理方法执行SQL的时候,需要如下设置:

 /**
     * @param entity
     * 使用CRUDTemplate操作
     */
    @UpdateProvider(type = CRUDTemplate.class, method = "update")
    **@Options(useGeneratedKeys=false )**
    public int update(T entity);

解决方案二:

贴出执行的SQL语句,结果是提示执行成功的:

 2015-02-23 02:41:13,407 [main] DEBUG work.mybatis.mapperTemplate.CRUDTemplate -sql : UPDATE ic_role
SET NAME=#{name},MODIFY_TIME=#{modifyTime},MODIFIER=#{modifier},STATE=#{state}
WHERE (id=#{id})
2015-02-23 02:41:13,407 [main] DEBUG ore.role.dao.RoleMapper.updateByProvider -==>  Preparing: UPDATE ic_role SET NAME=?,MODIFY_TIME=?,MODIFIER=?,STATE=? WHERE (id=?)
2015-02-23 02:41:13,516 [main] DEBUG ore.role.dao.RoleMapper.updateByProvider -==> Parameters: test_update2222(String), 2015-02-23 02:41:13.407(Timestamp), 1(String), 1(String), 8debb846eb1d493aa25bba1fc5fb0da5(String)
2015-02-23 02:41:13,516 [main] DEBUG ore.role.dao.RoleMapper.updateByProvider -<==    Updates: 1
时间: 2024-08-27 08:02:43

mybaties-Mybaties Update 不允许的操作的相关文章

利用事务日志来恢复Update、Delete误操作引起的数据丢失

恢复|数据 可能有不少朋友遇到过这样的问题:update或delete语句忘带了where子句,或where子句精度不够,执行之后造成了严重的后果,这种情况的数据恢复只能利用事务日志的备份来进行,所以如果你的SQL没有进行相应的全库备份或不能备份日志(truncate log on checkpoint选项为1),那么就无法进行数据的恢复了,或者只能恢复到最近一次的备份的数据了. 以下简单说明恢复数据方法:1,如果误操作之前存在一个全库备份(或已有多个差异备份或增量备份),首先要做的事就是进进行

利用事务日志来恢复Update、Delete误操作引起的数据丢

恢复|数据 可能有不少朋友遇到过这样的问题:update或delete语句忘带了where子句,或where子句精度不够,执行之后造成了严重的后果,这种情况的数据恢复只能利用事务日志的备份来进行,所以如果你的SQL没有进行相应的全库备份或不能备份日志(truncate log on checkpoint选项为1),那么就无法进行数据的恢复了,或者只能恢复到最近一次的备份的数据了. 以下简单说明恢复数据方法:1,如果误操作之前存在一个全库备份(或已有多个差异备份或增量备份),首先要做的事就是进进行

Laravel 5.3 查询构建器方法 where/update 新增 JSON 属性操作语法

从 5.0 版本起 Laravel 就支持 JSON 格式数据的转换,之前这样做的目的只是为了方便业务处理,数据存储在数据库的数据类型依然是 TEXT,但是 MySQL 5.7 版本起开始支持原生的 JSON 数据类型,这将为我们的开发带来极大方便.Laravel 5.3 也为基于 JSON 类型的数据查询和更新引入了新的语法. 假设我们有一个包含 JSON 类型字段的数据表: class CreateContactsTable extends Migration {     public fu

mysql insert的几点操作(DELAYED,IGNORE,ON DUPLICATE KEY UPDATE )_Mysql

INSERT语法 INSERT [LOW_PRIORITY | DELAYED | HIGH_PRIORITY] [IGNORE] [INTO] tbl_name [(col_name,...)] VALUES ({expr | DEFAULT},...),(...),... [ ON DUPLICATE KEY UPDATE col_name=expr, ... ] 或: INSERT [LOW_PRIORITY | DELAYED | HIGH_PRIORITY] [IGNORE] [INT

LINQ to SQL语句(11)之Update

更新(Update) 说明:更新操作,先获取对象,进行修改操作之后,直接调 用SubmitChanges()方法即可提交.注意,这里是在同一个DataContext中,对于 不同的DataContex看下面的讲解. 1.简单形式Customer cust = db.Customers.First(c => c.CustomerID == "ALFKI"); cust.ContactTitle = "Vice President"; db.SubmitChang

Hibernate:update和saveOrUpdate详解

先来点概念: 在Hibernate中,最核心的概念就是对PO的状态管理.一个PO有三种状态: 1.未被持久化的VO 此时就是一个内存对象VO,由JVM管理生命周期 2.已被持久化的PO,并且在Session生命周期内 此时映射数据库数据,由数据库管理生命周期 3.曾被持久化过,但现在和Session已经detached了,以VO的身份在运行 这种和Session已经detached的PO还能够进入另一个Session,继续进行PO状态管理,此时它就成为PO的第二种状态了.这种PO实际上是跨了Se

MySQL数据库INSERT、UPDATE、DELETE以及REPLACE语句的用法详解

本篇文章是对MySQL数据库INSERT.UPDATE.DELETE以及REPLACE语句的用法进行了详细的分析介绍,需要的朋友参考下   MySQL数据库insert和update语句引:用于操作数据库的SQL一般分为两种,一种是查询语句,也就是我们所说的SELECT语句,另外一种就是更新语句,也叫做数据操作语句.言外之意,就是对数据进行修改.在标准的SQL中有3个语句,它们是INSERT.UPDATE以及DELETE. 用 于操作数据库的SQL一般分为两种,一种是查询语句,也就是我们所说的S

PHP 构建语义 Web CRUD 操作

  创建.读.更新和删除(Create/Read/Update/Delete,CRUD)操作是最基本的数据库操作,但是它们也是最重要的操作.CRUD 操作通常是使用关系数据库系统中的结构化查询语言(Structured Query Language,SQL)完成的.随着 Web 变得更加具有面向数据特性,因此需要从基于 SQL 的 CRUD 操作转移到基于语义 Web 的 CRUD 操作.了解如何使用 PHP 通过基于语义的 Web 执行 CRUD 操作. 常用缩写词 API - 应用程序编程接

Android之采用execSQL与rawQuery方法完成数据的添删改查操作详解_Android

使用 SQLiteDatabase 操作 SQLite 数据库 复制代码 代码如下: /* Android提供了一个名为SQLiteDatabase的类,该类封装了一些操作数据库的API,使用该类可以完成对数据进行添加(Create).查询(Retrieve).更新(Update)和删除(Delete)操作(这些操作简称为CRUD).对SQLiteDatabase的学习,我们应该重点掌握execSQL()和rawQuery()方法. execSQL()方法可以执行insert.delete.up