[英]Foreign key constraint violation 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個表的用戶和提供。 並且它們通過外鍵關聯。 當前錯誤的原因不是關系故障。 您需要檢查用戶表和報價表中外鍵的“刪除時”字段是否設置為“ CASCADE”。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.