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
|