外键不坚持

时间:2014-10-15 18:08:01

标签: hibernate jpa

我们有两个表GeneralInfoDetailedInfo

G_IdGeneralInfo的主键和DetailedInfo的外键。

DetailedInfo有一个主键D_Info

两个表之间的关系从GeneralInfoDetailedInfoOneToMany,从DetailedInfoGeneralInfoManyToOne

在排除时我得到以下错误:

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

}

0 个答案:

没有答案