我的拍卖行项目和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);
答案 0 :(得分:0)
它似乎在您的项目中使用EJB spring + hibernate解决方案。 也许在数据库中有2个表用户和提供。 它们与外键有关。 当前错误的原因不是关系错误。 您需要检查外键的“On Delete”字段是否在用户和商品表中设置为“CASCADE”。