【问题标题】:Foreign key constraint violation JPA外键约束违反 JPA
【发布时间】:2018-05-28 18:29:13
【问题描述】:

我在这里有我的拍卖行项目和 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);

【问题讨论】:

  • 您可能忘记将您创建的报价添加到其所属拍卖的报价列表中。没有代码,我们只能下注。
  • 添加了测试代码,抱歉
  • 正如我所想:您的 UserAccount.addOffer() 方法设置了要约的拍卖,但不会将要约添加到拍卖的要约中。
  • 尝试设置o.setAuction(this);在将其添加到您的列表 offer.add(o) 并告诉它是否有效之前?并对 UserAccount 类中的 addAuction 执行相同操作
  • 非常感谢您,现在可以正常使用了。

标签: java jpa jakarta-ee


【解决方案1】:

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

【讨论】:

  • Spring + EJB (JavaEE) !我认为这不是很好的实现。
猜你喜欢
  • 1970-01-01
  • 2021-05-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-10-24
  • 2018-04-04
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多