Home » Eclipse Projects » EclipseLink » OneToMany does not populate Foreign Key or null foreign key
OneToMany does not populate Foreign Key or null foreign key [message #516675] |
Wed, 24 February 2010 18:15 |
Antonio Sobalvarro Messages: 4 Registered: February 2010 Location: Guatemala |
Junior Member |
|
|
Hi,
I have a bidirectional oneToMany relationship.
TestPadre has many TestHijo.
One joinColumn codigo_upc_hijo is not part of the primary key at the owning side of relationship.
That joinColumn gets null value when trying to persist.
THIS IS THE DATABASE DEFINITION
CREATE TABLE test_padre
(
id_empresa character varying(25) NOT NULL DEFAULT ''::character varying,
codigo_upc character varying(50) NOT NULL DEFAULT ''::character varying,
nombre character varying(300) NOT NULL DEFAULT ''::character varying,
CONSTRAINT key7 PRIMARY KEY (codigo_upc, id_empresa)
)
CREATE TABLE test_hijo
(
codigo_sku character varying(50) NOT NULL DEFAULT ''::character varying,
id_empresa character varying(25) NOT NULL DEFAULT ''::character varying,
codigo_upc_hijo character varying(50) NOT NULL DEFAULT ''::character varying,
unidad_medida character varying(25) NOT NULL DEFAULT ''::character varying,
CONSTRAINT key8 PRIMARY KEY (id_empresa, codigo_sku),
CONSTRAINT fk_test FOREIGN KEY (codigo_upc_hijo, id_empresa)
REFERENCES test_padre (codigo_upc, id_empresa) MATCH SIMPLE
ON UPDATE RESTRICT ON DELETE RESTRICT
)
THESE ARE THE ENTITY MAPPINGS
public class TestPadre implements Serializable {
private static final long serialVersionUID = 1L;
@EmbeddedId
protected TestPadrePK testPadrePK;
@Basic(optional = false)
@Column(name = "nombre")
private String nombre;
@OneToMany(cascade = CascadeType.ALL, mappedBy = "testPadre", fetch= FetchType.EAGER)
private List<TestHijo> testHijoCollection;
(get,set....)
@Embeddable
public class TestPadrePK implements Serializable {
@Basic(optional = false)
@Column(name = "id_empresa")
private String idEmpresa;
@Basic(optional = false)
@Column(name = "codigo_upc")
private String codigoUpc;
public class TestHijo implements Serializable {
private static final long serialVersionUID = 1L;
@EmbeddedId
protected TestHijoPK testHijoPK;
@Basic(optional = false)
@Column(name = "unidad_medida")
private String unidadMedida;
@JoinColumns({@JoinColumn(name = "codigo_upc_hijo", referencedColumnName = "codigo_upc", nullable=false), @JoinColumn(name = "id_empresa", referencedColumnName = "id_empresa", insertable = false, updatable = false)})
@ManyToOne(optional = false)
private TestPadre testPadre;
(get,set...)
@Embeddable
public class TestHijoPK implements Serializable {
@Basic(optional = false)
@Column(name = "codigo_sku")
private String codigoSku;
@Basic(optional = false)
@Column(name = "id_empresa")
private String idEmpresa;
The data to be persisted is
TestPadre padre = new TestPadre();
TestPadrePK padrePk = new TestPadrePK("se001", "200");
padre.setTestPadrePK(padrePk);
padre.setNombre("nombre de Test");
padre.setTestHijoCollection(new ArrayList<TestHijo>());
TestHijo hijo = new TestHijo();
TestHijoPK hijoPK = new TestHijoPK("200", "se001", "200");
hijo.setTestHijoPK(hijoPK);
hijo.setUnidadMedida("unidad Medida");
hijo.setTestPadre(padre);
padre.getTestHijoCollection().add(hijo);
The code to persist is
public TestPadre insertTest(TestPadre padre) {
for (TestHijo hijo : padre.getTestHijoCollection()) {
hijo.setTestPadre(padre);
}
em.persist(padre);
return padre;
}
When I try to persist TestPadre I get the following error
Exception [EclipseLink-4002] (Eclipse Persistence Services - 1.2.0.v20091016-r5565): org.eclipse.persistence.exceptions.DatabaseException Internal Exception: org.postgresql.util.PSQLException: ERROR: insert or update on table "test_hijo" violates foreign key constraint "fk_test" Detail: Key (codigo_upc_hijo,id_empresa)=(,se001) is not present in table "test_padre". Error Code: 0 Call: INSERT IGNORE INTO test_hijo (unidad_medida, id_empresa, codigo_sku) VALUES (?, ?, ?) bind => [unidad Medida, se001, 200] Query: InsertObjectQuery(test.TestHijo[testHijoPK=test.TestHijoPK[c odigoSku=200, idEmpresa=se001]]) at org.eclipse.persistence.exceptions.DatabaseException.sqlExce ption(DatabaseException.java:333) at
.....
What Am I doing wrong ?
thank you
|
|
| | | |
Goto Forum:
Current Time: Fri Apr 19 23:26:43 GMT 2024
Powered by FUDForum. Page generated in 0.03068 seconds
|