-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: Composite-key hell
PostPosted: Mon Jun 20, 2005 11:10 am 
Newbie

Joined: Mon Jun 20, 2005 11:07 am
Posts: 16
Hi everyone

I just have a no-sleep nite because of composite-key relationships in hibernate.
I have this relationship working fine, that is basically a PurchaseTicket(NotaCompra) and a BuyingItem(ItemCompra), with a Product(Produto).
The PurchaseTicket has a one-to-many with the BuyingItem, where the PT has all the priviledges to persist their BuyingItem Objects.
With a saveNotaCompra()(my method to persist the PurchaseTicket), I'm able to persist the entire PurchaseTicket, with his BuyingItens.
The sql in the tests are in the right order (first inserting the PurchaseTicket, next inserting all the BuyingItens,then updating the Products).

But last nite I've tried to do a mimic use-case, which was the Sell Flow.
I did the SellingTicket(NotaVenda), the SellingItens(ItemVenda) and used the same Product.
But this strange behavior is happening when I try to save the SellingTicket:

[junit] [cintex2] DEBUG [main] NotaVendaDAOHibernate.saveNotaVenda(48) |
[junit] Class - NotaVendaDAOHibernate
[junit] Method - saveNotaVenda
[junit] Hibernate: insert into item_venda (version, data_movimento, desconto
, quantidade, valor, codigo_nota_venda, id_produto) values (?, ?, ?, ?, ?, ?, ?)

[junit] [cintex2] WARN [main] JDBCExceptionReporter.logExceptions(57) | SQL
Error: 1216, SQLState: 23000
[junit] [cintex2] ERROR [main] JDBCExceptionReporter.logExceptions(58) | Dup
licate key or integrity constraint violation message from server: "Cannot add or
update a child row: a foreign key constraint fails"
[junit] [cintex2] WARN [main] JDBCExceptionReporter.logExceptions(57) | SQL
Error: 1216, SQLState: 23000
[junit] [cintex2] ERROR [main] JDBCExceptionReporter.logExceptions(58) | Dup
licate key or integrity constraint violation message from server: "Cannot add or
update a child row: a foreign key constraint fails"
[junit] [cintex2] ERROR [main] SessionImpl.execute(2399) | Could not synchro
nize database state with session
[junit] [cintex2] WARN [main] SQLErrorCodeSQLExceptionTranslator.translate(2
79) | Unable to translate SQLException with errorCode '1216', will now try the f
allback translator
[junit] ------------- ---------------- ---------------
[junit] Testcase: testGetNotaVenda(com.cintex2.dao.NotaVendaDAOTest):
Caused an ERROR
[junit] (Hibernate operation): data integrity violated by SQL ''; nested exc
eption is java.sql.BatchUpdateException: Duplicate key or integrity constraint v
iolation message from server: "Cannot add or update a child row: a foreign key c
onstraint fails"
[junit] org.springframework.dao.DataIntegrityViolationException: (Hibernate
operation): data integrity violated by SQL ''; nested exception is java.sql.Batc
hUpdateException: Duplicate key or integrity constraint violation message from s
erver: "Cannot add or update a child row: a foreign key constraint fails"
[junit] java.sql.BatchUpdateException: Duplicate key or integrity constraint
violation message from server: "Cannot add or update a child row: a foreign key
constraint fails"

I know the SellingItem(ItemVenda) has to be saved BEFORE the SellingTicket(NotaVenda),
not like this strange behavior, but I had switched the properties in my merge mapping(I've written
a complete mapping of BuyingItem(ItemCompra) and SellingItem(ItemVenda), because of the composite-keys stuff.
I've checked MANY MANY times if the inverse was false, and it is still false.

I'll keep trying to solve this, and will post the solution here to prevent this kind of behavior to anyone else.

Best Regards

Rafael Mauricio Nami


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 21, 2005 2:13 am 
Newbie

Joined: Mon Jun 20, 2005 11:07 am
Posts: 16
RESOLVED.

I've remembered that with composite keys I have to use getHibernateTemplate.save(Object o) instead of getHibernateTemplate().saveOrUpdate(Object o).

OBS - I'm using Spring.

Best Regards

Rafael Mauricio Nami


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
cron
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.