-->
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.  [ 13 posts ] 
Author Message
 Post subject: when save parent, the children do not get saved
PostPosted: Thu Oct 25, 2007 1:44 pm 
Beginner
Beginner

Joined: Thu Oct 25, 2007 1:21 pm
Posts: 29
Hi,

I have a question and it could be a very simple/silly question for you. Please reply back to my post immediately as soon as possible. Please treat this as very urgent. Thanks a lot for the help in advance.

When I save a parent, the children do not get saved, unless i save them independently. Is there anyway that i can bypass saving children independently? I just wanted to make one call to save parent which should take care of saving children as well. please guide me. This is a unidirectional one-to-many mapping.


<hibernate-mapping>
<class name="Parent" lazy="true" proxy="Parent" table="parent">
<cache usage="read-write"/>
<id name="id" access="field" type="long" >
<generator class="MyGenerator"></generator>
</id>
<!-- some properties here -->
<set name="children" cascade="all,delete-orphan"
access="field"
table="child">
<cache usage="read-write"/>
<key column="parentid" not-null="true"/>
<one-to-many class="Child"/>
</set>
</class>
</hibernate-mapping>

here is the code :

Session session1 = sf.openSession();
Parent parent1 = (Parent )session1.load(Parent.class, 123);
parent parent2 = new Parent(parent1);
session1 .flush();
Session session2 = sf.openSession();
session2 .save(parent2);
session2 .flush();

Please let me know what is wrong here. Thanks a lot.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 25, 2007 3:49 pm 
Newbie

Joined: Thu Oct 25, 2007 3:37 pm
Posts: 17
I'm not familiar with the 'all' setting on the cascade attribute. I use save-update to make this happen in very simliar code. Another thing might be your atcual java objects; when you add the child to the parent, does your logic connect all the references in the two objects as necessary.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 25, 2007 3:59 pm 
Beginner
Beginner

Joined: Thu Oct 25, 2007 1:21 pm
Posts: 29
Hi Tom,

I really appreicate your quick reply.

This is the code i am using. Is this the one you are asking me how child and parent are connected together in the code? Please clarify me here once again. Thanks for your reponse.

public Parent(Parent another) {
this.setName(another.getName());
this.children = new HashSet();
for(Child anotherChild : another.getChildren()) {
Child child = new Child();
child.setName(anotherChild.getName());
children.add(child);
}
}


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 26, 2007 4:50 am 
Newbie

Joined: Tue Oct 23, 2007 4:00 am
Posts: 9
Leela, Lets try this

public Parent(Parent another) {
this.setName(another.getName());
this.children = new HashSet();
for(Child anotherChild : another.getChildren()) {
Child child = new Child();
child.setName(anotherChild.getName());
children.add(child);
}
parent.getChildren().add(Child);
}


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 26, 2007 4:50 am 
Newbie

Joined: Tue Oct 23, 2007 4:00 am
Posts: 9
Leela, Lets try this

public Parent(Parent another) {
this.setName(another.getName());
this.children = new HashSet();
for(Child anotherChild : another.getChildren()) {
Child child = new Child();
child.setName(anotherChild.getName());
children.add(child);
}
parent.setChildren(children);
}


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 26, 2007 1:33 pm 
Beginner
Beginner

Joined: Thu Oct 25, 2007 1:21 pm
Posts: 29
Hi Babu,

Thanks a lot for the help. Appreciate it.

I have tried both ways and unfortunately, i could not get that working.

I do not know how I can solve this issue....

Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Sun Oct 28, 2007 2:09 pm 
Regular
Regular

Joined: Wed Aug 25, 2004 7:40 pm
Posts: 65
leela wrote:
parent parent2 = new Parent(parent1);


How this constructor works? Does the new instance of the model object get the same id from the old one?

BTW, I guess you mean " Parent parent2 = new Parent(parent1); "


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 29, 2007 7:47 am 
Newbie

Joined: Mon Oct 29, 2007 6:38 am
Posts: 1
The policy is that when the set of children is added to the parent , the child must also hold the instance of the parent.


i.e means if we have
children.add(child);
// we also have to set the instance of the parent in the child.
child.setParent(parent);
//after adding all the child to the set
parent.setChildren(children);

And then we should call save on parent to save the parent and the chid records.

Thanks.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 29, 2007 10:16 am 
Regular
Regular

Joined: Mon Jan 22, 2007 10:32 am
Posts: 101
Can you post the mapping for Child also?

_________________
Please rate this post if you find it helpful


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 29, 2007 2:52 pm 
Beginner
Beginner

Joined: Thu Oct 25, 2007 1:21 pm
Posts: 29
Hi vw729,

Yes it copies the id also from old parent when i create a new object. Extremely sorry for the late reply.

Thanks


Last edited by leela on Mon Oct 29, 2007 2:55 pm, edited 1 time in total.

Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 29, 2007 2:54 pm 
Beginner
Beginner

Joined: Thu Oct 25, 2007 1:21 pm
Posts: 29
Hi yugantshah,

Since this is a unidirectional mapping, i will not have child.setParent(). Please tell me whether i am missing anything here. Extremely sorry for the late reply. thanks a lot. Appreciate it.

Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 29, 2007 3:00 pm 
Beginner
Beginner

Joined: Thu Oct 25, 2007 1:21 pm
Posts: 29
Hi sethrohit1977,

Extremely sorry for the late reply. appreicate it. here is the child mapping. i really appreicate if somebody can help me fix this issue. thanks a lot.

<hibernate-mapping>
<class name="AbstractChild" lazy="true"
proxy="AbstractChild" abstract="true">
<cache usage="read-write"/>

<id name="id" access="field" type="long">
<generator class="MyGenerator"></generator>
</id>

<!-- some properties here -->

<union-subclass name="Child" table="Child">
<!-- some properties here -->
</union-subclass>
</class>
</hibernate-mapping>


thanks


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 29, 2007 3:19 pm 
Beginner
Beginner

Joined: Thu Oct 25, 2007 1:21 pm
Posts: 29
I get the following exception when i try to save parent.



org.hibernate.StaleStateException: Batch update returned unexpected row count from update [0]; actual row count: 0; expected: 1
at org.hibernate.jdbc.Expectations$BasicExpectation.checkBatched(Expectations.java:61)
at org.hibernate.jdbc.Expectations$BasicExpectation.verifyOutcome(Expectations.java:46)
at org.hibernate.jdbc.NonBatchingBatcher.addToBatch(NonBatchingBatcher.java:24)
at org.hibernate.persister.entity.AbstractEntityPersister.update(AbstractEntityPersister.java:2338)
at org.hibernate.persister.entity.AbstractEntityPersister.updateOrInsert(AbstractEntityPersister.java:2242)
at org.hibernate.persister.entity.AbstractEntityPersister.update(AbstractEntityPersister.java:2542)
at org.hibernate.action.EntityUpdateAction.execute(EntityUpdateAction.java:92)
at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:248)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:232)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:140)
at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:298)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1000)
at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:338)
at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:106)


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 13 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:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.