My suggestion is to flatten the hierarchy a little OR switch to a single
strategy (table per subclass)... deep hierarchies with changing strategies
are a pain to map and maintain.
Diego
On Fri, Aug 20, 2010 at 16:56, v64 <[email protected]> wrote:
> Thanks for your reply. The mapping file i originally posted had a bug
> in it where EndangeredAnimal was under Animal when it should have been
> under WildAnimal. Here's the fixed file:
>
> <?xml version="1.0" encoding="utf-8" ?>
> <hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
> assembly="Animals">
>
> <class name="Animals.Animal" abstract="true" table="Animal">
> <id type="int" unsaved-value="0">
> <generator class="native"/>
> </id>
> <discriminator column ="AnimalType" type="string" length="64"/>
> <property name="SpeciesName"/>
>
> <subclass name="Animals.DomesticAnimal" discriminator-
> value="DomesticAnimal">
> <property name="Name"/>
> </subclass>
>
> <subclass name="Animals.WildAnimal" discriminator-
> value="WildAnimal">
> <property name="Habitat"/>
> <property name="IsPredator"/>
>
> <subclass name="Animals.EndangeredAnimal" discriminator-
> value="EndangeredAnimal">
> <join table="EndangeredAnimal">
> <key column="AnimalID"/>
> <property name="RedListID"/>
> <property name="Population"/>
> <property name="Status"/>
> </join>
>
> <subclass name="Animals.IberianLinx" discriminator-
> value="IberianLinx">
> <join table="EndangeredAnimal">
> <key column="AnimalID"/>
> <property name="WhiskerLength"/>
> <property name="StripeCount"/>
> </join>
> </subclass>
> </subclass>
>
> </subclass>
>
> </class>
> </hibernate-mapping>
>
> I have tried using the same <join> for IberianLynx as for
> EndangeredAnimal and schema generation actually worked fine. However
> when actually trying to save an instance of IberianLynx, NH tried to
> insert two separate records into EndangeredAnimal, one for
> IberianLynx, one for its base EndangeredAnimal instance. This of
> course is incorrect and resulted in a primary key violation:
>
> INSERT INTO Animal (SpeciesName, Habitat, IsPredator, AnimalType)
> VALUES (@p0, @p1, @p2, 'IberianLinx'); select SCOPE_IDENTITY();@p0 =
> 'Iberian Linx', @p1 = 'Iberian Peninsula', @p2 = True
> INSERT INTO EndangeredAnimal (RedListID, Population, Status, AnimalID)
> VALUES (@p0, @p1, @p2, @p3);@p0 = 'DTW6282331', @p1 = 57, @p2 =
> 'Critically Endangered', @p3 = 1
> INSERT INTO EndangeredAnimal (WhiskerLength, StripeCount, AnimalID)
> VALUES (@p0, @p1, @p2);@p0 = 10, @p1 = 100, @p2 = 1
>
> Is there another way to map IberianLynx's properties to
> EndangeredAnimal and not to Animal?
>
> Thanks!
> v64
>
>
> On Aug 20, 3:33 pm, Diego Mijelshon <[email protected]> wrote:
> > From NH's mapping point of view, it doesn't matter that IberianLynx
> > extends EndangeredAnimal,
> > as it's seen as an Animal subclass.
> > Also, you can have many <join>s, so NH wouldn't know where to put it.
> > You can probably specify the same join in IberianLynx, although schema
> > generation might fail.
> >
> > Diego
> >
> >
> >
> > On Fri, Aug 20, 2010 at 15:23, v64 <[email protected]> wrote:
> > > I'm trying to map a deep class hierarchy and having trouble getting
> > > the desired mapping. Here's an example:
> >
> > > Classes:
> >
> > > namespace Animals
> > > {
> > > public class Animal
> > > {
> > > public string SpeciesName { get; set; }
> > > }
> >
> > > public class DomesticAnimal : Animal
> > > {
> > > public string Name { get; set; }
> > > }
> >
> > > public class WildAnimal : Animal
> > > {
> > > public string Habitat { get; set; }
> > > public bool IsPredator { get; set; }
> > > }
> >
> > > public class EndangeredAnimal : WildAnimal
> > > {
> > > public string RedListID { get; set; }
> > > public int Population { get; set; }
> > > public string Status { get; set; }
> > > }
> >
> > > public class IberianLinx : EndangeredAnimal
> > > {
> > > public int WhiskerLength { get; set; }
> > > public int StripeCount { get; set; }
> > > }
> > > }
> >
> > > Mapping:
> >
> > > <?xml version="1.0" encoding="utf-8" ?>
> > > <hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
> > > assembly="Animals">
> >
> > > <class name="Animals.Animal" abstract="true" table="Animal">
> > > <id type="int" unsaved-value="0">
> > > <generator class="native"/>
> > > </id>
> > > <discriminator column ="AnimalType" type="string" length="64"/>
> > > <property name="SpeciesName"/>
> >
> > > <subclass name="Animals.DomesticAnimal" discriminator-
> > > value="DomesticAnimal">
> > > <property name="Name"/>
> > > </subclass>
> >
> > > <subclass name="Animals.WildAnimal" discriminator-
> > > value="WildAnimal">
> > > <property name="Habitat"/>
> > > <property name="IsPredator"/>
> > > </subclass>
> >
> > > <subclass name="Animals.EndangeredAnimal" discriminator-
> > > value="EndangeredAnimal">
> > > <join table="EndangeredAnimal">
> > > <key column="AnimalID"/>
> > > <property name="RedListID"/>
> > > <property name="Population"/>
> > > <property name="Status"/>
> > > </join>
> >
> > > <subclass name="Animals.IberianLinx" discriminator-
> > > value="IberianLinx">
> > > <property name="WhiskerLength"/>
> > > <property name="StripeCount"/>
> > > </subclass>
> > > </subclass>
> > > </class>
> >
> > > </hibernate-mapping>
> >
> > > When I generate the schema I get these:
> >
> > > create table Animal
> > > (
> > > id INT IDENTITY NOT NULL,
> > > AnimalType NVARCHAR(64) not null,
> >
> > > SpeciesName NVARCHAR(255) null,
> > > Name NVARCHAR(255) null,
> > > Habitat NVARCHAR(255) null,
> > > IsPredator BIT null,
> > > WhiskerLength INT null,
> > > StripeCount INT null,
> >
> > > primary key (id)
> > > )
> >
> > > create table EndangeredAnimal (
> > > AnimalID INT not null,
> >
> > > RedListID NVARCHAR(255) null,
> > > Population INT null,
> > > Status NVARCHAR(255) null,
> >
> > > primary key (AnimalID)
> > > )
> >
> > > My problem is that I don't want IberianLynx's properties to be in
> > > Animal, I need them to be in its immediate ancestor's table, i.e.
> > > EndangeredAnimal. Why did they end up in Animal?
> >
> > > TIA
> > > v64
> >
> > > --
> > > You received this message because you are subscribed to the Google
> Groups
> > > "nhusers" group.
> > > To post to this group, send email to [email protected].
> > > To unsubscribe from this group, send email to
> > > [email protected]<nhusers%[email protected]>
> <nhusers%[email protected]<nhusers%[email protected]>
> >
> > > .
> > > For more options, visit this group at
> > >http://groups.google.com/group/nhusers?hl=en.- Hide quoted text -
> >
> > - Show quoted text -
>
> --
> You received this message because you are subscribed to the Google Groups
> "nhusers" group.
> To post to this group, send email to [email protected].
> To unsubscribe from this group, send email to
> [email protected]<nhusers%[email protected]>
> .
> For more options, visit this group at
> http://groups.google.com/group/nhusers?hl=en.
>
>
--
You received this message because you are subscribed to the Google Groups
"nhusers" 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/nhusers?hl=en.