Mapping based on Join? Read only entities?

16 views
Skip to first unread message

Brett Veenstra

unread,
May 20, 2009, 5:03:15 PM5/20/09
to Fluent NHibernate
I'm trying to produce a read-only view of several of my entities.

For example, a "Part" sits in a "Part" table (Read/Write), but a
"ProducedPart" is a Part that has rows in a "Production" table (Read-
Only). I want to be able to generate a distinct list of
"ProducedPart" based on the production data.

What I think I need is a way to apply a filter during the Mapping
phase that takes other Entities into account. How would I map this?

Is this a proper problem for NHibernate to solve?

Thanks.

Hudson Akridge

unread,
May 20, 2009, 5:14:45 PM5/20/09
to fluent-n...@googlegroups.com
It's possible that you might find an answer a bit faster on the nhibernate-users group, but we're always happy to try and help out. I'm not sure I understand completely what you're trying to do. So let me attempt to regurgitate it back and make sure we're both on the same page:

You want to be able to have a model class (Part) which pulls it's information from two different table/views, one that's read-only and another that is read-write?

Rei

unread,
May 20, 2009, 6:46:19 PM5/20/09
to Fluent NHibernate
Could you please explain in more detail what it is that you want to
do?

Brett Veenstra

unread,
May 21, 2009, 8:54:07 AM5/21/09
to Fluent NHibernate
Sorry for the confusion.

I have a simple model class (Part), which pulls from it's information
from a single table (t_Part).

I would like a subclass of this model called (ProducedPart), that
would still utilize NHibernate's caching mechanisms, but would only be
instances of (Part) that have a foreign key relationship in a table
called "PartProduction".

I only need a read-only version of this model.

I could always implement a Facade/Repository over this, but I was
hoping to setup a mapping (via FNH) that would pull "t_Part" joined
with "PartProduction".

Thanks for fielding my question!


On May 20, 5:14 pm, Hudson Akridge <hudson.akri...@gmail.com> wrote:
> It's possible that you might find an answer a bit faster on the
> nhibernate-users group, but we're always happy to try and help out. I'm not
> sure I understand completely what you're trying to do. So let me attempt to
> regurgitate it back and make sure we're both on the same page:
> You want to be able to have a model class (Part) which pulls it's
> information from two different table/views, one that's read-only and another
> that is read-write?
>

Brett Veenstra

unread,
May 21, 2009, 9:08:05 AM5/21/09
to Fluent NHibernate
The SQL that I would expect for this Model would be:


SELECT p.*
FROM t_Part p
INNER JOIN t_PartProduction pp ON pp.PartID = p.PartID
WHERE pp.ProductionYear = '?'


Considering Hudson's earlier comment that I should post this to the
NHUsers group, I apologize. Since I didn't want to just blatantly
cross-post, I setup a StackOverflow question for this:
http://stackoverflow.com/questions/892820/nhibernate-incorrect-thinking-subclassed-model-based-on-join

Since I'm a n00b to NH and it's been made more approachable through
FNH, I posted here first, sorry guys.

Thanks for any ideas!

Stuart Childs

unread,
May 21, 2009, 9:21:38 AM5/21/09
to fluent-n...@googlegroups.com
Well, I had been typing out a response here but then you had to go and post a SO question and I'm a rep whore :P

Hope it helps, and like Hudson said, we're always happy to help even when the questions are much more NH related.  We just want to make sure you get the best help and the NH forum is probably populated with all sorts of mapping experts. :)

Brett Veenstra

unread,
May 21, 2009, 10:09:09 AM5/21/09
to Fluent NHibernate
I think I have it working now...



public Part()
{
WithTable("t_Part");

Id(i => i.Id).ColumnName("PartID");
Map(m => m.Name).ColumnName("Part");

SetAttribute("where", "PartID IN ( SELECT pp.PartID FROM
PartProduction pp ) ");

ReadOnly();
}

I couldn't find how to use FNH to specify the "where" element on the
HBM "class", so I just used the SetAttribute.

Thanks folks!




On May 21, 9:21 am, Stuart Childs <chil...@gmail.com> wrote:
> Well, I had been typing out a response here but then you had to go and post
> a SO question and I'm a rep whore :P
> Hope it helps, and like Hudson said, we're always happy to help even when
> the questions are much more NH related.  We just want to make sure you get
> the best help and the NH forum is probably populated with all sorts of
> mapping experts. :)
>
> On Thu, May 21, 2009 at 8:08 AM, Brett Veenstra <brettveens...@gmail.com>wrote:
>
>
>
> > The SQL that I would expect for this Model would be:
>
> > SELECT p.*
> > FROM t_Part p
> > INNER JOIN t_PartProduction pp ON pp.PartID = p.PartID
> > WHERE pp.ProductionYear = '?'
>
> > Considering Hudson's earlier comment that I should post this to the
> > NHUsers group, I apologize.  Since I didn't want to just blatantly
> > cross-post, I setup a StackOverflow question for this:
>
> >http://stackoverflow.com/questions/892820/nhibernate-incorrect-thinki...

Hudson Akridge

unread,
May 21, 2009, 10:20:27 AM5/21/09
to fluent-n...@googlegroups.com
That works :) 

You may also look at mapping those two parts polymorphicly, i.e. map an IPart to an I_Parts table, then map a Part as a join subclass to IPart, with it's table of Parts, then map the ReadOnlyPart to a join subclass with IPart with it's table of ProductionParts.

That would of course mean that the I_Parts table contains an index of both, but the actual fields that represent the part would exist in two separate tables which could then have various read-only and read-write attributes placed upon them. 

Brett Veenstra

unread,
May 22, 2009, 6:43:04 AM5/22/09
to Fluent NHibernate
Even though I can modify the schema in this case, it's really a
virtual model that I'm after.

I want to have something that I can use inside NH with it's caching
and querying capabilities, compose easily from the existing data, and
something that is immutable.

Perhaps I should have created a VIEW in the schema and tried to map to
that, it would be essentially the same.

But, is that a recommended approach for these scenarios?

Thanks.


On May 21, 10:20 am, Hudson Akridge <hudson.akri...@gmail.com> wrote:
> That works :)
> You may also look at mapping those two parts polymorphicly, i.e. map an
> IPart to an I_Parts table, then map a Part as a join subclass to IPart, with
> it's table of Parts, then map the ReadOnlyPart to a join subclass with IPart
> with it's table of ProductionParts.
> That would of course mean that the I_Parts table contains an index of both,
> but the actual fields that represent the part would exist in two separate
> tables which could then have various read-only and read-write attributes
> placed upon them.
>

Hudson Akridge

unread,
May 22, 2009, 8:40:56 AM5/22/09
to fluent-n...@googlegroups.com
I was actually going to suggest a view earlier. As to whether or not that's a recommended approach? No clue :) I've never mapped to a view, nor heard of anyone who has. But Sql semantics shouldn't care one way or another. I believe that would prevent inserts and updates, at least for a reasonably complex view.
Reply all
Reply to author
Forward
0 new messages