有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java JPA多通一对多约束冲突

我有两个实体Fichier和Category:

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;

...

分类。爪哇

@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;
}

    ....

梅因。爪哇

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]

该错误表示违反了完整性约束,但为什么? 我已经尝试将child持久化到parent之前,但仍然存在相同的错误


共 (1) 个答案

  1. # 1 楼答案

    我认为您应该显式地设置@Id列的值,或者使用一些自动生成策略来映射它