首页 > 解决方案 > 外键约束违反 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);

标签: javajpajakarta-ee

解决方案


它似乎在您的项目中使用 EJB spring + hibernate 解决方案。数据库中可能有 2 个表 user 和 offer。它们是通过外键关联的。当前错误的原因不是关系错误。您需要检查用户和报价表中外键的“删除时”字段是否设置为“CASCADE”。


推荐阅读