views:

236

answers:

2

I am working on a legacy system and have introduced fluent nHibernate but have one last mapping that I can not seem to get working.

Code:

public class Permit
    {
    public int PermitId { get; set; }
    public char Discipline { get; set; }
    public PermitDetails PermitDetails { get; set; }
    }

public PermitDetails
    {
    public int PermitId { get; set; }
    }

public class GasPermitDetails : PermitDetails
    {
       ... permit details
    }

public class ElectricalPermitDetails : PermitDetails
    {
       ... permit details
    }

Schema:

*tblPermit*
PermitId, int
Discipline, char
.... some other columns

*tblGas*
PermitId, int
....gasDetails

*tblElectrical*
PermitId, int
....electrical details

(if tblPermit.Discipline is "G" we need to get the data out of tblGas. if tblPermit.Discipline is "E" we need to get the data out of tblElectrical). I have messing around trying to get this figured out but have not had any luck so far.

A: 

It looks to me that the schema came out right. You may need to be more specific about the problem you have because I take it that the PermitId for all the PermitDetails subclasses becomes foreign keys to the corresponding relation tables.

Eg. for the discipline "gas", the permit in question should have gas information for the permit. It has the relationship if the PermitID in tblGas is a foreign key to tblPermit's id. Like this:

+---------------+         +----------------------+
|               | 1     1 |                      |
|   tblPermit   |<------->| tblGas               |
|               |         | (for gas discipline) |
|               |         |                      |
+---------------+         +----------------------+
|               |         |                      |
| {PK} PermitId |         | {FK} PermitID        | <- ForeignKey to tblPermit.PermitID
|               |         |                      |
+---------------+         +----------------------+

You won't need the Discipline column to select the correct data. You only need one query that looks up tblElectrical, tblGas on the permit's identifier. That way, you'll get your permit along with it's information that way.

(PS. Excuse me for the use of UML class diagram to visualise the database tables, but crows feet notation does not translate well in ASCII)

Spoike
Your diagram is correct. I just don't know how to write the mapping to tell nHibernate which type of data to load when it hydrates the data back.
Kudos2u2
I don't know really how to do that myself (hibernate has gone off my radar recently). You could always check out http://www.hibernate.org/hib_docs/nhibernate/html/inheritance.html and http://stackoverflow.com/questions/296834/fluent-nhibernate-how-to-map-a-subclass-one-to-one if it gives any ideas.
Spoike
A: 

(if tblPermit.Discipline is "G" we need to get the data out of tblGas. if tblPermit.Discipline is "E" we need to get the data out of tblElectrical). I have messing around trying to get this figured out but have not had any luck so far.

The discriminator field is irrelevant, as the PK fields of the subtypes are FK's to their supertype's table anyway. So this is a table-per-subtype mapping.

Frans Bouma

related questions