Any better ideas?

16 views
Skip to first unread message

Peter Forstmeier

unread,
Feb 3, 2012, 9:20:26 AM2/3/12
to Fluent NHibernate
Hi List,
i have the following mappings:

public class CustomerHomeAddress : BaseAddress
{
Classes derived from BaseAddress are all empty. The Property's
are all in BaseAddress
}


public class BaseAdressMapping : ClassMap<BaseAddress>
{
public BaseAdressMapping()
{
Table("InheritedAddresses");
Id(x =>
x.Id).GeneratedBy.GuidComb().UnsavedValue("00000000-0000-0000-0000-000000000000");

DiscriminateSubClassesOnColumn("ClassType").AlwaysSelectWithValue()
.Not.Nullable()
.Length(DefaultFieldDescription.DefaultStringColumnLength);

More Prop's......

}
}

public class
CustomerHomeAddressMapping :SubclassMap<CustomerHomeAddress>
{
public CustomerHomeAddressMapping()
{
var s = string.Format("{0}_{1}", "Customer",
"HomeAddress");
DiscriminatorValue(s);
}
}

The same SubClass mappings are for CustomerBillingAddress,
EmployeeHomeAddress, ProjectHomeAddres etc.

My 'Main-Classes' like Customer,Projectes,Employees,Supplier's look
like this:

public class CustomerMapping : BaseClassMap<Domains.Customer>
{
public CustomerMapping()
{
Id(x =>
x.Id).GeneratedBy.GuidComb().UnsavedValue("00000000-0000-0000-0000-000000000000");
........
HasMany<BaseAddress>(x => x.Addresses)
.KeyColumn("Foreignkey")
.NotFound.Ignore()
.Cascade.AllDeleteOrphan()
.Not.LazyLoad();

}
}
The HasMany part is the same all over my 'Main-Classes'

Is anybody here how is willing to correct my mapping or has any better
idea?

Thanks in advance
Peter

Joe Brockhaus

unread,
Feb 3, 2012, 9:49:03 AM2/3/12
to fluent-n...@googlegroups.com
perhaps it would be good to know what's wrong?

does the BaseAddress table have a column of name "Foreignkey" ?

does it have a References<Customer>(x=>x.Customer).Column("Foreignkey")

do you have other entities that derive from Customer?


------
Joe Brockhaus
joe.br...@gmail.com
------------


Peter

--
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.


Joe Brockhaus

unread,
Feb 3, 2012, 9:49:52 AM2/3/12
to fluent-n...@googlegroups.com
does it (BaseAddressMap) have a References<C...

------
Joe Brockhaus
joe.br...@gmail.com
------------


Peter Forstmeier

unread,
Feb 4, 2012, 4:29:02 AM2/4/12
to Fluent NHibernate
Hi,
there is nothing wrong. The mapping is working so far but i'm not
shure if i have some hidden bugs.
I just would like to know if this mapping can be done better.

<does the BaseAddress table have a column of name "Foreignkey" ?
Shure.

<does it have a
References<Customer>(x=>x.Customer).Column("Foreignkey")
No, at the time, Mapping form Customer(and others) are unidirectional
and BaseAddress is the Many side
and has no Reference back to Customer(and others).

So i appreciate any suggestions

Thanks
Peter


On 3 Feb., 15:49, Joe Brockhaus <fel0ni...@gmail.com> wrote:
> *  does *it* (BaseAddressMap) have a References<C...
>
> ------
> Joe Brockhaus
> joe.brockh...@gmail.com
> ------------
>
>
>
> On Fri, Feb 3, 2012 at 9:49 AM, Joe Brockhaus <fel0ni...@gmail.com> wrote:
> > does it have a References- Zitierten Text ausblenden -
>
> - Zitierten Text anzeigen -

Joe Brockhaus

unread,
Feb 4, 2012, 9:04:58 AM2/4/12
to Peter Forstmeier, Fluent NHibernate
Are addresses supposed to be reused across customers?

I suspect by this mapping (or what I can see of it) that you only have
as many addresses as you have types ... Or that you want all addresses
of a given type to be in a list for a customer by having the customer
store a type. Neither of which really makes sense to me.

Does customer have a property for address type? (aka the discriminator)

Or does customer have a list of addressIds in a linking table?

Sent from my Windows Phone
From: Peter Forstmeier
Sent: 2/4/2012 4:29 AM
To: Fluent NHibernate
Subject: [fluent-nhib] Re: Any better ideas?

Thanks
Peter

--

Peter Forstmeier

unread,
Feb 6, 2012, 2:19:48 AM2/6/12
to Fluent NHibernate
Hi Joe,

> Are addresses supposed to be reused across customers?

No, each customer has it's own address's

> I suspect by this mapping (or what I can see of it) that you only have
> as many addresses as you have types ...

Yes, if i need a new Addresstype i derive a new one from BaseAddress

>Or that you want all addresses
> of a given type to be in a list for a customer by having the customer
> store a type. Neither of which really makes sense to me.

All my Entity's dealing with addresses are mapped like this:

HasMany<BaseAddress>(x => x.Addresses)
.KeyColumn("Foreignkey")
.NotFound.Ignore()
.Cascade.AllDeleteOrphan()
.Not.LazyLoad();
>
> Does customer have a property for address type? (aka the discriminator)

See above, but i would be interested in using a Discrimonator within
each of my Enity's

>
> Or does customer have a list of addressIds in a linking table?

See above, no linking table, the Addresses are linked only by
ForeignKey

Peter

Joe Brockhaus

unread,
Feb 6, 2012, 10:21:37 AM2/6/12
to fluent-n...@googlegroups.com
>> Are addresses supposed to be reused across customers?
>No, each customer has it's own address's

I was really trying to get at understanding why you left the association unidirectional in your domain, when the database is seemingly bidirectional.
-- Column 'Foreignkey' in the 'InheritedAddresses' table is really 'CustomerID'  

However .. that might not be true, simply based on your mention of other address types like Project, Employee, etc.

Which leads me to believe that all-tolled, you have at least 3 tables referencing addresses, all by the same 'Foreignkey' column.
-- Customer
-- Project
-- Employee

So .. the issue becomes uniqueness of those Foreignkey values across all of those other tables.
-- And the FK value translates to the PK of the parent entity
i.e. 
- this is bad: 
-- Customer { ID = 1 }
-- Project { ID = 1 }
-- Employee {ID = 1 }

- this is want you want
-- Customer { ID = 1 }
-- Project { ID = 2 }
-- Employee {ID = 3 }

So, as long as you are using Guids for IDs in all your entities that have many addresses, you should be ok.

------
Joe Brockhaus
joe.br...@gmail.com
------------


Peter Forstmeier

unread,
Feb 8, 2012, 2:14:59 AM2/8/12
to Fluent NHibernate
Hi,
sorry for the late answer.
Yes, this is my intention:

> Which leads me to believe that all-tolled, you have at least 3 tables
> referencing addresses, all by the same 'Foreignkey' column.
> -- Customer
> -- Project
> -- Employee


I'm using Guid's all over my tables:

> So, as long as you are using Guids for IDs in all your entities that have
> many addresses, you should be ok.

Thanks for the help
Peter
Reply all
Reply to author
Forward
0 new messages