I have a table Invoice. Then I have a table sales. The invoice table has Primary Key as Invoice Number. Sales table holds all the items or sales for a particular invoice.
The invoice Number field of the sales table has a fkey relation with the invoice number of the Invoice table.
I have made the Invoice number of the Invoice table be auto incremented.
The problem I am facing is , using Hibernate, I am trying to create a record in the invoice table use that invoice number for creating records in the sales table. My invoice.hbm.xml looks as follows
Somehow the application is trying to insert new records first into the sales table then into the Invoice table. Because of this its getting the invoice number field as null.
please suggest me what chould I do so that either the order of creation of tables is reverse or a workaround to created the sales record and then update them with the invoice number.
Please use the Code tags for your posts, without the indentation xml and code is really hard to read and figure where one object stops and another begins. There are buttons a the top of posting that have all the tags you could possibly want. Including the Code tags.
Ok I will try that one.
But one question, it still tries to create records in Sales table first and still wont find the invoicenumber, how would that be handled? Because during my trial and error, all through yesterday I tried that as well, and dont think it worked. But may be trying to get it working repeatedly I must have missed out something, I will give it a shot again today.
posted 10 years ago
Sorry for not using the code tag, shall do that from now.
posted 10 years ago
Its still not working. Following are the changes I made.
Note, I tried different ways of creating the many-to-one relation, like giving the column name as well etc. the inverse tag was only applicable for the one-to-many side of the relation, so I have put it on the Invoice hbm.
Exception org.hibernate.AssertionFailure: null id in com.postsales.domain.model.Sales entry (don't flush the Session after an exception occurs)
Right now as a workaround, I am treating these as two separate tables, removed the relationship mapping from the hbm, I have the transaction management at the service level so I believed that it will handle things, of making it as a single transaction, and so updating them separately as two tables just as we do in database just go and insert one record, and use that id and update the table with the foreign key.
In my Dao, I have the following:
I know thats a bit crude way of doing it, but its working for me. Do comment on my xml if you think any changes would help me.
The only thing that kept the leeches off of me was this tiny ad:
Devious Experiments for a Truly Passive Greenhouse!