We are deep in NHibernate magic territory :) If that still doesn't work for
you, I'll see what I can do about setting up a test environment to what
you've got and giving it a go. It's an interesting problem, and I'd like to
know what the final solution will be. My knowledge in this area is purely
theoretical ;)

On Thu, Jan 21, 2010 at 4:00 PM, Corey Coogan <coreycoo...@yahoo.com> wrote:

> I'll give that a try tomorrow.
>
> Thanks,
> Corey
>
> On Jan 21, 2:45 pm, Hudson Akridge <hudson.akri...@gmail.com> wrote:
> > Ah, that might make sense actually. Because you technically don't have
> > PolicyNumber mapped as a property, it's part of your composite key. Try
> > something like this:
> > Map(x=> PolicyNumberForNH, "PolicyNumber").Access.None();
> >
> > Then change your property-ref to PolicyNumberForNH and see if NHibernate
> > gets the clue then. I believe it actually needs a <property> to map a
> > property-ref to, and not just a POCO property.
> >
> > On Thu, Jan 21, 2010 at 2:26 PM, Corey Coogan <coreycoo...@yahoo.com>
> wrote:
> > > I spoke too soon.  My claim that it worked was a case of mistaken
> > > identity on a test that was being run but recently changed.
> >
> > > Here's what this syntax is producing in an HBM.
> >
> > > - <bag cascade="save-update" name="CreditCards" mutable="true">
> > > - <key property-ref="PolicyNumber">
> > >  <column name="`PolNbr`" />
> > >  </key>
> > >  <one-to-many class="Aah.Policy.InfoLayer.CrdtCardAcctRecord,
> > > NHibernate.Spike.Data, Version=1.0.0.0, Culture=neutral,
> > > PublicKeyToken=null" />
> > >  </bag>
> >
> > > I don't think that is right?  I get an error during configuration from
> > > NH that it can't find property PolicyNumber on my Policy object
> > > (Parent class to CreditCards).  That message is very misleading and
> > > led to hours of chasing the wrong stuff.
> >
> > > On Jan 20, 11:41 am, Hudson Akridge <hudson.akri...@gmail.com> wrote:
> > > > Awesome, glad to be of assistance :)
> >
> > > > On Wed, Jan 20, 2010 at 11:40 AM, Corey Coogan <
> coreycoo...@yahoo.com
> > > >wrote:
> >
> > > > > This worked like a charm and is exactly what I needed.
> >
> > > > > Thanks again!
> >
> > > > > On Jan 20, 9:34 am, Hudson Akridge <hudson.akri...@gmail.com>
> wrote:
> > > > > > Understood. Ok, well, let's see if this works:
> > > > > > HasMany(x=>
> >
> > > x.CreditCards).AsBag().KeyColumn("PolNbr").PropertyRef("PolicyNumber");
> >
> > > > > > The PropertyRef should allow you to map to a non-key column in
> the
> > > parent
> > > > > > table. On the References() side it allows you to map to a non key
> > > > > property
> > > > > > in the related table. PropertyRef is for mapping lagacy data
> > > > > specifically.
> >
> > > > > > If that doesn't work, do a quick ExportTo() in your config and
> paste
> > > the
> > > > > > hbm.xml mappings if you could, so I can make sure FNH is writing
> the
> > > > > correct
> > > > > > XML.
> >
> > > > > > You're fortunate that you're going from Composite to a single id,
> and
> > > not
> > > > > > the other way around. NHibernate doesn't support Hibernate's
> > > <properties>
> > > > > > tag yet, which means you can only map from one column to another
> > > > > (hibernate
> > > > > > allows you to do many columns to many columns in a property-ref)
> >
> > > > > > On Tue, Jan 19, 2010 at 8:59 PM, Corey Coogan <
> coreycoo...@yahoo.com
> >
> > > > > wrote:
> > > > > > > Thanks again Hudson.  I'm totally with you on the DB stuff and
> have
> > > > > > > read quite a bit on the relationships. Unfortunately, most the
> > > tables
> > > > > > > truly need a PolicyNumber and PolicyDate as the composite key.
> > >  That's
> > > > > > > because everytime a policy is renewed or changed in any way, a
> new
> > > > > > > "portfolio" gets created which is a whole mess of tables with
> those
> > > 2
> > > > > > > columns used to identify them.  On the other hand, something
> like
> > > > > > > CreditCard, is tied to a policy regardless of it's portfolio,
> so in
> > > > > > > this case it is related to a PolicyNumber only.
> >
> > > > > > > I realized that the keys typically match up as a traditional
> > > foreign
> > > > > > > key constraint, but that's not the way these two tables are
> > > related,
> > > > > > > which is why I'm posting this question.  These are tied
> together
> > > today
> > > > > > > with a hand-rolled data access layer.  The user specifies a
> > > > > > > policynumber and an as of date and the following queries are
> > > executed
> > > > > > > on a single open connection:
> >
> > > > > > > - Get the max(policydate) <= asOfDate
> > > > > > > - Ge the Policy record for the PolicyNumber/PolicyDate
> > > > > > > - Get credit cards for the PolicyNumber
> >
> > > > > > > I'm OK with specifiying a hand-written sql statement if
> necessary.
> > > > > > > Maybe I need to handle these cases where there is no real FK in
> the
> > > > > > > database by populating with an NH query after the initial
> > > retrieval.
> > > > > > > I'm just looking for the best way to map this stuff despite the
> > > ugly
> > > > > > > nature of what I'm working with.  So I although I appreciate
> the
> > > > > > > advice regarding DB design, but this is truly out of my hands.
> >
> > > > > > > Thanks,
> > > > > > > Corey
> >
> > > > > > > On Jan 19, 3:44 pm, Hudson Akridge <hudson.akri...@gmail.com>
> > > wrote:
> > > > > > > > By the HasMany declaration. This is a one-to-many mapping in
> NH
> > > > > lingo,
> > > > > > > and
> > > > > > > > I'd recommend reading up on
> > > > > > > > that<
> >
> > >http://ayende.com/Blog/archive/2009/04/13/nhibernate-mapping-ltsetgt...
> > > > > .>.
> > > > > > > > NHibernate is smart enough to know what your ParentId column
> is
> > > on
> > > > > Policy
> > > > > > > > when going to the child table (CreditCards), it only needs to
> be
> > > told
> > > > > > > what
> > > > > > > > column matches the ParentId in the child (CreditCards) table.
> In
> > > your
> > > > > > > case,
> > > > > > > > it's PolNbr.
> >
> > > > > > > > I'd recommend against using composite keys in your mapping.
> You
> > > don't
> > > > > > > have
> > > > > > > > to mirror how the database has declared things. In that case,
> if
> > > > > > > > PolicyNumber is truly your ID, which it seems like you can do
> > > since
> > > > > > > things
> > > > > > > > that relate to your Policies only contain a PolicyId back to
> > > their
> > > > > > > parent,
> > > > > > > > then that's the only column that needs to be specified as
> your
> > > Id.
> >
> > > > > > > > Composite Keys are for when there's no other option in my
> > > opinion,
> > > > > and if
> > > > > > > > you declare composite keys, any children tables must also
> have
> > > the
> > > > > > > parents
> > > > > > > > composite key columns contained within them to make for a
> valid
> > > > > database
> > > > > > > > relationship. Since the CreditCards table appears to only
> have
> > > the
> > > > > > > > PolicyNumber (PolNbr) then you, by database semantics, should
> be
> > > able
> > > > > to
> > > > > > > > just treat a Policy as unique in the business domain by
> nothing
> > > more
> > > > > than
> > > > > > > a
> > > > > > > > PolicyNumber. If that wasn't a case, a CreditCard could
> belong to
> > > > > > > multiple
> > > > > > > > Policies, and in that case, the DBA has made an error and
> really
> > > > > needs a
> > > > > > > > join table between Policies and CreditCards.
> >
> > > > > > > > On Tue, Jan 19, 2010 at 3:18 PM, Corey Coogan <
> > > coreycoo...@yahoo.com
> >
> > > > > > > wrote:
> > > > > > > > > Wait, I just realized that I may still be confused.  Your
> > > example
> > > > > is
> > > > > > > > > assigning Id(x =>, but this is a composite key.  How will
> FNH
> > > know
> > > > > > > > > that PolicyNumber=PolNbr?
> >
> > > > > > > > > On Jan 19, 2:37 pm, Hudson Akridge <
> hudson.akri...@gmail.com>
> > > > > wrote:
> > > > > > > > > > I'm guessing that PolicyNumber/PolicyDate in Policy are
> > > mapped as
> > > > > a
> > > > > > > > > > composite key?
> >
> > > > > > > > > > Is there any reason for this other than that's what the
> > > database
> > > > > has
> > > > > > > > > mapped
> > > > > > > > > > it as? Is PolicyNumber a valid entity Identifier as far
> as
> > > the
> > > > > domain
> > > > > > > > > model
> > > > > > > > > > is concerned?
> >
> > > > > > > > > > If so, then you'd just map PolicyNumber with an
> > > > > > > > > > Id(x=> x.PolicyNumber).GeneratedBy.Assigned(); //Just a
> guess
> > > on
> > > > > the
> > > > > > > > > > assigned thing since I think your app might generate them
> >
> > > > > > > > > > And map your collection like so:
> > > > > > > > > > HasMany<CreditCard>(x => x.CreditCards).AsBag()
> > > > > > > > > >                .KeyColumn(PolNbr );
> >
> > > > > > > > > > Viola. Done. No need for a where at all there.
> >
> > > > > > > > > > Now, given that I caught a mapping. prefix, I'm assuming
> > > you're
> > > > > using
> > > > > > > an
> > > > > > > > > > automapper. I'd advise against it in your case. It's very
> > > rare to
> > > > > > > expect
> > > > > > > > > > automapper to automagically map for a legacy database
> > > > > environment.
> > > > > > > Swap
> > > > > > > > > to
> > > > > > > > > > Fluent Mappings.
> >
> > > > > > > > > > On Tue, Jan 19, 2010 at 1:06 PM, Corey Coogan <
> > > > > coreycoo...@yahoo.com
> >
> > > > > > > > > wrote:
> > > > > > > > > > > I have 2 entities that are described below.  I want to
> map
> > > many
> > > > > > > credit
> > > > > > > > > > > cards to one policy.  There is no proper FK between
> them in
> > > our
> > > > > > > Oracle
> > > > > > > > > > > DB (I can't change the DB at all).  They do match up on
> the
> > > > > > > > > > > PolicyNumber->PolNbr properties though.
> >
> > > > > > > > > > > I can't figure out how to map this relationship.
>  Here's
> > > what I
> > > > > > > have
> > > > > > > > > > > so far, but I'm not sure how to specify the Key in the
> > > > > containing
> > > > > > > > > > > entity that doesn't match.
> >
> > > > > > > > > > > mapping.HasMany<CreditCard>(x => x.CreditCards)
> > > > > > > > > > >                .KeyColumn(PolNbr )
> > > > > > > > > > > //where to map CreditCard.PolNbr to Policy.PolicyNumber
> >
> > > > > > > > > > > Policy
> > > > > > > > > > > {
> > > > > > > > > > > PolicyNumber : string (PK)
> > > > > > > > > > > PolicyDate : date (PK)
> > > > > > > > > > > CreditCards : IList<CreditCard>
> > > > > > > > > > > }
> >
> > > > > > > > > > > CreditCard
> > > > > > > > > > > {
> > > > > > > > > > > PolNbr : String (PK)
> > > > > > > > > > > CcType : String (PK)
> > > > > > > > > > > ExpDt : String (PK)
> > > > > > > > > > > //3 more columns that are the PK
> > > > > > > > > > > }
> >
> > > > > > > > > > > --
> > > > > > > > > > > You received this message because you are subscribed to
> the
> > > > > Google
> > > > > > > > > Groups
> > > > > > > > > > > "Fluent NHibernate" group.
> > > > > > > > > > > To post to this group, send email to
> > > > > > > > > fluent-nhibern...@googlegroups.com.
> > > > > > > > > > > To unsubscribe from this group, send email to
> > > > > > > > > > > fluent-nhibernate+unsubscr...@googlegroups.com<fluent-nhibernate%2bunsubscr...@googlegroups.com>
> <fluent-nhibernate%2bunsubscr...@googlegroups.com<fluent-nhibernate%252bunsubscr...@googlegroups.com>
> >
> > > <fluent-nhibernate%2bunsubscr...@googlegroups.com<fluent-nhibernate%252bunsubscr...@googlegroups.com>
> <fluent-nhibernate%252bunsubscr...@googlegroups.com<fluent-nhibernate%25252bunsubscr...@googlegroups.com>
> >
> >
> > > > > <fluent-nhibernate%2bunsubscr...@googlegroups.com<fluent-nhibernate%252bunsubscr...@googlegroups.com>
> <fluent-nhibernate%252bunsubscr...@googlegroups.com<fluent-nhibernate%25252bunsubscr...@googlegroups.com>
> >
> >
> > ...
> >
> > read more ยป
>
> --
> You received this message because you are subscribed to the Google Groups
> "Fluent NHibernate" group.
> To post to this group, send email to fluent-nhibern...@googlegroups.com.
> To unsubscribe from this group, send email to
> fluent-nhibernate+unsubscr...@googlegroups.com<fluent-nhibernate%2bunsubscr...@googlegroups.com>
> .
> For more options, visit this group at
> http://groups.google.com/group/fluent-nhibernate?hl=en.
>
>
>
>


-- 
- Hudson
http://www.bestguesstheory.com
http://twitter.com/HudsonAkridge

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

Reply via email to