我已经在这里找了很多帖子,但仍然不能解决这个问题。有人说您需要删除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方法,但不能真正理解它,感谢任何帮助的家伙!
1条答案
按热度按时间emeijp431#
问题解决:
更改了此行:customerservice.updatecustomer(getentity());
对此:customerservice.updatecustomer(customerservice.getcustomer(getentity().getid())