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.  [ 3 posts ] 
Author Message
 Post subject: Inheritance mapping
PostPosted: Tue Aug 28, 2007 7:18 am 
Newbie

Joined: Mon Jun 18, 2007 4:07 am
Posts: 3
Hi. I have a simple class hierarchy with 3 classes which are the following:

Code:
public class Device
{
        private int id;
        private string macAddress;
}

public class Logger : Device
{
        private int reportinterval;

}

public class Sensor : Device
{
        private int sampleRate;
        private int measuringTime;
}


The Device class is just an "interface", it is just the parent class of the other two classes to consist the common fields. I will use it in a gridview for example but when I select a row I wanna get the Logger or Sensor object, so actually there won't be any Device object in my application.

I checked the NHibertnate's documentation and I chose the one-table-per-hierarchy implementation but my tutor said it is not good because it may not work and I couldn't convince him it will. So I implemented a following relational database:

Code:
CREATE TABLE `device` (
  `Id` int(11) NOT NULL auto_increment,
  `MacAddress` varchar(4) NOT NULL,
  PRIMARY KEY  (`Id`),
  UNIQUE KEY `U_Device` (`MacAddress`)
) ENGINE=InnoDB;

CREATE TABLE `logger` (
  `Id` int(11) NOT NULL auto_increment,
  `ReportInterval` int(11) default NULL,
  `DeviceId` int(11) default NULL,
  PRIMARY KEY  (`Id`),
  KEY `FK_Logger_Device` (`DeviceId`)
) ENGINE=InnoDB;

CREATE TABLE `sensor` (
  `Id` int(11) NOT NULL auto_increment,
  `SampleRate` int(11) default NULL,
  `MeasuringTime` int(11) default NULL,
  `DeviceId` int(11) default NULL,
  PRIMARY KEY  (`Id`),
  KEY `FK_Sensor_Device` (`DeviceId`)
) ENGINE=InnoDB;

ALTER TABLE `logger`
  ADD FOREIGN KEY (`DeviceId`) REFERENCES `device` (`Id`) ON DELETE CASCADE ON UPDATE CASCADE;

ALTER TABLE `sensor`
  ADD FOREIGN KEY (`DeviceId`) REFERENCES `device` (`Id`) ON DELETE CASCADE ON UPDATE CASCADE;


My question is how to map this database into my object model, bacause I couldn't find any example yet. Could anyone help me? Thanx a lot.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 28, 2007 7:39 am 
Regular
Regular

Joined: Thu Nov 23, 2006 10:29 am
Posts: 106
Location: Belgium
Hello,

This looks like a table-per-subclass model.
You'd have 3 classes: Device, Logger and Sensor. Logger and Sensor inherit from Device.
In your mapping file, you'd create a mapping file for Device and in that mapping file, use 'joined-subclass' tags to map the Logger and Sensor classes (the 'key column' tag should be set to DeviceId).

I think this should work.

_________________
Please rate this post if it helped.

X.


Top
 Profile  
 
 Post subject: Re: Inheritance mapping
PostPosted: Tue Aug 28, 2007 10:34 am 
Expert
Expert

Joined: Fri May 13, 2005 11:13 am
Posts: 292
Location: Rochester, NY
divums wrote:
I checked the NHibertnate's documentation and I chose the one-table-per-hierarchy implementation but my tutor said it is not good because it may not work and I couldn't convince him it will.


Is there some information we're missing here? Because if you are in the position of being able to define your database, table-per-hierarchy is usually the best option, and is certainly so for this hierarchy. The extra joins will almost certainly be more expensive than the discriminator column.

A couple of other points: you do not need an independent ID column in you subclass tables, DeviceID will suffice because there is an effective one-to-one relation with the ID on the device table. Also, if the Device class won't be used by itself, define it as an interface or abstract class--the inheritance mapping will still function.


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

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.