I have two entities A and 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;
...
the bPK field is a Composite Primary Key:
@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 code:
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
);
the main code:
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();
I get this error:
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)";...
the error indicates a violation of integrity constrains, but why?
a single possibility whether the insertion of the entity B is made before A…
Any help please?
Thank you in advance.
EDIT:
solved just replace this :
@JoinColumn(name = "A_IDA", referencedColumnName = "IDA", nullable = false, insertable = false, updatable = false)
by this one:
@MapsId("aIda")
finally 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;
You have the A_AID field controlled by the aIda attribute in the Embeddable – that means this attribute must be set with the value from A before you can persist B.
If you are using using JPA 2.0, you can mark the @ManyToOne with the @MapsId(“aIda”) which will allow you to remove the @JoinColumn for it. This will make the JPA provider set the value in b.bPK.aIda with the value from A on persist.
If you are not using JPA 2,0, you can either set it yourself by first persisting A and then changing your addB method to also set B’s bPK.aIda, or you can change the fields so that the JoinColumn is writable and make the bPK.aIda insertable=false, updatable=false.