【问题标题】:mybatis insert all nextval unique constraint error in @transaction mdoemybatis 在@transaction mdoe 中插入所有 nextval 唯一约束错误
【发布时间】:2015-10-23 08:06:42
【问题描述】:

MyBatis 3 春天-java

我正在尝试批量插入,当有超过 1 条记录时出现以下错误。它与一个记录完美结合

我相信 b/c 它在事务中 nextval 不会在每次迭代中生成 nextval。我对此有任何帮助吗?

nested exception is java.sql.SQLIntegrityConstraintViolationException: ORA-00001: unique constraint (LINEAR_UPSELL.SYS_C0016697) violated

我的方法在java文件中有Transaction注解

@Transactional(propagation=Propagation.REQUIRED, readOnly=false, rollbackFor=Exception.class)

在映射器文件下面是我的插入语句

<insert id="insertService" parameterType="java.util.List">
    insert all
    <foreach collection="list" item="ch" index="index" >
    into tva_upselladmin_channel (id, source_id, service_id, name) values  (
        TVA_UPSELLADMIN_CHANNEL_SEQ.nextVal,
        #{ch.sourceId}, 
        #{ch.serviceId}, 
        #{ch.name}
        )
    </foreach>
            SELECT * FROM dual
</insert>

【问题讨论】:

    标签: spring transactions mybatis


    【解决方案1】:

    在 oracle nextval 中,insert all 语句不能很好地工作。你必须找到解决方法如下

    extractvalue(dbms_xmlgen.getxmltype('select TVA_UPSELLADMIN_CHANNEL_SEQ.nextval - 1 from dual'),'//text()')
    

    下面的完整插入不知道有-1。

    <insert id="insertServiceMappings" parameterType="java.util.List">
        insert all
        <foreach collection="list" item="channel" index="index" >
        into tva_upselladmin_channel (id, source_id, service_id, name) values  (
            extractvalue(dbms_xmlgen.getxmltype('select TVA_UPSELLADMIN_CHANNEL_SEQ.nextval - 1 from dual'),'//text()'),
            #{channel.sourceId}, 
            #{channel.serviceId}, 
            #{channel.name}
            )
        </foreach>
                SELECT * FROM dual
    </insert>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-15
      • 2018-11-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多