【发布时间】:2014-04-08 09:10:42
【问题描述】:
我有两个实体 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]
该错误表明违反了完整性约束,但为什么呢? 我已经尝试在父母之前坚持孩子,但仍然是同样的错误。
【问题讨论】:
-
您的 ID 是如何生成的?没有 @GeneratedValue 注释,您也没有在代码中设置它们。
-
我在 Fichier 和 Categorie 中添加了
@GeneratedValue(strategy = GenerationType.IDENTITY)但我收到此错误Caused by: org.apache.openjpa.lib.jdbc.ReportingSQLException: "IDCATEGORIE" is not permitted in this context..数据库已配置为生成 id。 -
您可以将策略设置为
AUTO- 然后提供商应选择适用的策略。你能验证你的实体在持久化之前没有 ID 吗? -
不添加 Fichier 到 Categorie,我可以坚持 Categorie。 AUTO 不起作用,因为它找不到默认的提供者策略。即使我不将 Fichier 添加到类别中,身份也不起作用。完整错误是
Caused by: org.apache.openjpa.lib.jdbc.ReportingSQLException: "IDCATEGORIE" n'est pas autorisé dans le contexte dans lequel il est utilisé.. SQLCODE=-206, SQLSTATE=42703, DRIVER=3.63.123 {prepstmnt 301873790 INSERT INTO "Schema"."Categorie" ("description", "nom", "idCatParent") VALUES (?, ?, ?)} [code=-206, state=42703] -
这很奇怪。我没有使用 OpenJPA 的经验,但这可能是一个错误。如果您移除
insertable=false限制并在提交前定义您自己的Id 值会发生什么?
标签: java jpa persistence openjpa