vue+SpringBoot处理数据 嵌套异常(nested exception)

1. 问题描述

在做收藏功能时,出现嵌套异常报错
服务端报错日志如下

2021-04-20 20:58:10.607 ERROR 10200 --- [io-8088-exec-10] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.dao.InvalidDataAccessApiUsageException: detached entity passed to persist: com.cwq.bnsp_behind.entity.Collect; nested exception is org.hibernate.PersistentObjectException: detached entity passed to persist: com.cwq.bnsp_behind.entity.Collect] with root cause

前端使用VUE+axios库,代码如下

   addCollect(){
      
      var _this=this;
      this.collect.collectUserId=sessionStorage.getItem("userId");
      this.collect.collectArticleId=this.article.articleId;
      this.collect.collectState=1;
    //  alert(this.collect.collectUserId);
      this.$axios.put("http://localhost:8088/Collects/addCollect",this.collect)
      .then(function (){
         _this.$message({
          message: '收藏成功',
          type: 'success'
        });
      })
    }

2. 解决分析

实体类中有两个字段自动创建。collectId和collectTime

package com.cwq.bnsp_behind.entity;


import lombok.Data;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;

import javax.persistence.*;
import java.util.Date;

@Data
@Entity

//自动创建时间
@EntityListeners(AuditingEntityListener.class)
public class Collect {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer collectId;

    private Integer collectArticleId;

    private Integer collectUserId;

    @CreatedDate
    private Date collectTime;

    private Integer collectState;

}


然后又在前端界面中给它指定值,导致报错


      // 收藏
      collect:{
        collectArticleId: 0,
        collectId: 0,
        collectState: 0,
        collectTime: "",
        collectUserId: 0
}   ,

3. 解决步骤

将 collectId和collectTime去掉即可,

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值