java - 外键约束违反 JPA
问题描述
我在这里有我的拍卖行项目和 3 个实体:
public class Auction implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
@ManyToOne
private UserAccount user; //nie daje sie id tylko obiekt
private String description;
private String title;
private double price;
@OneToMany(mappedBy = "auction", orphanRemoval = true, cascade = CascadeType.ALL, fetch = FetchType.EAGER)
private List<Offer> offers = new LinkedList<Offer>();
private Timestamp creationTimestamp = new Timestamp(System.currentTimeMillis());
private Timestamp expirationTimestamp;
public void addOffer(Offer o) {
offers.add(o);
o.setAuction(this);
}
public class Offer implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private double price;
@ManyToOne
private Auction auction;
@ManyToOne
private UserAccount user;
private Timestamp creationTimestamp = new Timestamp(System.currentTimeMillis());
public class UserAccount implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String username;
private String email;
@OneToMany(mappedBy = "user", orphanRemoval = true, cascade = CascadeType.ALL, fetch = FetchType.EAGER)
private List<Offer> offers = new LinkedList<Offer>();
@OneToMany(mappedBy = "user",fetch = FetchType.EAGER, orphanRemoval = true, cascade = CascadeType.ALL)
private List<Auction> auctions = new LinkedList<Auction>();
public void addAuction(Auction a) {
auctions.add(a);
a.setUser(this);
}
public void addOffer(Offer o, Auction a) {
offers.add(o);
o.setUser(this);
o.setAuction(a);
}
将数据添加到数据库可以顺利进行,但删除链接行仅适用于 UserAccount。首先,我创建用户,然后添加带有报价的拍卖。当我尝试删除用户的拍卖错误时出现:
Internal Exception: org.apache.derby.shared.common.error.DerbySQLIntegrityConstraintViolationException: DELETE on table 'AUCTION' caused a violation of foreign key constraint 'OFFER_AUCTION_ID' for key (1). The statement has been rolled back.
Error Code: 20000
Call: DELETE FROM AUCTION WHERE (ID = ?)
bind => [1 parameter bound]
您能告诉我解决方案是什么吗,我是否认为这是关系错误,在这种情况下拍卖和用户不应该共享报价?测试代码:
UserAccount user = new UserAccount();
user.setUsername("filip");
user.setEmail("example100@gmail.com");
Offer offer = new Offer();
offer.setPrice(2200d);
Offer offer2 = new Offer();
offer.setPrice(2500d);
Auction auction = new Auction();
auction.setDescription("opel na sprzedaz");
auction.setPrice(2000d);
auction.setTitle("opelek na sprzedaz");
auction.addOffer(offer);
auction.setExpirationTimestamp(new Timestamp(System.currentTimeMillis() - 86400000l));
user.addOffer(offer, auction);
user.addOffer(offer2, auction);
user.addAuction(auction);
userEjb.save(user);
解决方案
它似乎在您的项目中使用 EJB spring + hibernate 解决方案。数据库中可能有 2 个表 user 和 offer。它们是通过外键关联的。当前错误的原因不是关系错误。您需要检查用户和报价表中外键的“删除时”字段是否设置为“CASCADE”。
推荐阅读
- linux-device-driver - 为 I2C 设备加载平台驱动程序时,该设备是否需要存在?
- lua - temp:7: 尝试索引全局“mon”(一个 nil 值)
- linux - 如何将一个函数连接到不同的函数上?
- haskell - Haskell 中惰性和并行性如何共存?
- python - 根据帖子是否喜欢添加不同的图像
- python - 如何禁用仅在将 .py 转换为 .exe 后才显示的 python 警告?
- javascript - CSS中设置的圆半径,如何通过JS获取值?
- c++ - c++ Vector push_back 移动新元素
- javascript - 如何使多个 document.getElementById() 在同一页面上工作?
- r - 如何在 R 中为连续模式标记组?