我正在使用Spring Boot应用程序的H2数据库。我创建了一个帐户服务类,用于保留account,account_type和customer的数据。请在下面找到代码:
默认构造函数,Getter Setter,使用所有字段的构造函数,toString,hashCode,等式出现在下面的每个类中。
充当实体的帐户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;
}
用作实体以及数据传输对象(即DTO)的AcountType POJO
@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 (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
请更正我对错误的理解。我认为此错误即将到来,因为没有在数据库中创建“客户”和“帐户类型”记录,因此当在数据库中插入“帐户”时,无法找到帐户类型ID,因此会引发此错误。
答案 0 :(得分:0)
是的,如果不存在具有匹配ID的实体,则将违反完整性约束。
如果要实现级联创建相关实体,则应在关系映射中添加bool isMailIdAlreadyExist = matches.Any(m => !m);
bool isUserNameAlreadyExist = matches.LastOrDefault();
。例如:
cascade
但是即使那样,当您发送具有特定ID的JSON数据并且它们在数据库中不存在时,也会出现此错误。