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.  [ 10 posts ] 
Author Message
 Post subject: Duplicate entities after initial save
PostPosted: Fri Apr 18, 2008 2:27 am 
Beginner
Beginner

Joined: Wed Jan 16, 2008 4:00 am
Posts: 41
Hello everybody!
I need your assistance in some basic hibernate concepts.

I made a n:m relation via composite element.

I now fetch element 'a', and add programmatically childentities b afterwards.

After some manipulation I update 'a'. Now 'a' gets updated and all b's are saved too.

Now I exchange all compositeelements (a.b = bCollection) and update again.

After this update i have the bCollection prior to the update and the new bCollection persisted in the database.

- so far I can imagine why hibernate does that, BUT now i update 'a' again after exchanging b (a.b = newBCollection). This time the newBCollection is persisted without old ones saved.

Any ideas?

Thx in advance!


Top
 Profile  
 
 Post subject: Duplicate entities after initial save
PostPosted: Fri Apr 18, 2008 4:38 am 
Senior
Senior

Joined: Thu Jun 21, 2007 8:03 am
Posts: 127
Location: UK
Hi marks,

Just to clarify, the code (a.b = bCollection) ... does that replace the collection property?

If so, NHibernate would not work properly if you replace its PersistentCollection objects with your own.

Regards,
Richard


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 18, 2008 4:43 am 
Beginner
Beginner

Joined: Wed Jan 16, 2008 4:00 am
Posts: 41
Hi!

Yes, a.b = bCollection replaces the collection a.b.

What i don't understand is that it works proberly after saving it the second time.

I would fully understand that it doens't work, but somehow it does work.

Unfortunately I cannot do something like:

Code:
pseudo:

foreach(b in a.b)
{ dao.deleteobject(b); }

a.b = new Collection
foreach(b in BCollection)
{ a.b.add(b);
dao.SaveObject(b);
}


b/c i can't save compositeelements


Top
 Profile  
 
 Post subject: Duplicate entities after initial save
PostPosted: Fri Apr 18, 2008 4:54 am 
Senior
Senior

Joined: Thu Jun 21, 2007 8:03 am
Posts: 127
Location: UK
Hi,

If 'a' is a persistent entity, then I don't think you can write: a.b = new Collection()

This replaces NHibernate's PersistentCollection, and all bets are off as to the behaviour after that.

I think your only options are to either:

1. Delete all the existing entries in the collection, followed by re-adding the new ones;

2. Figure out the added/deleted ones, and update the a.b collection to match your other collection.

Hope that helps.

Regards,
Richard


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 18, 2008 5:14 am 
Beginner
Beginner

Joined: Wed Jan 16, 2008 4:00 am
Posts: 41
how do i delete composite elements?
i could only :
Code:
a.b = null
dao.save(a)
a.b = newCollection
dao.save(a)


Top
 Profile  
 
 Post subject: Duplicate entities after initial save
PostPosted: Fri Apr 18, 2008 5:24 am 
Senior
Senior

Joined: Thu Jun 21, 2007 8:03 am
Posts: 127
Location: UK
I think the original code you had would work to delete, so to delete and re-add, I was imagining something like (based on your original snippet):

Code:
pseudo:

foreach(b in a.b)
{ dao.deleteobject(b); }

foreach(b in BCollection)
{ a.b.add(b);
dao.SaveObject(b);
}


or if you specify "all-delete-orphan" in your one-to-many collection, something like:

Code:
pseudo:

a.b.Clear();

foreach (b in BCollection)
{
    b.parent = a;
    a.b.add(b);
}


Regards,
Richard


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 18, 2008 5:33 am 
Beginner
Beginner

Joined: Wed Jan 16, 2008 4:00 am
Posts: 41
yes, but unfortunately I can't delete Compositeelements. Hibernate complains that there is no mapping specified


Top
 Profile  
 
 Post subject: Duplicate entities after initial save
PostPosted: Fri Apr 18, 2008 5:48 am 
Senior
Senior

Joined: Thu Jun 21, 2007 8:03 am
Posts: 127
Location: UK
Hi,

Can you post the mapping, code, and exception you're getting?

Cheers,
Richard


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 18, 2008 6:53 am 
Beginner
Beginner

Joined: Wed Jan 16, 2008 4:00 am
Posts: 41
its a usual <bag><comp-ele></comp-ele></bag> mapping.
exception tells me that there is no mapping file for b.

Is there no better / cleaner way to remove composite elements, than a.b = null; dao.save(a) ?

btw: i cant do a dao.save(b) b/c b is a composite element!


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 18, 2008 7:00 am 
Senior
Senior

Joined: Thu Jun 21, 2007 8:03 am
Posts: 127
Location: UK
Quote:
its a usual <bag><comp-ele></comp-ele></bag> mapping.


It will work in the usual way then. :-)


Quote:
Is there no better / cleaner way to remove composite elements, than a.b = null; dao.save(a) ?


Code:
a.b.Clear();

should work OK.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 10 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.