繁体   English   中英

违反外键约束JPA

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM