problem mapping an association as a list

17 views
Skip to first unread message

Tim Robinson

unread,
Dec 19, 2012, 8:20:55 AM12/19/12
to fluent-n...@googlegroups.com
Hi,

In my data model I have a simple situation with document which has many versions. I want the versions to be retrieved in order (most recent first) so I have mapped the association like this

            HasMany(x => x.Versions)
                .AsList()
                .OrderBy("UpdateDate DESC")
                .KeyColumn("DocumentId")
                .Inverse()
                .Cascade.AllDeleteOrphan();

The problem is that when nhibernate generates the SQL, it tries to retrieve a nonexistent column called 'index' from the database. If I take out the AsList() specifier it seems to work OK and returns them in the correct order but I'm not sure whether this is just luck. can anyone explain what AsList is actually supposed to do?

I've read about indexed and ordered collections in the jboss hibernate documentation but it's not really clear how it all translates into fnh

--- Tim

Carl Bussema

unread,
Dec 19, 2012, 9:07:29 AM12/19/12
to fluent-n...@googlegroups.com
A List is a specifically, sequentially-ordered set of items, which has an index column in the database that says what the order is. This is useful when you have items that have an arbitrary (e.g., user-defined) order.

In your scenario, it sounds like you have a Bag (or more likely, a Set, because the same "version" could not appear in the same "document" more than once, but sets aren't particularly easy to order). NHibernate can map a Bag onto IList or ICollection, and since you want to have the items in a specific order, it sounds like you need the IList.

.AsList() tells NH to treat the association as a LIST, .AsSet() tells it to treat it as a SET, and .AsBag() [which is the default] says to treat it as a Bag.


--- Tim

--
You received this message because you are subscribed to the Google Groups "Fluent NHibernate" group.
To view this discussion on the web visit https://groups.google.com/d/msg/fluent-nhibernate/-/fpPtoQCMjBsJ.
To post to this group, send email to fluent-n...@googlegroups.com.
To unsubscribe from this group, send email to fluent-nhibern...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/fluent-nhibernate?hl=en.

Tim Robinson

unread,
Dec 19, 2012, 9:23:26 AM12/19/12
to fluent-n...@googlegroups.com
Thanks Carl, that makes a lot of sense.

So just to confirm, if I use an IList to store the versions and specify OrderBy() in my mapping, then they will come out in the required order, even if the mapping specifies AsBag()?

--- Tim

Oskar Berggren

unread,
Dec 19, 2012, 9:04:24 AM12/19/12
to fluent-n...@googlegroups.com
A list is a data structure where element are associated with a
numerical index in the list which governs the ordering of elements.
NHibernate will persist this ordering using an index-column. A list in
NHibernate will not be rearranged just be being persisted to the DB
and then read back.

Perhaps you can use either a SortedList as per
http://nhforge.org/doc/nh/en/index.html#collections-sorted with the
UpdateDate as index column.

/Oskar


2012/12/19 Tim Robinson <tim.j.r...@gmail.com>:
Reply all
Reply to author
Forward
0 new messages