PDA

View Full Version : Saving objects


JohnDew
Sep 13th, 2007, 02:59 AM
Hi all,
I am stuck in a situation wherein I have to store Bill and BilledServices object at the same time.
That is, if a bill object is saved using this.getHibernateTemplate().save(bill) even billedServices set in the bill object has to be stored in different table.
Now the bill object is getting stored but the billedServices set is not.
billedServices set has BilledServcies objects. BilledServices object has a field 'bill' of type Bill.
In the hbm file its...
<set name="bServices" inverse="true" order-by="BNo DESC">
<key column="BNo"/>
<one-to-many class="com.a.b.c.BServices"/>
</set>
Above mapping is for the bill table.
<many-to-one name="bill" class="com.a.b.c.Bill">
<column name="BNo"></column>
</many-to-one>
Above mapping is for the billedservices table.
BNo is of type int default null.
Please suggest what can be done.

kajh
Sep 13th, 2007, 04:40 PM
Hi!

You might try to move inverse="true" to the other side of the relationship and add a cascade="save-update" on the bServices set in the config for bill.


-Kaj :)

JohnDew
Sep 14th, 2007, 01:44 AM
Hi,
Thanks Kaj, for your suggestion. I did accordingly. Now I can see entries in the billedservices table. But the thing left out is that BNo is null for all entries. If "BNo" is made not null I am getting the following error:
2007-09-14 10:52:51,812 ERROR [org.hibernate.util.JDBCExceptionReporter] - Column 'BNo' cannot be null
2007-09-14 10:52:51,812 ERROR [org.hibernate.event.def.AbstractFlushingEventListe ner] - Could not synchronize database state with session

This is for the BilledServices table I have done
BNo INT UNSIGNED NOT NULL,
FOREIGN KEY (BNo) ReFERENCES Bill (BNo),
and this is what I have done in hbm.xml file for bill table
<set name="billedServices" cascade="save-update" inverse="true" order-by="BNo DESC">

What can be done?Please suggest.

JohnDew
Sep 14th, 2007, 01:52 AM
and this is what I have done in hbm.xml file for bill table
<set name="billedServices" cascade="save-update" inverse="true" order-by="BNo DESC">

kajh
Sep 14th, 2007, 01:59 AM
Let us not run this thread in two forums... Let us continue here since we started here.

Try to remove the inverse="true" from the mapping for the bill table.

You find a description of inverse at http://www.hibernate.org/hib_docs/reference/en/html/collections.html , see the section "6.3.2. Bidirectional associations".

-Kaj :)

JohnDew
Sep 14th, 2007, 02:17 AM
I tried removing inverse="true",but its not working. I will go through the reference and in case of any queries regarding this I will get back.
Thanks.

JohnDew
Sep 14th, 2007, 03:08 AM
Thanks Kaj, everything is working fine now.
The changes made are:
I included not-null="true"
<many-to-one name="bill" class="com.a.b.c.Bill" column="BNo" not-null="true"/>

When I included this, the ERROR was:
ERROR [com.a.b.c.BillController] - Error while storing the bill due to :org.springframework.orm.hibernate3.HibernateSyste mException: not-null property references a null or transient value: com.a.b.c.BilledServices.bill; nested exception is org.hibernate.PropertyValueException: not-null property references a null or transient value: com.a.b.c.BilledServices.bill

With this error I actually came to know the cause, so I tried setting the bill object of all the billedServices objects in the set of the bill object and it worked fine.

Thanks a lot, for providing the link.

kajh
Sep 14th, 2007, 03:17 AM
Good! :)

I'm glad to hear that you solved this issue!


-Kaj :)