简体   繁体   English

H2参照完整性约束违反

[英]H2 Referential integrity constraint violation

I am using H2 database of spring boot application. 我正在使用Spring Boot应用程序的H2数据库。 I have created one account service class which is used to persist data of account, account_type and customer. 我创建了一个帐户服务类,用于保留account,account_type和customer的数据。 Please find the code below: 请在下面找到代码:

Default constructor,Getter Setter,Constructor using all Fields,toString,hashCode,equals are present in each of the below class. 默认构造函数,Getter Setter,使用所有字段的构造函数,toString,hashCode,等式存在于下面的每个类中。

A Account POJO serving as an Entity as well as a Data Transfer Object ie DTO 充当实体的帐户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;
}

A AcountType POJO serving as an Entity as well as a Data Transfer Object ie DTO 用作实体的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;

}

A Customer POJO serving as an Entity as well as a Data Transfer Object ie DTO 客户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;
}

when I am trying to make POST call to create customer , account and account type as below : 当我尝试进行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
}

It's failing with below error. 失败并显示以下错误。

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 (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)

Please correct my understanding of the error. 请更正我对错误的理解。 I think this error is coming because Customer and ACcount Type record are not created in DB and hence when Account is getting inserted in DB, it's unable to find account type id and hence this error is thrown. 我认为此错误即将到来,因为没有在数据库中创建“客户”和“帐户类型”记录,因此当在数据库中插入“帐户”时,无法找到帐户类型ID,因此会引发此错误。

Yes, if the entities with matching IDs are not present you will get integrity constraint violation. 是的,如果不存在具有匹配ID的实体,则将违反完整性约束。

If you want to achieve cascade creation of related entities you should add cascade to your relational mappings. 如果要实现相关实体的级联创建,则应将cascade添加到关系映射中。 For instance: 例如:

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

But even then when you send JSON data with specific IDs and they don't exist in DB you will get this error. 但是即使这样,当您发送带有特定ID的JSON数据并且它们在数据库中不存在时,也会出现此错误。

暂无
暂无

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

相关问题 Hibernate / H2 @OneToMany移除子代时违反“参照完整性约束”? - Hibernate/H2 @OneToMany “Referential integrity constraint violation” on remove of child? 更新和/或删除时违反 Hibernate H2 参照完整性约束 - Hibernate H2 Referential Integrity Constraint Violation on Update and/or Remove 违反参照完整性约束:删除 Hibernate 中的实体时(memory DBMS 中的 H2) - Referential integrity constraint violation: when deleting Entity in Hibernate (H2 in memory DBMS) 无继承关系的参照完整性约束违规 - Referential integrity constraint violation for none inheritance relationship 如何防止在测试中违反参照完整性约束? - How to prevent referential integrity constraint violation in tests? 参照完整性约束违规 - 添加超过2条记录时失败 - Referential integrity constraint violation - failing when add more then 2 records 所有者删除时违反参照完整性约束(OneToMany单向) - Referential integrity constraint violation on owner delete (OneToMany unidirectional) Spring 引导 - 测试存储库 - 违反参照完整性约束 - Spring Boot - testing repository - Referential integrity constraint violation JPA 2:在保存具有无方向OneToMany关系的实体时违反了引用完整性约束 - JPA 2: Referential integrity constraint violation while saving entity with undirectional OneToMany relationship 当删除带有播放框架2.2.x的对象时,违反引用完整性约束 - Referential integrity constraint violation when deleting an object with play-framework 2.2.x
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM