JPA ManyToOne OneToMany约束违规

时间:2014-04-08 09:10:42

标签: java jpa persistence openjpa

我有两个实体Fichier和Categorie:

Fichier.java

@Entity
@Table(name="\"Fichier\"")
public class Fichier implements Serializable {
    private static final long serialVersionUID = 1L;

    @Id
    @Column(name="\"idFichier\"", insertable=false, updatable=false)
    private int idFichier;

    ...

    @JoinColumn(name = "\"idCategorie\"", referencedColumnName = "\"idCategorie\"")
    @ManyToOne(cascade = ALL )
    private Categorie categorieParent;

...

和Categorie.java

@Entity
@Table(name="\"Categorie\"")
public class Categorie implements Serializable {
    private static final long serialVersionUID = 1L;

    @Id
    @Column(name="\"idCategorie\"", insertable=false, updatable=false)
    private int idCategorie;

    ...
    @OneToMany(mappedBy = "categorieParent", cascade = ALL)
    private List<Categorie> listeCategorie = new ArrayList<Categorie>();
    @JoinColumn(name = "\"idCatParent\"", referencedColumnName = "\"idCategorie\"")
    @ManyToOne()
    private Categorie categorieParent;
    //bi-directional many-to-one association to Fichier
    @OneToMany(mappedBy="categorieParent", cascade = ALL)
    private List<Fichier> listeFichier;

    ...

    public Categorie addListeCategorie(Categorie listeCategorie){
        getListeCategorie().add(listeCategorie);
        listeCategorie.setCategorieParent(this);
        return listeCategorie;
}

    ....

Main.java

Categorie c =new Categorie();
Fichier f= new Fichier();
c.addFichier(f);
em.persist(c);

我收到此错误:

Caused by: org.apache.openjpa.lib.jdbc.ReportingSQLException: THE INSERT OR UPDATE VALUE OF FOREIGN KEY "Schema.Fichier.Contient" , the insert value of a foreign key must be equal to the value of the parent key SQLCODE=-530, SQLSTATE=23503, DRIVER=3.63.123 {prepstmnt -640961458 INSERT INTO "Schema"."Fichier" ("contenu", "description", "niveau", "nom", "type", "idCategorie") VALUES (?, ?, ?, ?, ?, ?)} [code=-530, state=23503]

错误表明违反了完整性限制,但为什么呢? 我已经尝试在父母之前坚持孩子,但仍然是同样的错误。

1 个答案:

答案 0 :(得分:0)

我认为你应该明确地设置@Id列的值,或者用一些自动生成策略来映射它。