Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » EclipseLink » eclipselink does two inserts and one update when using cyclic references
eclipselink does two inserts and one update when using cyclic references [message #669831] Wed, 11 May 2011 14:04 Go to next message
Luiz E. is currently offline Luiz E.
Messages: 97
Registered: September 2010
Member
hi all
i'm using cyclic references in a model like this:

@Entity
public class LancamentoNF extends BaseEntityObject implements Serializable {
  @ManyToOne(cascade={CascadeType.PERSIST})
  //@JoinColumn(name="lancamentos")
  private MovFiscalContabil lancamentos = new MovFiscalContabil();



@Entity
@Table(name ="movfiscalcontabil")
public class MovFiscalContabil extends BaseEntityObject implements Serializable {

  @ManyToOne
  //@JoinColumn(name="id")
  private LancamentoNF lancamentoNF;


but when i save the entity (LancamentoNF) the following occurs:
[EL Fine]: 2011-05-11 14:57:06.484--Connection(25881278)--INSERT INTO movfiscalcontabil (TIPOIMPOSTO, VALORICMS, VALORIPI, VALORDEBITO, VALORCREDITO, lancamentoscontabeis_id, empresas_id, planoContaDebito, LANCAMENTONF_ID, movimentoduplicatas_id, itensnf_id, planoContaIcms, planoContaIpi, planoContaCredito) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
[EL Fine]: 2011-05-11 14:57:06.484--Connection(25881278)--	bind => [null, 10.0, 10.0, 1010.0, 990.0, null, null, null, null, null, null, null, null, null]
[EL Fine]: 2011-05-11 14:57:06.484--Connection(25881278)--select lastval()
[EL Fine]: 2011-05-11 14:57:06.484--Connection(25881278)--INSERT INTO LANCAMENTONF (CLIENTEFORNECEDORCOMPLEMENTO, BASECALCULO, CLIENTEFORNECEDORCEP, VALORIPI, CLIENTEFORNECEDORNUMERO, VALORTOTALNOTA, CLIENTEFORNECEDORINSCRICAOESTADUAL, CLIENTEFORNECEDORINSCRICAOMUNICIPAL, CLIENTEFORNECEDORLETRA, TIPODOCUMENTO, CLIENTEFORNECEDORMUNICIPIO, CLIENTEFORNECEDORNM, VALORPARCIALNOTA, CLIENTEFORNECEDORTIPOLOGRADOURO, CLIENTEFORNECEDORENDERECO, VALORICMS, NUMERO, SERIE, CLIENTEFORNECEDORFANTASIA, DATALANCAMENTO, DATAEMISSAO, CLIENTEFORNECEDORENDERECOCOMPLETO, CLIENTEFORNECEDORCPFCNPJ, ESPECIE, CLIENTEFORNECEDORUF, cfop_id, parametrosCFOP, tipologradouro_id, clientefornecedor_id, LANCAMENTOS_MOVFISCALCONTABIL_ID, modelonf_id, empresas_id, condicaopagamento_id, municipios_id) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
[EL Fine]: 2011-05-11 14:57:06.484--Connection(25881278)--	bind => [Casa de esquina, 0.0, 85904130, 10.0, 1, 1010.0, , , a, 1, Exterior, Diogo Bla, 1000.0, Avenida, dasdsada, 10.0, 344, AB, Diogo, 2011-05-11, 2011-05-11, Avenida dasdsada, 1 - Centro - Exterior/Exterior, 051.092.549-90, NF, PR, 252, null, null, 3, 4, 40, 1, 1, null]
[EL Fine]: 2011-05-11 14:57:06.5--Connection(25881278)--select lastval()
[EL Fine]: 2011-05-11 14:57:06.5--Connection(25881278)--UPDATE movfiscalcontabil SET planoContaIcms = ?, VALORICMS = ?, VALORCREDITO = ?, VALORDEBITO = ?, planoContaCredito = ?, planoContaIpi = ?, LANCAMENTONF_ID = ?, VALORIPI = ?, empresas_id = ?, planoContaDebito = ? WHERE (MOVFISCALCONTABIL_ID = ?)
[EL Fine]: 2011-05-11 14:57:06.5--Connection(25881278)--	bind => [376, 10.0, 990.0, 1010.0, 120, 377, 6, 10.0, 1, 4, 4]



as the log says, he does a insert, then another insert, and then a update. but the problem is that for me, that update doesn't make any sense! I've debugged the app before saves the LancamentoNF entity, and the MovFiscalContabil is pretty fine!
i tried with and without the @joincolumn but had no success.
any ideas?

thanks a lot

[Updated on: Wed, 11 May 2011 14:11]

Report message to a moderator

Re: eclipselink does two inserts and one update when using cyclic references [message #670053 is a reply to message #669831] Thu, 12 May 2011 10:05 Go to previous messageGo to next message
Chris Delahunt is currently offline Chris Delahunt
Messages: 1016
Registered: July 2009
Senior Member
Hello,

I am not sure which version of EclipseLink you are using, but when a cycle is detected it will do a shallow insert and then update the relationship fields later. Is this what you are seeing in the update statement?

Best Regards,
Chris
Re: eclipselink does two inserts and one update when using cyclic references [message #670054 is a reply to message #669831] Thu, 12 May 2011 10:07 Go to previous messageGo to next message
James Sutherland is currently offline James Sutherland
Messages: 1939
Registered: July 2009
Location: Ottawa, Canada
Senior Member

The log looks correct. Because you have a foreign key relationship in both directions you have made it impossible to insert into the tables, to resolve this EclipseLink does a "shallow" insert, then an update of the foreign keys to ensure the foreign key constraints are not violated.

This seems to be working correctly, what is your issue?

In general you model seems wrong, it seems the two objects have a OneToOne relationship not a ManyToOne, you should use a OneToOne and have only one foreign key and use mappedBy in the other.


James : Wiki : Book : Blog : Twitter
Re: eclipselink does two inserts and one update when using cyclic references [message #671278 is a reply to message #669831] Tue, 17 May 2011 07:07 Go to previous messageGo to next message
Luiz E. is currently offline Luiz E.
Messages: 97
Registered: September 2010
Member
@James
can i put @OneToMany on a object? isn't OneToMany for lists?
which one should had a @OneToMany?
thanks a lot!
Re: eclipselink does two inserts and one update when using cyclic references [message #671936 is a reply to message #671278] Thu, 19 May 2011 07:56 Go to previous message
James Sutherland is currently offline James Sutherland
Messages: 1939
Registered: July 2009
Location: Ottawa, Canada
Senior Member

I said your model seems like you should have two @OneToOne, not a @OneToMany or @ManyToOne.
@OneToMany, @ManyToOne is for when one side has a collection, and the other an object.
@OneToOne is for when both sides have an object.


James : Wiki : Book : Blog : Twitter
Previous Topic:Multi-column, one-to-one, JPA 2.0 @EmbeddedId using @MapsId fails with read-only on @JoinColum
Next Topic: @NamedStoredProcedureQuery and StoredProcedureCall problem
Goto Forum:
  


Current Time: Fri Aug 01 07:55:24 EDT 2014

Powered by FUDForum. Page generated in 0.01886 seconds