-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: one-to-one and inheritance
PostPosted: Thu Nov 05, 2009 8:43 am 
Newbie

Joined: Mon Oct 19, 2009 4:32 pm
Posts: 3
hello everyone,

I have some difficulties trying to map a bidirectional zero-or-one-to-one association when inheritance is involved.
I would need some hints and pointers on the matter because I am not able to find out a solution by myself (and I wasn't able to find similar examples or experiences in the forums.)

Here's the setup:
I have a class Cow and a class Sheep, each inherits from a class Mammal which itself inherits from Animal. The inheritance is handled through a one-table-per-subclass strategy (joined-subclass).
On the other end, I have a Farm that can only house, at most, one Mammal of each species (e.g. zero or one Cow and zero or one Sheep, but no Duck for example.)

Code:
Mammal[0..*]<-->[1..1]Farm
Cow is_a Mammal
Sheep is_a Mammal
Cow[0..1]<-->[1..1]Farm
Sheep[0..1]<-->[1..1]Farm

Only zero or one member of each mammal species can be associated with a specific farm

Tables:
Mammal:
Code:
id int(11) NOT NULL,
PRIMARY KEY (id),
CONSTRAINT xxx FOREIGN KEY (id) REFERENCES animal (id)


Cow or Sheep:
Code:
id int(11) NOT NULL,
farm_id id(11) NOT NULL UNIQUE,
PRIMARY KEY (id),
CONSTRAINT yyy FOREIGN KEY (id) REFERENCES mammal (id),
CONSTRAINT zzz FOREIGN KEY (farm_id) REFERENCES farm (id)


Farm:
Code:
id int(11) NOT NULL,
PRIMARY KEY (id)


Now here's the part where I have some real difficulties using Hibernate to map the association: I need to be able to gather all mammals (a property called getMammals() in my farm class) in a farm without having to individually fetch them by species (no properties getCow() and getSheep() in my farm class).

It works as expected if I have a seperate field for each different kind of mammals in the farm class:
Farm object:
Code:
...
    <one-to-one cascade="all" class="Sheep" name="sheep" property-ref="farm"/>
    <one-to-one cascade="all" class="Cow" name="cow" property-ref="farm"/>
...

Animal object:
Code:
...
<joined-subclass abstract="true" name="Mammal" table="mammal">
      <key column="id"/>
      <joined-subclass name="Sheep" table="sheep">
        <key column="id"/>
        <many-to-one cascade="none" class="Farm" column="farm_id" name="farm" not-null="true" unique="true"/>
      </joined-subclass>
      <joined-subclass name="Cow" table="cow">
        <key column="id"/>
        <many-to-one cascade="none" class="Farm" column="farm_id" name="farm" not-null="true" unique="true"/>
      </joined-subclass>
</joined-subclass>
...


Now what if I want to collect all mammals linked to a farm via a getMammals() in the farm class? The problem is that I can't change the database structure and I can't come up with a proper solution. Can you help me please?

TIA


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.