Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
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 Go to next message
Antonio Sobalvarro is currently offline Antonio SobalvarroFriend
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
Re: OneToMany does not populate Foreign Key or null foreign key [message #516982 is a reply to message #516675] Thu, 25 February 2010 16:49 Go to previous messageGo to next message
Chris Delahunt is currently offline Chris DelahuntFriend
Messages: 1389
Registered: July 2009
Senior Member
Hello,

From the looks of it, EclipseLink is not inserting the foreign key values it has for the TestHijo entity (the constraint is on codigo_upc_hijo which isn't in the insert, and id_empresa is being set because it is writable through the EmbeddedId mapping). Are there any other relationships involved that you are not showing? EclipseLink might perform a shallow inserts if it determines that there is a circular reference dependencies. If that is the case, you can set the database to delay constraint checking until the end of the transaction, as EclipseLink will update to correct the foreign keys after all the inserts are complete.

Best Regards,
Chris
Re: OneToMany does not populate Foreign Key or null foreign key [message #517020 is a reply to message #516982] Thu, 25 February 2010 18:44 Go to previous messageGo to next message
Antonio Sobalvarro is currently offline Antonio SobalvarroFriend
Messages: 4
Registered: February 2010
Location: Guatemala
Junior Member
Thank you Chris,

There are no additional relationships at database level nor entity level.

If you check in the insert statement within the generated error, the column codigo_upc_hijo is not included, so I believe EclipseLink is not generating this field for the insert.

But I did two additional tests.

I included a mapping for codigo_upc_hijo inside the embeddId TestHIjoPK for TestHijo, and TestPadre with TestHijo are persisted OK. But this should not be, as I am including fields which are not part of the primary key.

Another test is that I included codigo_upc_hijo field mapping within TestHijo, and I set the foreign key value just before the persist operation, and again the persist operation works.

I don´t have a clue what is going on when using the correct mapping.

Regards
Re: OneToMany does not populate Foreign Key or null foreign key [message #517289 is a reply to message #516675] Fri, 26 February 2010 19:49 Go to previous message
Chris Delahunt is currently offline Chris DelahuntFriend
Messages: 1389
Registered: July 2009
Senior Member
Sorry, I should noticed the version and that part of the mapping is marked as updatable/insertable=false. EclipseLink would mark the whole mapping as read-only if part of it was marked updatable/insertable=false, which is what you are encountering. This was fixed in bug 280436 for the 2.0 release.

Try using EclipseLink 2.0 or changing the mappings slightly so that the relationship is completely writable and the basic mappings insertable/updatable=false as neccessary.

Best Regards,
Chris
Previous Topic:Using Observable Lists within entity classes?
Next Topic:EclipseLink and Dynamic Entities
Goto Forum:
  


Current Time: Fri Apr 19 23:26:43 GMT 2024

Powered by FUDForum. Page generated in 0.03068 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top