简体   繁体   English

违反外键约束JPA

[英]Foreign key constraint violation JPA

I have my auction house project and 3 entities here: 我在这里有我的拍卖行项目和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);
}

Adding data to database works smoothly but removing linked rows works only for UserAccount. 将数据添加到数据库的过程很顺利,但是删除链接的行仅适用于UserAccount。 Firstly I create user and then I add auctions with offers. 首先,我创建用户,然后添加带有报价的拍卖。 When I try to remove user's auction error appears: 当我尝试删除用户的拍卖错误时:

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]

Can you tell me what is the solution, am I right thinking it's relations fault, auctions and users should not share offers in this case? 您能否告诉我解决方案是什么,我是否正确地认为这是关系错误,在这种情况下拍卖和用户不应共享报价? Testing code: 测试代码:

    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);

It seems it's using EJB spring + hibernate solution in your project. 看来您的项目中正在使用EJB spring + hibernate解决方案。 Maybe there are 2 tables user and offer in database. 数据库中可能有2个表的用户和提供。 and they are related by foreign key. 并且它们通过外键关联。 The reason of current error is not relations fault. 当前错误的原因不是关系故障。 You need to check that the "On Delete" field of foreign key is set to "CASCADE" in user and offer tables. 您需要检查用户表和报价表中外键的“删除时”字段是否设置为“ CASCADE”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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