views:

24

answers:

1

Hi

This question is really to establish if what im trying to achieve is possible.

I have three tables i'll omit the actual names to keep it simple and less confusing

so we have

Person [ PID - uniqueidentifier PK ]

Applicant [ PID - uniqueidentifier PK, AID - varchar(20)]

Student [ PID - uniqueidentifier PK, SID - int]

both the student and applicant tables are related to the person table with a one-to-one relationship (where the primary key is held in the person table), but are not related to each other.

I have the following domain objects

public class Person
{
    public virtual Guid PID{get;set;}
}

public class Applicant: Person
{
    public virtual string AID {get;set;}
}

public class Student:Person
{
    public virtual int SID {get;set;}
}

These each have a map class derived from either ClassMap or SubClassMap

public class PersonMap: ClassMap<Person>
{
    public PersonMap()
    {
        Id(x => x.PID).GeneratedBy.Assigned();
        Table("Person");
    }
}

public class ApplicantMap: SubclassMap<Applicant>
{
    public ApplicantMap()
    {
        Table("Applicant");
        KeyColumn("PID");
        Map(x => x.AID);
    }
}

public class StudentMap:SubclassMap<Student>
{
    public StudentMap()
    {
        Table("Student");
        KeyColumn("PID");
        Map(x => x.SID);
    }
}

My question is, is it possible to have a person who is an applicant and a student at the same time.In database terms I can have a row in each table that holds the same PID, will nhibernate allow me to save and retrieve all three objects?

one of the problems I have is trying to insert into student when the id already exists for an applicant and of course person..

public void MakePersonAStudent(Person p)
{
    Student newStudent = new Student();
    newStudent.PID = p.PID;
    newStudent.SID = getNewStudentID();
    Session.Save(newPerson);
}    

The exception generated is:

a different object with the same identifier value was already associated with the session .... 73e5fd90-c27a-49d8-87dc-cd6413c120a2, of entity: Student

A: 

use single_table inheritance. doc for hibernate: http://docs.jboss.org/hibernate/core/3.3/reference/en/html/inheritance.html

foret
Thanks for the reply, i'm not sure that is what i'm after.(If I understand correctly) Single table requires a discriminator column so NHB can work out which class to instantiate. If I want the same row to be instantiated to two different sub types i.e the same person record can be instantiated as a Student and an Applicant. Would I need two rows in the Person table each with a discriminator representing an Applicant and a Student instance? Thanks
if i'm not mistaken, they must have differences in fields, thats all. I.E: Applicant has AID field and Student has SID field. however i don't really know how it can tell the difference without not null constraint (cuz if everything is null, you just can't understand if class considers that field or not.). So maybe additional fields must be not_nullable. But these are my thoughts, you'd better google it yourself for more reliable info. PS: you cannot convert Applicant to Student because there are not in the same inheritance branch - there are parallel.
foret
ok, after a bit of reading im looking at section 9.1.2. "Table per subclass" and thinking this is what I want to achieve.would it be possible to have this implementation and have a Student also exist with the same ID in the Applicant table?Thanks again for your help

related questions