-->
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.  [ 9 posts ] 
Author Message
 Post subject: Generic collections
PostPosted: Sat Oct 01, 2005 8:27 am 
Contributor
Contributor

Joined: Sat Sep 24, 2005 11:25 am
Posts: 198
Just wanted to announce that I've a working solution for generic collections in .Net 2 for NHibernate.

Lots of details are here:
http://www.ayende.com/projects/nhibernate-query-analyzer/generics.aspx

But the gist of it is that I created a custom property accessor and wrapper collections.[/url]


Top
 Profile  
 
 Post subject:
PostPosted: Sat Oct 01, 2005 9:09 am 
Contributor
Contributor

Joined: Wed May 11, 2005 4:59 pm
Posts: 1766
Location: Prague, Czech Republic
Great!

One note about the "edge case" you describe on that page - if you use bags, NHibernate does not load the bag if you only want to add an item. But it's not really safe to do it for sets as you did. In addition to the problem you described, this will also allow the user to add a duplicate item into the set. So it might actually be better to convert EntitySet into EntityBag. But that's just my opinion.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Oct 01, 2005 11:04 am 
Contributor
Contributor

Joined: Sat Sep 24, 2005 11:25 am
Posts: 198
Part of the assumtions fo the EntitySet & EntityRef is that EntityRef is the one that is making this connection.

Because of this I think that there is no possibility of having a duplicate item in the sets.

Even if you try to add an item to a non loaded set twice, all that would happen is that you would set the relevant property twice, you wouldn't get two rows in the DB, or even two objects which refer to the same thing.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Oct 01, 2005 11:34 am 
Contributor
Contributor

Joined: Wed May 11, 2005 4:59 pm
Posts: 1766
Location: Prague, Czech Republic
Ok, what will happen if the non-loaded set contains an entity of class Child with id 1, and you create a new entity with the same id of 1 and try to add it to the set? I know that these are all corner cases, but given enough users somebody will encounter it :)


Top
 Profile  
 
 Post subject:
PostPosted: Sat Oct 01, 2005 1:06 pm 
Contributor
Contributor

Joined: Sat Sep 24, 2005 11:25 am
Posts: 198
My current entities are all generator native, so it can't happen to me. Which is probably why I didn't consider it.

What happens when you try to do it for a /loaded/ set?

Doesn't it violate the way you should work in the first place?


Top
 Profile  
 
 Post subject:
PostPosted: Sat Oct 01, 2005 1:34 pm 
Contributor
Contributor

Joined: Wed May 11, 2005 4:59 pm
Posts: 1766
Location: Prague, Czech Republic
If you add an item twice into a loaded set, Add should return false the second time and not add anything. This is the way it works in Java, and Iesi Set should act the same.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Oct 01, 2005 1:46 pm 
Contributor
Contributor

Joined: Sat Sep 24, 2005 11:25 am
Posts: 198
It doesn't return false (because it implements ICollection<T>, which has Void Add(T item); )
But it doesn't add it to the collection. Nor would it do it when you save to DB


Top
 Profile  
 
 Post subject:
PostPosted: Sat Oct 01, 2005 2:49 pm 
Contributor
Contributor

Joined: Wed May 11, 2005 4:59 pm
Posts: 1766
Location: Prague, Czech Republic
Ah ok I didn't know that Add returns void in .NET2. That will surely make life easier for NHibernate in the future, as it will be possible to avoid loading the whole collection on Add.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Oct 01, 2005 3:36 pm 
Contributor
Contributor

Joined: Sat Sep 24, 2005 11:25 am
Posts: 198
I think that the assumstion in .Net is that an error in add is an exception, not an error code.
And remove either remove or not, but the end result is that the object is no longer in the collection.

BTW, what about many-to-many relationships?
Right now my model doesn't require me to use them, but I can certainly see a case when I'll want to. Then you have to load one of the collection, right?


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