我们有两个表GeneralInfo
和DetailedInfo
。
G_Id
是GeneralInfo
的主键和DetailedInfo
的外键。
DetailedInfo
有一个主键D_Info
两个表之间的关系从GeneralInfo
到DetailedInfo
为OneToMany
,从DetailedInfo
到GeneralInfo
为ManyToOne
。
在排除时我得到以下错误:
ERROR: Cannot add or update a child row: a foreign key constraint fails
(`Info`.`DetailedInfo`, CONSTRAINT `DetailedInfo_ibfk_1` FOREIGN KEY (`G_Id`)
REFERENCES `DetailedInfo` (`G_Id`))
org.springframework.web.util.NestedServletException: Request processing failed;
nested exception is javax.persistence.PersistenceException:
org.hibernate.exception.ConstraintViolationException:
could not execute statement
请帮助我们。
在JPA中,我们给出了这样的映射:
@Entity
@Table(name = "GeneralInfo")
public class GeneralInfoEntity extends AbstractEntity {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "G_Id")
private long id;
@OneToMany(cascade=CascadeType.ALL,mappedBy="general")
private List<PatientExternalSystemRefEntity> patientExternalSystemRefEntity;
//follwed by getter and setter
}
@Entity
@Table(name = "DetailedInfo")
public class DetailedInfoEntity extends AbstractEntity implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "D_Info")
private long id;
@Column(name = "G_Id")
private long generalId;
@ManyToOne
@JoinColumn(name = "G_Id",nullable=false,insertable=false,updatable=false)
private GeneralInfoEntity general;
//follwed by getter and setter
}