[
Date Prev][
Date Next][
Thread Prev][
Thread Next][
Date Index][
Thread Index]
[
List Home]
Re: [eclipselink-users] OneToMany does not populate Foreign Key or null foreign key
|
It looks like the TestHijo is being inserted correctly, but the TestPadre is
not getting inserted first. Enable logging and see if the insert is logged
for the TestPadre.
AntonioJSG wrote:
>
> 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 INTO test_hijo (unidad_medida,
> id_empresa, codigo_sku) VALUES (?, ?, ?) bind => [unidad Medida, se001,
> 200] Query:
> InsertObjectQuery(test.TestHijo[testHijoPK=test.TestHijoPK[codigoSku=200,
> idEmpresa=se001]]) at
> org.eclipse.persistence.exceptions.DatabaseException.sqlException(DatabaseException.java:333)
> at
> .....
>
> What Am I doing wrong ?
>
> thank you
>
>
>
-----
http://wiki.eclipse.org/User:James.sutherland.oracle.com James Sutherland
http://www.eclipse.org/eclipselink/
EclipseLink , http://www.oracle.com/technology/products/ias/toplink/
TopLink
Wiki: http://wiki.eclipse.org/EclipseLink EclipseLink ,
http://wiki.oracle.com/page/TopLink TopLink
Forums: http://forums.oracle.com/forums/forum.jspa?forumID=48 TopLink ,
http://www.nabble.com/EclipseLink-f26430.html EclipseLink
Book: http://en.wikibooks.org/wiki/Java_Persistence Java Persistence
--
View this message in context: http://old.nabble.com/OneToMany-does-not-populate-Foreign-Key-or-null-foreign-key-tp27714271p27748130.html
Sent from the EclipseLink - Users mailing list archive at Nabble.com.