【问题标题】:Multiple representations of the same entity are being merged Error同一实体的多个表示正在合并错误
【发布时间】:2018-09-23 07:42:57
【问题描述】:

我在这里查看了很多帖子,但仍然无法解决这个问题。有人说你需要删除 CascadeType.MERGE 但它没有解决我的错误。我的应用是 Spring-boot 应用,使用 hibernate & MySQL db 作为数据源,我的代码:

MySQL 脚本:

DROP SCHEMA IF EXISTS `couponsystem`;

CREATE SCHEMA `couponsystem`;

use `couponsystem`;



DROP TABLE IF EXISTS `company`;

CREATE TABLE `company` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(20) NOT NULL UNIQUE,
  `password` varchar(20) NOT NULL,
  `email` varchar(20) DEFAULT NULL,
  PRIMARY KEY (`id`)
)ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=latin1;


DROP TABLE IF EXISTS `coupon`;

CREATE TABLE `coupon` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `title` varchar(20) NOT NULL UNIQUE,
  `start_date` datetime DEFAULT NULL,
  `end_date` datetime DEFAULT NULL,
  `amount` int DEFAULT NULL,
  `type` varchar(15) DEFAULT NULL,
  `message` varchar(50) DEFAULT NULL,
  `price` float DEFAULT NULL,
  `company_id` int(11),
  PRIMARY KEY (`id`),
  KEY `FK_company_id` (`company_id`),
  CONSTRAINT `FK_company_id` FOREIGN KEY (`company_id`) REFERENCES `company` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
)ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=latin1;

DROP TABLE IF EXISTS `customer`;

CREATE TABLE `customer` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(20) NOT NULL UNIQUE,
  `password` varchar(20) NOT NULL,
  PRIMARY KEY (`id`)
)ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=latin1;


CREATE TABLE `customer_coupon`(

`customer_id` int(11) NOT NULL,
`coupon_id` int(11) NOT NULL,
PRIMARY KEY (`customer_id`,`coupon_id`),
KEY `fk_customer_coupon_idx` (`customer_id`),
CONSTRAINT `FK_customer_id` FOREIGN KEY (`customer_id`) REFERENCES `customer` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT `FK_coupon_id` FOREIGN KEY (`coupon_id`) REFERENCES `coupon` (`id`) ON DELETE CASCADE ON UPDATE CASCADE


)ENGINE=InnoDB DEFAULT CHARSET=latin1;

SET FOREIGN_KEY_CHECKS = 1;

客户:

@Entity
@Table(name = "customer")
public class Customer {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    private int id;

    ....

    @ManyToMany(fetch = FetchType.EAGER, cascade = { CascadeType.PERSIST, CascadeType.DETACH, CascadeType.REFRESH,
            CascadeType.MERGE })
    @JoinTable(name = "customer_coupon", joinColumns = @JoinColumn(name = "customer_id", referencedColumnName = "id"), inverseJoinColumns = @JoinColumn(name = "coupon_id", referencedColumnName = "id"))
    private List<Coupon> coupons;

优惠券:

@Entity
@Table(name = "coupon")
public class Coupon {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    private int id;

    ......

    @ManyToOne(fetch = FetchType.EAGER, cascade = { CascadeType.PERSIST, CascadeType.DETACH, CascadeType.REFRESH,
            CascadeType.MERGE })
    @JoinColumn(name = "company_id")
    private Company company;

    @ManyToMany(mappedBy = "coupons")
    private List<Customer> customers;

客户控制器:

@RequestMapping(value = "/purchaseCoupon")
    public ResponseEntity<CouponSystemResponse> purchaseCoupon(@RequestParam(value = "id") int id) {
            Coupon coupon = couponService.getCoupon(id);
            getEntity().getCoupons().add(coupon); -->getEntity() gets the customer
            customerService.updateCustomer(getEntity());

客户服务:

@Service
public class CustomerService {

    @Autowired
    CustomerRepository customerRepo;

    .....

    public void updateCustomer(Customer customer) {
        customerRepo.save(customer);
    }

每次我尝试向客户添加优惠券时,都会收到这个烦人的错误:

Multiple representations of the same entity [com.orel.couponsystem.entity.Coupon#2] are being merged. Managed: [Coupon [id=2, title=test[X][Y]2, startDate=2014-02-14 00:00:00.0, endDate=2018-02-14 00:00:00.0, amount=5, type=SPORTS, message=test, price=12.0]]; Detached: [Coupon [id=2, title=test[X][Y]2, startDate=2014-02-14 00:00:00.0, endDate=2018-02-14 00:00:00.0, amount=5, type=SPORTS, message=test, price=12.0]]; nested exception is java.lang.IllegalStateException: Multiple representations of the same entity [com.orel.couponsystem.entity.Coupon#2] are being merged. Managed: [Coupon [id=2, title=test[X][Y]2, startDate=2014-02-14 00:00:00.0, endDate=2018-02-14 00:00:00.0, amount=5, type=SPORTS, message=test, price=12.0]]; Detached: [Coupon [id=2, title=test[X][Y]2, startDate=2014-02-14 00:00:00.0, endDate=2018-02-14 00:00:00.0, amount=5, type=SPORTS, message=test, price=12.0]]

出现此错误的原因是什么?如何修复?正如我所说,我试图删除 CascadeType.MERGE 没有运气,看到了一些关于 equals 方法但不能真正理解的东西,感谢任何帮助!

【问题讨论】:

    标签: mysql spring hibernate jpa spring-data-jpa


    【解决方案1】:

    问题解决了:

    更改了这一行: customerService.updateCustomer(getEntity());

    对此: customerService.updateCustomer(customerService.getCustomer(getEntity().getId())

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-03-14
      • 1970-01-01
      • 2017-02-26
      • 2019-06-08
      • 1970-01-01
      • 2020-04-11
      • 2021-11-01
      • 2019-01-31
      相关资源
      最近更新 更多