0
ちょっとデータをデータベースに挿入しようとしていますが、エラーが発生しました。子行を追加または更新できません:Hibernateで外部キー制約が失敗します
は、子行を追加または更新できません:外部キー制約が失敗した(。pos
をexchange_order_detail
、CONSTRAINT FK_5ed8cb725cbc40e2be44a4ab3d9
FOREIGN KEY(GOODS_DETAIL_ID2
)がgoods_detail
(ID
)を参照)
ここに私のコードは
package com.istana.pos.db.domain;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
@SuppressWarnings("serial")
@Entity
@Table(name = "EXCHANGE_ORDER_DETAIL", schema = "pos")
public class ExchangeOrderDetail implements java.io.Serializable {
private int id;
private String exchangeNumber;
private Integer goodsDetailId1;
private Integer goodsDetailId2;
private double price1;
private double price2;
private double discount1;
private double discount2;
private ExchangeOrder exchangeOrder;
private GoodsDetail goodsDetail1;
private GoodsDetail goodsDetail2;
@Id
@Column(name = "ID", unique = true, nullable = false)
@GeneratedValue(strategy = GenerationType.AUTO)
public int getId() {
return this.id;
}
public void setId(int id) {
this.id = id;
}
@Column(name = "EXCHANGE_NUMBER", nullable = false)
public String getExchangeNumber() {
return exchangeNumber;
}
public void setExchangeNumber(String exchangeNumber) {
this.exchangeNumber = exchangeNumber;
}
@Column(name = "GOODS_DETAIL_ID1", nullable = false)
public Integer getGoodsDetailId1() {
return goodsDetailId1;
}
public void setGoodsDetailId1(int goodsDetailId1) {
this.goodsDetailId1 = goodsDetailId1;
}
@Column(name = "GOODS_DETAIL_ID2", nullable = false)
public Integer getGoodsDetailId2() {
return goodsDetailId2;
}
public void setGoodsDetailId2(int goodsDetailId2) {
this.goodsDetailId2 = goodsDetailId2;
}
@Column(name = "PRICE1")
public double getPrice1() {
return price1;
}
public void setPrice1(double price1) {
this.price1 = price1;
}
@Column(name = "PRICE2")
public double getPrice2() {
return price2;
}
public void setPrice2(double price2) {
this.price2 = price2;
}
@Column(name = "DISCOUNT1")
public double getDiscount1() {
return discount1;
}
public void setDiscount1(double discount1) {
this.discount1 = discount1;
}
@Column(name = "DISCOUNT2")
public double getDiscount2() {
return discount2;
}
public void setDiscount2(double discount2) {
this.discount2 = discount2;
}
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "EXCHANGE_NUMBER", nullable = true, insertable = false, updatable = false, referencedColumnName = "EXCHANGE_NUMBER")
public ExchangeOrder getExchangeOrder() {
return exchangeOrder;
}
public void setExchangeOrder(ExchangeOrder exchangeOrder) {
this.exchangeOrder = exchangeOrder;
}
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "GOODS_DETAIL_ID1", nullable = false, insertable = false, updatable = false, referencedColumnName = "ID")
public GoodsDetail getGoodsDetail1() {
return goodsDetail1;
}
public void setGoodsDetail1(GoodsDetail goodsDetail1) {
this.goodsDetail1 = goodsDetail1;
}
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "GOODS_DETAIL_ID2", nullable = false, insertable = false, updatable = false, referencedColumnName = "ID")
public GoodsDetail getGoodsDetail2() {
return goodsDetail2;
}
public void setGoodsDetail2(GoodsDetail goodsDetail2) {
this.goodsDetail2 = goodsDetail2;
}
}
別ですJavaクラス
package com.istana.pos.db.domain;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
@SuppressWarnings("serial")
@Entity
@Table(name = "EXCHANGE_ORDER_DETAIL", schema = "pos")
public class ExchangeOrderDetail implements java.io.Serializable {
private int id;
private String exchangeNumber;
private Integer goodsDetailId1;
private Integer goodsDetailId2;
private double price1;
private double price2;
private double discount1;
private double discount2;
private ExchangeOrder exchangeOrder;
private GoodsDetail goodsDetail1;
private GoodsDetail goodsDetail2;
@Id
@Column(name = "ID", unique = true, nullable = false)
@GeneratedValue(strategy = GenerationType.AUTO)
public int getId() {
return this.id;
}
public void setId(int id) {
this.id = id;
}
@Column(name = "EXCHANGE_NUMBER", nullable = false)
public String getExchangeNumber() {
return exchangeNumber;
}
public void setExchangeNumber(String exchangeNumber) {
this.exchangeNumber = exchangeNumber;
}
@Column(name = "GOODS_DETAIL_ID1", nullable = false)
public Integer getGoodsDetailId1() {
return goodsDetailId1;
}
public void setGoodsDetailId1(int goodsDetailId1) {
this.goodsDetailId1 = goodsDetailId1;
}
@Column(name = "GOODS_DETAIL_ID2", nullable = false)
public Integer getGoodsDetailId2() {
return goodsDetailId2;
}
public void setGoodsDetailId2(int goodsDetailId2) {
this.goodsDetailId2 = goodsDetailId2;
}
@Column(name = "PRICE1")
public double getPrice1() {
return price1;
}
public void setPrice1(double price1) {
this.price1 = price1;
}
@Column(name = "PRICE2")
public double getPrice2() {
return price2;
}
public void setPrice2(double price2) {
this.price2 = price2;
}
@Column(name = "DISCOUNT1")
public double getDiscount1() {
return discount1;
}
public void setDiscount1(double discount1) {
this.discount1 = discount1;
}
@Column(name = "DISCOUNT2")
public double getDiscount2() {
return discount2;
}
public void setDiscount2(double discount2) {
this.discount2 = discount2;
}
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "EXCHANGE_NUMBER", nullable = true, insertable = false, updatable = false, referencedColumnName = "EXCHANGE_NUMBER")
public ExchangeOrder getExchangeOrder() {
return exchangeOrder;
}
public void setExchangeOrder(ExchangeOrder exchangeOrder) {
this.exchangeOrder = exchangeOrder;
}
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "GOODS_DETAIL_ID1", nullable = false, insertable = false, updatable = false, referencedColumnName = "ID")
public GoodsDetail getGoodsDetail1() {
return goodsDetail1;
}
public void setGoodsDetail1(GoodsDetail goodsDetail1) {
this.goodsDetail1 = goodsDetail1;
}
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "GOODS_DETAIL_ID2", nullable = false, insertable = false, updatable = false, referencedColumnName = "ID")
public GoodsDetail getGoodsDetail2() {
return goodsDetail2;
}
public void setGoodsDetail2(GoodsDetail goodsDetail2) {
this.goodsDetail2 = goodsDetail2;
}
}
そしてここにデータを作成するサービスがあります
public ExchangeOrder createExchangeOrder(ExchangeOrder domain, List<ExchangeOrderDetail> details, List<BankPayment> bankPayments) {
ExchangeOrder newDomain = exchangeOrderDao.saveNew(domain);
if (domain != null) {
for (ExchangeOrderDetail detail : details) {
System.out.println("saving exchange order 2");
if (d.getGoodsDetail2() != null) {
detail.setExchangeNumber(newDomain.getExchangeNumber());
detail.setExchangeOrder(newDomain);
exchangeOrderDetailDao.saveNew(detail);
}
}
for (BankPayment bp : bankPayments) {
bp.setTransactionNumber(domain.getExchangeNumber());
bankPaymentDao.saveNew(bp);
}
}
return newDomain;
}
ありがとうございます。