Loading...

fluent-nhibernate@googlegroups.com

[Prev] Thread [Next]  |  [Prev] Date [Next]

Re: [fluent-nhib] Re: How to provide a 'where' condition in one-to-many mapping? Paul Batum Sat Oct 16 02:00:19 2010

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.

On Wed, Oct 6, 2010 at 6:43 PM, Ale <[EMAIL PROTECTED]> wrote:

> 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
>
>
>
>
> On Oct 5, 11:29 pm, James Gregory <[EMAIL PROTECTED]> wrote:
> > 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).
> >
> >
> >
> > On Tue, Oct 5, 2010 at 9:54 PM, Ale <[EMAIL PROTECTED]> wrote:
> > > 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
> >
> > > --
> > > You received this message because you are subscribed to the Google
> Groups
> > > "Fluent NHibernate" group.
> > > To post to this group, send email to
> [EMAIL PROTECTED]
> > > To unsubscribe from this group, send email to
> > > [EMAIL PROTECTED]<[EMAIL PROTECTED]>
> <[EMAIL PROTECTED]>
> > > .
> > > For more options, visit this group at
> > >http://groups.google.com/group/fluent-nhibernate?hl=en.- Hide quoted
> text -
> >
> > - Show quoted text -
>
> --
> You received this message because you are subscribed to the Google Groups
> "Fluent NHibernate" group.
> To post to this group, send email to [EMAIL PROTECTED]
> To unsubscribe from this group, send email to
> [EMAIL PROTECTED]<[EMAIL PROTECTED]>
> .
> For more options, visit this group at
> http://groups.google.com/group/fluent-nhibernate?hl=en.
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"Fluent NHibernate" group.
To post to this group, send email to [EMAIL PROTECTED]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/fluent-nhibernate?hl=en.