How to provide a 'where' condition in one-to-many mapping?

993 views
Skip to first unread message

Ale

unread,
Oct 5, 2010, 4:54:20 PM10/5/10
to Fluent NHibernate
Hi there, I have a table (legacy db, cannot change schema) 'Book',
with the following three columns (well, in reality we have many more
obviously):

Id (the key),
Name
Edition

So in the database we may have two editions of a certain book named
'DDD Tutorials':

Id: 979, Name: 'DDD Tutorials', Edition: 1
Id: 980, Name: 'DDD Tutorials', Edition: 2

Now I would like the Book entity to have a mapped property,
IList<Book> named 'PreviousEditions'. This would hold a list of other
Books, specifically the ones with the same Name, but whose 'Edition'
value is lower than the edition of the current Book entity.

In the database there is no foreign key in Book referring recursively
to other editions of a book, hence, we cannot use the HasMany mapping
as-is.

But I have found the following mapping to work for the "recursive"
mapping without database defined FK:

HasMany(x =>
x.PreviousEditions).AsBag().KeyColumn("NAME").PropertyRef("Name");

This will "populate" the PreviousEditions with all Books having the
same name as the current Book entity. But as mentioned, I also need to
add the condition that the Edition should be lower/less than the
edition of the current book Entity (i.e. *Previous* editions).

I understand that one may use the Where method in the mapping, but all
examples I find have "static" where clauses, e.g.:

HasMany(blabla).Where("deleted = 0") // or the expression based
overload

But in my case I need to provide something more dynamic/parameterized,
like:

HasMany(blabla).Where("EDITION < ?") where the parameter '?' would be
the edition of the current Book entity....


Please, any help would be very very much appreciated!

Best regards/
AC

James Gregory

unread,
Oct 5, 2010, 5:29:06 PM10/5/10
to fluent-n...@googlegroups.com
As far as I know, you can't create dynamic where clauses for relationships; instead you should look at using a proper query (HQL, Criteria, or Linq).


--
You received this message because you are subscribed to the Google Groups "Fluent NHibernate" group.
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.


Ale

unread,
Oct 6, 2010, 3:43:07 AM10/6/10
to Fluent NHibernate
Ok, thanks for answering! But we would like to have this as part of
the mapping, so that I automatically get PreviousEditions initiated
with the previous editions when I get the Book entity using GetById
from the repository.

Do you mean I have to explicitly query for the previous editions in
the BookRepository's GetById method, and then assign the resulting
list to the PreviousEditions property "manually"?

In that case I would not get a true mapping, so changing a value in a
previous Edition would not get persisted when I tell the session to
save the "current" Book/edition which obviously is the aggregate root
(and which might have business methods that needs to change state on
some previous version).

Hmm, as I have managed to initiate (via the mapping HasMany(x =>
x.PreviousEditions).AsBag().KeyColumn("NAME").PropertyRef("Name")) a
List with ALL editions of the current Book (having the same name), I
could always add a property 'PreviousVersions' that filters this list
and returns only those Books that have an edition number lower (=
previous) than the current (this) book.

But it would be nice if it was possible to handle this using a
mapping, as I have no use case that is really interested in subsequent
editions (which get fetched the way the unfiltered mapping works now).

Please observe that I'm quite new to NH/Fluent, if that was not
apparent already ;-)

Regards/
AC
> > fluent-nhibern...@googlegroups.com<fluent-nhibernate%2Bunsubscr­i...@googlegroups.com>
> > .
> > For more options, visit this group at
> >http://groups.google.com/group/fluent-nhibernate?hl=en.- Hide quoted text -
>
> - Show quoted text -

Paul Batum

unread,
Oct 16, 2010, 4:32:23 AM10/16/10
to fluent-nhibernate
In the past I've been able to achieve something along these lines by inspecting the sql generated when you add the where mapping. I've been able to add where clauses that are based on the existing result set by finding out what certain parameter names or table aliases are and using those. Not sure if it will be feasible in your particular scenario though.

To unsubscribe from this group, send email to fluent-nhibern...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages