У меня есть две сущности 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)";...
ошибка указывает на нарушение ограничений целостности, но почему?
единственная возможность, была ли вставка сущности B сделана перед A ...
Любая помощь, пожалуйста?
Заранее спасибо.
EDIT :
решено просто заменить это:
@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;