Link to home
Start Free TrialLog in
Avatar of Jay Roy
Jay RoyFlag for United States of America

asked on

unable to save hibernate inner objects

hi guys

I am unable to save/update my inner object

I have workflow object, workflow object has Template object and
template object has ArrayList of Questions

when i say session.saveOrUpdate(workflow);
the template object is updated BUT the questions doesnt get updated

my flow is object1-->contains object2-->contains list3.

when saveorupdate is performed on object1, object2 gets persisted but list3 is not persisted. any idea where i am missing?

workflow.hbm.xml
<many-to-one name="template" class="com.hibernate.entity.Template"   cascade="save-update" lazy="false" fetch="select">
            <column name="TEMPLATE_ID" />
        </many-to-one>


template hbm.xml
<bag name="questions" table="QUESTION" inverse="true" lazy="false" fetch="select" >
            <key>
                <column name="TEMPLATE_ID" not-null="true" />
            </key>
            <one-to-many class="com.hibernate.entity.Question"  />
        </bag>

Is the cascade="save-update" causing the template to be updated but the List of question inside the template doesnt get updated.

Been strugling for few days, any help greatly apprecated
thanks
Avatar of for_yan
for_yan
Flag of United States of America image



Perhaps you want to read this article on realted subject on saving related objects in Hibernate:
http://www.kkaok.pe.kr/doc/hibernate/reference/html/example-parentchild.html
Avatar of josephtsang
josephtsang

The problem is on inverse="true". It means saving template does not necessarily save the associated questions, rather it defers the save operation to each question individually.
Avatar of Jay Roy

ASKER

inverse="true".
since the relationship is bidirectional,i have to specify inverse="true"
inverse="true" means you want the relationship ONLY be maintained by the Question side, not the Template.

If you remove this inverse="true", both Question and Template may maintain the relationship upon save().

Therefore this is not something for specifying a bidirectional relationship. Rather it is used for restricting ONLY one side will be responsible for the relationship maintenance.
Avatar of Jay Roy

ASKER

Ok got it. Thanks. Can u tell me what are the benefits of giving inverse=true?
When should I use it and when should I avoid it? Certainly in above case it looks
Like I need to avoid it.
Thx
One case of using inverse="true" is about the Parent-child relationship maintenance. You may read this for the elaboration. In your case Template is the parent and Questions are its children.

The other case of using inverse="true" is about the performance. Without this whenever we save Template we also save the associated Questions in the arraylist. The situation gets worse when the questions in the arraylist are many but we have only modified a few of them. You may see a lot of redundant SQL update statements processed for those not having any changes. On the other hand, if we put inverse="true" on Template such that the save operation will not be "cascaded" from Template to Questions, it means we need to save Questions individually in our program and ease out that performance issue just mentioned.
Avatar of Jay Roy

ASKER

so if i dont specify inverse="true" the save is cascaded into the inner levels for example
From template to Questions.

BUT if i dont specify inverse="true", then all the questions are saved/updated which i dont want
i want only that specific question to be updated.

Is my understanding correct?
and should i specify  inverse="true"  in <bag name="questions" table="QUESTION" inverse="true" lazy="false" fetch="select" >  ?

thanks
ASKER CERTIFIED SOLUTION
Avatar of josephtsang
josephtsang

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Jay Roy

ASKER

great
works fine.

any help with the following question will be greatly appreciated

https://www.experts-exchange.com/questions/27074814/query-Objects-in-hibernate.html

thanks