连续3天,我坚持这个问题,我试着尽可能清楚地解释:
我正在进行软件库存管理,我使用EclipseLink(JPA2.0)来管理数据库。
问题是当我用相关文章创建新发票时
,并尝试坚持他们,然后我得到一个Referential integrity constraint violation Exception
...
真正的问题是我用netbeans生成了所有实体(因为我不熟悉注释) 我无法确认它们是否正确生成(但我仍怀疑)....
表SQL:
CREATE TABLE fact_proforma (
idfact_proforma INT UNSIGNED NOT NULL AUTO_INCREMENT,
utilisateur_login VARCHAR(25) NOT NULL,
client_idclient INT UNSIGNED NOT NULL,
date DATE NOT NULL,
PRIMARY KEY(idfact_proforma),
FOREIGN KEY(client_idclient)
REFERENCES client(idclient)
ON DELETE NO ACTION
ON UPDATE CASCADE,
FOREIGN KEY(utilisateur_login)
REFERENCES utilisateur(login)
ON DELETE NO ACTION
ON UPDATE CASCADE
);
CREATE TABLE fact_proforma_has_article (
fact_proforma_idfact_proforma INT UNSIGNED NOT NULL,
article_idarticle VARCHAR(40) NOT NULL,
prix_ht DOUBLE NOT NULL,
qte DOUBLE NOT NULL,
remise DOUBLE NOT NULL,
marge_benef DOUBLE NOT NULL,
PRIMARY KEY(fact_proforma_idfact_proforma, article_idarticle),
FOREIGN KEY(fact_proforma_idfact_proforma)
REFERENCES fact_proforma(idfact_proforma)
ON DELETE CASCADE
ON UPDATE CASCADE,
FOREIGN KEY(article_idarticle)
REFERENCES article(idarticle)
ON DELETE NO ACTION
ON UPDATE CASCADE
);
CREATE TABLE article (
idarticle VARCHAR(40) NOT NULL,
libel VARCHAR(100) NOT NULL,
prix_ht DOUBLE NOT NULL,
tva_idtva DOUBLE NOT NULL,
qte DOUBLE NOT NULL,
min_qte DOUBLE NOT NULL,
marge_benef DOUBLE NOT NULL,
remise DOUBLE NOT NULL,
unite_idunite VARCHAR(10) NOT NULL,
famille_idfamille VARCHAR(50) NOT NULL,
etat CHAR(1) NOT NULL,
PRIMARY KEY(idarticle),
FOREIGN KEY(tva_idtva)
REFERENCES tva(idtva)
ON DELETE NO ACTION
ON UPDATE CASCADE,
FOREIGN KEY(famille_idfamille)
REFERENCES famille(idfamille)
ON DELETE NO ACTION
ON UPDATE CASCADE,
FOREIGN KEY(unite_idunite)
REFERENCES unite(idunite)
ON DELETE NO ACTION
ON UPDATE CASCADE
);
CREATE TABLE utilisateur (
login VARCHAR(25) NOT NULL,
pass VARCHAR(15) NOT NULL,
class CHAR(1) NOT NULL,
etat CHAR(1) NOT NULL,
PRIMARY KEY(login)
);
FactProforma.java://“Facture Proforma”是“形式发票”
public class FactProforma implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Basic(optional = false)
@Column(name = "IDFACT_PROFORMA", nullable = false)
private Integer idfactProforma;
@Basic(optional = false)
@Column(name = "DATE", nullable = false)
@Temporal(TemporalType.DATE)
private Date date;
@OneToMany(cascade=CascadeType.ALL , mappedBy = "factProforma")
private List<FactProformaHasArticle> factProformaHasArticleList;
@JoinColumn(name = "UTILISATEUR_LOGIN", referencedColumnName = "LOGIN", nullable = false)
@ManyToOne(optional = false)
private Utilisateur utilisateurLogin;
@JoinColumn(name = "CLIENT_IDCLIENT", referencedColumnName = "IDCLIENT", nullable = false)
@ManyToOne(optional = false)
private Client clientIdclient;
FactProformaHasArticle.java
public class FactProformaHasArticle implements Serializable {
private static final long serialVersionUID = 1L;
@EmbeddedId
protected FactProformaHasArticlePK factProformaHasArticlePK;
@Basic(optional = false)
@Column(name = "PRIX_HT", nullable = false)
private double prixHt;
@Basic(optional = false)
@Column(name = "QTE", nullable = false)
private double qte;
@Basic(optional = false)
@Column(name = "REMISE", nullable = false)
private double remise;
@Basic(optional = false)
@Column(name = "MARGE_BENEF", nullable = false)
private double margeBenef;
@JoinColumn(name = "FACT_PROFORMA_IDFACT_PROFORMA", referencedColumnName = "IDFACT_PROFORMA", nullable = false, insertable = false, updatable = false)
@ManyToOne(optional = false)
private FactProforma factProforma;
@JoinColumn(name = "ARTICLE_IDARTICLE", referencedColumnName = "IDARTICLE", nullable = false, insertable = false, updatable = false)
@ManyToOne(optional = false)
private Article article;
FactProformaHasArticlePK.java
@Embeddable
public class FactProformaHasArticlePK implements Serializable {
@Basic(optional = false)
@Column(name = "FACT_PROFORMA_IDFACT_PROFORMA", nullable = false)
private int factProformaIdfactProforma;
@Basic(optional = false)
@Column(name = "ARTICLE_IDARTICLE", nullable = false, length = 40)
private String articleIdarticle;
我的代码:
FactProforma factpro=new FactProforma(null, new Date());
Utilisateur user=new Utilisateur(loginActuel);
Client client=new Client(Integer.parseInt(codeClient.getText()));
java.util.List<FactProformaHasArticle> ListOfArticles =this.c.GetPanier(dtm,factpro);
factpro.setClientIdclient(client);
factpro.setFactProformaHasArticleList(ListOfArticles);
factpro.setUtilisateurLogin(user);
EntityManager em= emf.createEntityManager();
em.getTransaction().begin();
em.persist(factpro);
em.getTransaction().commit();
堆栈跟踪:
Exception in thread "AWT-EventQueue-0" javax.persistence.RollbackException: Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.3.0.v20110604-r9504): org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: org.h2.jdbc.JdbcSQLException: Referential integrity constraint violation: "CONSTRAINT_9A: PUBLIC.FACT_PROFORMA_HAS_ARTICLE FOREIGN KEY(FACT_PROFORMA_IDFACT_PROFORMA) REFERENCES PUBLIC.FACT_PROFORMA(IDFACT_PROFORMA)"; SQL statement:
Internal Exception: org.h2.jdbc.JdbcSQLException: Referential integrity constraint violation: "CONSTRAINT_9A: PUBLIC.FACT_PROFORMA_HAS_ARTICLE FOREIGN KEY(FACT_PROFORMA_IDFACT_PROFORMA) REFERENCES PUBLIC.FACT_PROFORMA(IDFACT_PROFORMA)"; SQL statement:
INSERT INTO TEST.PUBLIC.FACT_PROFORMA_HAS_ARTICLE (MARGE_BENEF, PRIX_HT, QTE, REMISE, ARTICLE_IDARTICLE, FACT_PROFORMA_IDFACT_PROFORMA) VALUES (?, ?, ?, ?, ?, ?) [23506-164]
INSERT INTO TEST.PUBLIC.FACT_PROFORMA_HAS_ARTICLE (MARGE_BENEF, PRIX_HT, QTE, REMISE, ARTICLE_IDARTICLE, FACT_PROFORMA_IDFACT_PROFORMA) VALUES (?, ?, ?, ?, ?, ?) [23506-164]
at org.h2.message.DbException.getJdbcSQLException(DbException.java:329)
at org.h2.message.DbException.getJdbcSQLException(DbException.java:329)
at org.h2.message.DbException.get(DbException.java:169)
at org.h2.message.DbException.get(DbException.java:169)
at org.h2.message.DbException.get(DbException.java:146)
at org.h2.message.DbException.get(DbException.java:146)
at org.h2.constraint.ConstraintReferential.checkRowOwnTable(ConstraintReferential.java:345)
at org.h2.constraint.ConstraintReferential.checkRowOwnTable(ConstraintReferential.java:345)
at org.h2.constraint.ConstraintReferential.checkRow(ConstraintReferential.java:287)
at org.h2.constraint.ConstraintReferential.checkRow(ConstraintReferential.java:287)
at org.h2.table.Table.fireConstraints(Table.java:862)
at org.h2.table.Table.fireConstraints(Table.java:862)
at org.h2.table.Table.fireAfterRow(Table.java:879)
at org.h2.table.Table.fireAfterRow(Table.java:879)
at org.h2.command.dml.Insert.insertRows(Insert.java:126)
at org.h2.command.dml.Insert.update(Insert.java:84)
at org.h2.command.dml.Insert.insertRows(Insert.java:126)
at org.h2.command.CommandContainer.update(CommandContainer.java:73)
at org.h2.command.dml.Insert.update(Insert.java:84)
at org.h2.command.Command.executeUpdate(Command.java:226)
at org.h2.command.CommandContainer.update(CommandContainer.java:73)
at org.h2.server.TcpServerThread.process(TcpServerThread.java:325)
at org.h2.command.Command.executeUpdate(Command.java:226)
at org.h2.server.TcpServerThread.run(TcpServerThread.java:146)
at java.lang.Thread.run(Thread.java:722 at org.h2.server.TcpServerThread.process(TcpServerThread.java:325)
)
Error Code: 23506
Call: INSERT INTO TEST.PUBLIC.FACT_PROFORMA_HAS_ARTICLE (MARGE_BENEF, PRIX_HT, QTE, REMISE, ARTICLE_IDARTICLE, FACT_PROFORMA_IDFACT_PROFORMA) VALUES (?, ?, ?, ?, ?, ?)
at org.h2.server.TcpServerThread.run(TcpServerThread.java:146)
bind => [6 parameters bound]
at java.lang.Thread.run(Thread.java:722)
明天我会尝试另一个DBMS,以避免冲突......
PS:我很抱歉法语。我别无选择。
更新: 它起作用了:
EntityManager em= emf.createEntityManager();
em.getTransaction().begin();
em.persist(fact);
em.flush();
for(FactProformaHasArticle couple: estComposeFacture)
{
couple.getFactProformaHasArticlePK().setFactProformaIdfactProforma(fact.getIdfactProforma());
em.persist(couple);
}
em.getTransaction().commit();
答案 0 :(得分:1)
已编辑的帖子: 您需要确保FactProformaHasArticle和FactProforma之间的关系已被完全引用。我怀疑你需要有这样的东西才能坚持下去:
List<FactProformaHasArticle> ListOfArticles =this.c.GetPanier(dtm,factpro)
for(FactProformaHasArticle fpha: ListOfArticles) {
fpha.setFactProforma(factpro);
}
(注意:成员变量的第一个字母的小写字母会更好,即
listOfArticles
代替ListOfArticles
等)
INITIAL POST(如果它也适用于您的情况):
您是否尝试从主键的注释中删除optional=false
和nullable=false
?我曾经遇到过这个问题:由于这个原因,JPA不允许MyEntity me=new MyEntity(null);
。尝试类似:
public class FactProforma implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
// removed:@Basic(optional = false)
@Column(name = "IDFACT_PROFORMA") // removed: , nullable = false)
private Integer idfactProforma;
// rest should be ok
}
答案 1 :(得分:1)
你已经有了一个解决方案,但我想我之所以提到它之所以有用是因为你有通过EmbeddedId的基本映射控制的主键字段 - 所以即使你可能在持久化之前设置了关系,pk字段将为null,直到embeddedId的articleIdarticle手动设置为值。这是将“ARTICLE_IDARTICLE”映射两次的问题 - 两个映射都应该由应用程序维护,并且在持久化FactProformaHasArticle之前需要使用pk值。
JPA 2.0使这个设置更容易,因为你可以在关系上使用@MapsId注释来显示它控制embeddedId的基本映射 - 所以你只需要设置关系并让JPA为你设置字段。或者你可以删除embeddedId,将对象用作PKclass并直接用@Id标记关系,如下所述:
http://en.wikibooks.org/wiki/Java_Persistence/Identity_and_Sequencing#JPA_2.0 使用此选项将允许您删除刷新调用,因为JPA将为您设置pk值。