H2参考完整性约束违反



我正在使用spring引导应用程序的H2数据库。我创建了一个帐户服务类,用于持久化account、account_type和customer的数据。请找到以下代码:

以下每个类中都存在默认构造函数、Getter Setter、constructor using all Fields、toString、hashCode、equals。

作为实体的账户POJO以及数据传输对象,即DTO

@Entity
@Table(name = "ACCOUNT")
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class Account {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;
@OneToOne
@JoinColumn(name="account_type_id")
private AccountType accountType;
@Temporal(TemporalType.DATE)
@Column(name = "date_created", unique = true, nullable = false, length = 10)
private Date dateCreated;
@Column(nullable = false)
private double originalCreditAmount;
@Column(nullable = false)
private double balanceAmount;
@Column(nullable = false)
private boolean fullyPaid;
@Column(nullable = false)
private int term;
@Column(nullable = false)
private float rateOfInterest;
@Column(nullable = false)
private boolean escrowAttached;
@Column(nullable = false)
private boolean pmiAttached;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "customer_id", nullable = false)
@JsonBackReference
Customer customer;
}

AcountType POJO用作实体和数据传输对象,即DTO

@Entity
@Table(name = "ACCOUNT_TYPE")
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class AccountType {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;
@Column(nullable = false)
private String accountTypeName;
@Column(nullable = false)
private String accountTypeDescription;
}

作为实体的客户POJO以及数据传输对象,即DTO

@Entity
@Table(name = "customer")
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class Customer {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;
@Column(nullable = false)
private String firstName;
@Column(nullable = false)
private String lastName;
@Column(nullable = false)
private String socialSecurityNumber;
@Temporal(TemporalType.DATE)
@Column(name = "dob", unique = true, nullable = false, length = 10)
private Date dateOfBirth;
@Column(nullable = false)
private double totalLoanAmount;
@Column(nullable = false)
private int bonusPoints;
@Temporal(TemporalType.DATE)
@Column(name = "customer_since", unique = true, nullable = false, length = 10)
private Date memberSince;
@OneToMany(fetch = FetchType.LAZY, mappedBy = "customer")
@JsonManagedReference
private Set<Address> addresses = new HashSet<Address>();
@OneToMany(fetch = FetchType.LAZY, mappedBy = "customer")
@JsonManagedReference
private Set<Account> accounts = new HashSet<Account>();
@OneToMany(fetch = FetchType.LAZY, mappedBy = "customer")
@JsonManagedReference
private Set<Contact> contacts = new HashSet<Contact>();

@OneToMany(fetch = FetchType.LAZY, mappedBy = "customer")
@JsonManagedReference
private Set<Education> education = new HashSet<Education>();
@OneToMany(fetch = FetchType.LAZY, mappedBy = "customer")
@JsonManagedReference
private Set<Employment> employment = new HashSet<Employment>();
@OneToMany(fetch = FetchType.LAZY, mappedBy = "customer")
@JsonManagedReference
private Set<Investment> investments = new HashSet<Investment>();

@OneToMany(fetch = FetchType.LAZY, mappedBy = "customer")
@JsonManagedReference
private Set<Liability> liabilities = new HashSet<Liability>();

@Column()
private int rating;
}

当我试图进行POST调用以创建客户、帐户和帐户类型时,如下所示:

{
"customer": {
"id": 1,
"firstName": "Jordaya",
"lastName": "Scott",
"dateOfBirth": "1980-04-12",
"totalLoanAmount": 287000,
"bonusPoints": 70000,
"memberSince": "2000-04-11",
"socialSecurityNumber": "449-84-4944",
"rating": 7
},
"accountType": {
"id": 2,
"accountTypeName": "Savings A/C",
"accountTypeDescription": "Savings Account"
},
"dateCreated": "2017-01-01",
"originalCreditAmount": 300000,
"balanceAmount": 200000,
"fullyPaid": false,
"term": 30,
"rateOfInterest": 3.25,
"escrowAttached": false,
"pmiAttached": false
}

它失败了,出现以下错误。

org.h2.jdbc.JdbcSQLException: Referential integrity constraint violation: "FKGW84MGPACW9HTDXCS2J1P7U6J: PUBLIC.ACCOUNT FOREIGN KEY(ACCOUNT_TYPE_ID) REFERENCES PUBLIC.ACCOUNT_TYPE(ID) (2)"; SQL statement:
insert into account (account_type_id, balance_amount, customer_id, date_created, escrow_attached, fully_paid, original_credit_amount, pmi_attached, rate_of_interest, term, id) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)

请纠正我对错误的理解。我认为这个错误是因为Customer和ACcount Type记录没有在DB中创建,因此当ACcount被插入DB时,它无法找到帐户类型id,因此引发了这个错误。

是的,如果具有匹配ID的实体不存在,则会违反完整性约束。

如果您想实现相关实体的级联创建,您应该将cascade添加到关系映射中。例如:

@OneToOne(cascade = CascadeType.ALL...)

但即便如此,当您发送带有特定ID的JSON数据,而这些数据在DB中不存在时,您也会收到此错误。

最新更新