JPA2 / EclipseLink OneToMany级联持久性问题“参照完整性约束违规”

时间:2012-03-26 00:22:34

标签: jpa eclipselink

我有两个实体A和B:

A.java:

...
public class A implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Basic(optional = false)
@Column(name = "IDA", nullable = false)
private Integer ida;
@Basic(optional = false)
@Column(name = "NAME", nullable = false, length = 30)
private String name;
@OneToMany(cascade = CascadeType.ALL, mappedBy = "a")
private List<B> bList=new ArrayList();

public void addB(B bp){
bp.setA(this);
bList.add(bp);
}
...

B.java:

...
public class B implements Serializable {
private static final long serialVersionUID = 1L;
@EmbeddedId
protected BPK bPK;
@Basic(optional = false)
@Column(name = "NAME", nullable = false, length = 30)
private String name;
@JoinColumn(name = "A_IDA", referencedColumnName = "IDA", nullable = false, insertable = false, updatable = false)
@ManyToOne(optional = false)
private A a;
...

bPK字段是复合主键:

@Embeddable
public class BPK implements Serializable {
@Basic(optional = false)
@Column(name = "IDB", nullable = false, length = 20)
private String idb;
@Basic(optional = false)
@Column(name = "A_IDA", nullable = false)
private int aIda;

public BPK() {
}

public BPK(String idb, int aIda) {
    this.idb = idb;
    this.aIda = aIda;
}
...

SQL代码:

CREATE TABLE A (
idA INTEGER UNSIGNED NOT NULL AUTO_INCREMENT,
name VARCHAR(30) NOT NULL,
PRIMARY KEY(idA)
);

CREATE TABLE B (
idB VARCHAR(20) NOT NULL,
A_idA INTEGER UNSIGNED NOT NULL,
name VARCHAR(30) NOT NULL,
PRIMARY KEY(idB, A_idA),
FOREIGN KEY(A_idA)
REFERENCES A(idA)
  ON DELETE NO ACTION
  ON UPDATE NO ACTION
);

主要代码:

   A a=new A(null,"A1");
   BPK bpk=new BPK();
   bpk.setIdb("b1");
   a.addB(new B(bpk,"B1"));

   EntityManager em=getEntityManager();
   em.getTransaction().begin();
   em.persist(a);
   em.getTransaction().commit();

我收到此错误:

 Exception in thread "main" javax.persistence.RollbackException: Exception  [EclipseLink-4002] (Eclipse Persistence Services - 2.3.0.v20110604-r9504):    org.eclipse.persistence.exceptions.DatabaseException
 [EL Warning]: 2012-03-26 01:29:16.724--UnitOfWork(1902320872)--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_42_1: PUBLIC.B FOREIGN KEY(A_IDA) REFERENCES PUBLIC.A(IDA)"; SQL  statement:
 Internal Exception: org.h2.jdbc.JdbcSQLException: Referential integrity constraint   violation: "CONSTRAINT_42_1: PUBLIC.B FOREIGN KEY(A_IDA) REFERENCES PUBLIC.A(IDA)";...

错误表明违反了完整性限制,但为什么呢? 是否在A之前插入实体B的可能性 有什么帮助吗? 提前谢谢。

修改: 解决了这个问题:

 @JoinColumn(name = "A_IDA", referencedColumnName = "IDA", nullable = false, insertable = false, updatable = false)

这一个:

 @MapsId("aIda")

最后B.java:

 @NamedQuery(name = "B.findByName", query = "SELECT b FROM B b WHERE b.name = :name")})
 public class B implements Serializable {
 private static final long serialVersionUID = 1L;
 @EmbeddedId
 protected BPK bPK;
 @Basic(optional = false)
 @Column(name = "NAME", nullable = false, length = 30)
 private String name;
 @MapsId("aIda")
 @ManyToOne(optional = false)
 private A a;

1 个答案:

答案 0 :(得分:1)

您可以使用Embeddable中的aIda属性控制A_AID字段 - 这意味着必须先使用A中的值设置此属性,然后才能保留B.

如果您使用的是JPA 2.0,则可以使用@MapsId(“aIda”)标记@ManyToOne,这样您就可以删除@JoinColumn。这将使JPA提供程序在b.bPK.aIda中设置值,并保留A中的值。

如果您没有使用JPA 2,0,您可以先自己设置A然后再更改addB方法以设置B的bPK.aIda,也可以更改字段以便JoinColumn可写并且使bPK.aIda insertable = false,updatable = false。