-->
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.  [ 18 posts ]  Go to page 1, 2  Next
Author Message
 Post subject: preserve Parent Object
PostPosted: Tue Mar 07, 2006 1:50 pm 
Newbie

Joined: Tue Mar 07, 2006 1:01 pm
Posts: 8
Hi,

maybe this is a stupid question but
is there a possibility to preserve an parent object with joined-subclass.

The Problem is as follows:
A class "patient" is inherited from a class "person" (joined-subclass). If the class "patient" is deleted the values of the class "person" are also deleted This is OK from OO but i would like to preserved the values of the parent object in database.

Is there an buildin feature (i know i could care myself about this)? - found nothing in the Documentation/Forum.

thx


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 07, 2006 4:55 pm 
Contributor
Contributor

Joined: Wed May 11, 2005 4:59 pm
Posts: 1766
Location: Prague, Czech Republic
No, there is no feature like this. Why would you want to preserve the person data? This would mean that if somebody searches for the deleted patient, NH will think it actually exists (because theperson record is present) and this will lead to errors. If you want to archive the person data in another table on deletion, you can do so using a database trigger.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 07, 2006 6:02 pm 
Newbie

Joined: Tue Mar 07, 2006 1:01 pm
Posts: 8
Thanks for your quick reply!

I need to perserve the informations for further usage.

For Eg. Think about an centralised Person-Management System where informations can be administrated. Further there are specialized Person-Management Modules for Eg. extended informations about Patients, Doctors, Hospitalmanagment, Secretaries, ... . All these Modules can be used within the Base-System. If an eg. Patient has been deleted, only the specialized Informations should be deleted. The base Informations (e.g firstname, lastname, birthday, adresse,..) should be further available in the Base-System and if this person-informatoins are used within an other specialized Person-Module the Informations belong to the base person must not be reentered. This Informations are only deleted if a base person would be deleted (and is not used within the specialized modules).


Top
 Profile  
 
 Post subject: Re: preserve Parent Object
PostPosted: Wed Mar 08, 2006 3:54 am 
Expert
Expert

Joined: Thu Jan 19, 2006 4:29 pm
Posts: 348
marso wrote:
The Problem is as follows:
A class "patient" is inherited from a class "person" (joined-subclass).


You got a wrong design ;)

What about if a person is a doctor and a patient at the same time?

The common solution is to have a person class which can play several roles: Patient, Doctor and so on.

One-to-Many relationship from person to PersonRole.

So, person would be deleted when last rokle of person is deleted.

Gert


Top
 Profile  
 
 Post subject: Re: preserve Parent Object
PostPosted: Wed Mar 08, 2006 5:51 am 
Newbie

Joined: Tue Mar 07, 2006 1:01 pm
Posts: 8
gert wrote:
You got a wrong design ;)

What about if a person is a doctor and a patient at the same time?

This is the point!
gert wrote:
The common solution is to have a person class which can play several roles: Patient, Doctor and so on.

One-to-Many relationship from person to PersonRole.


Maybe a One-to-One relationship would be possible (each extension would be saved in a own extension table only once) but from OO its not the best solution because the values of the person-object are not directly accessible (eg. patient.person.firstname vs. patient.firstname).

gert wrote:

So, person would be deleted when last rokle of person is deleted.

Gert


I solved the problem with a ONE-2-ONE relationship. The class person is inherited by Patient but also has an private Object Person. The nH mapping is a one-2-one relationship (cascade-option).

This solution has both advantages - OO all values are directly accesible within code, - nH manages the data-storage!

Maybe it's not the best solution but it works fine!

Thx for the Hints!


Top
 Profile  
 
 Post subject: Re: preserve Parent Object
PostPosted: Wed Mar 08, 2006 6:38 am 
Expert
Expert

Joined: Thu Jan 19, 2006 4:29 pm
Posts: 348
marso wrote:
I solved the problem with a ONE-2-ONE relationship. The class person is inherited by Patient but also has an private Object Person. The nH mapping is a one-2-one relationship (cascade-option).


I have logical collapse here:
If a Patient (instance named "John Doe", id =1 ) is a person, and
a Doctor (instance named "John Doe", id = 2) is also a person then
Patient "John Doe" is not the same person as doctor "John Doe" (As they have different id-s)

I would still go with Person -> PersonRole pattern. Patient would be inherited from PersonRole. PersonRole could implement calculated proeprties to access similar properties of associated person.

Gert


Top
 Profile  
 
 Post subject: Re: preserve Parent Object
PostPosted: Wed Mar 08, 2006 6:44 am 
Expert
Expert

Joined: Thu Jan 19, 2006 4:29 pm
Posts: 348
marso wrote:
I solved the problem with a ONE-2-ONE relationship. The class person is inherited by Patient but also has an private Object Person. The nH mapping is a one-2-one relationship (cascade-option).


Another example:

Person name="John Doe", id = 1;
Doctor (inherits from person), Associated with person {Id =1}, name="John Doe", id = 2;
Patient (inherits from person), Associated with person {Id =1}, name="John Doe", id = 3;

You change the name of Person(id=3) to "John Doe Junior". Now You have to update also person where id=2 and Id=1, or You have inconcistent data in database. Usually having such duplicated data in database (same name three times) is not a good idea.

Gert


Top
 Profile  
 
 Post subject: Re: preserve Parent Object
PostPosted: Wed Mar 08, 2006 7:56 am 
Newbie

Joined: Tue Mar 07, 2006 1:01 pm
Posts: 8
Sorry for confusing you.

A Person could for eg. be a doctor and a patient - bad example would almost never be the case but a doctor could be also a member of the managment board and ist the same person, with the same basic informations. Same personID ist referenced to 2 different extension tables. (Only on Row in table person. one row in extension-table "doctor" referenced by ID, one row in extesion-table "management" referenced by ID).

This means there is only one row per person (not three).The extra informations implemented by the inherited classes stored in extra tables by referencing the PersonID.

My befor mentioned solution would handle this except i could not save the correct object because if the patient is casted to an person and i would call session.save(o) - the object will be known as an patient by nH.

I now will try your mentioned solution with one2many - but I beliefe i will run into the same problems!

Thx


Top
 Profile  
 
 Post subject: Re: preserve Parent Object
PostPosted: Wed Mar 08, 2006 8:23 am 
Expert
Expert

Joined: Thu Jan 19, 2006 4:29 pm
Posts: 348
marso wrote:
A Person could for eg. be a doctor and a patient - bad example would almost never be the case but a doctor could be also a member of the managment board and ist the same person, with the same basic informations. Same personID ist referenced to 2 different extension tables. (Only on Row in table person. one row in extension-table "doctor" referenced by ID, one row in extesion-table "management" referenced by ID).


In this case, You have a Doctor with Id = 1 and a Patient with id =1. Now, if You ask NH to load a Person with id=1, should it return a Doctor or a Patient? Anyway, I fail to figure out how to cerate such situation trought NH.

Maybe I've got it all wrong. Could You post a mapping file for Your solution?


Gert


Top
 Profile  
 
 Post subject: Re: preserve Parent Object
PostPosted: Wed Mar 08, 2006 9:21 am 
Newbie

Joined: Tue Mar 07, 2006 1:01 pm
Posts: 8
If I load a Person it only should return a Person (doesn't matter what else extensions are stored in DB).
If I load a doctor-object it should load a doktor-object with all informations (Personinformations and extended doctor Informations - inherited Class) - As i understood the Documentation this is a classical situation for usage of subclass or joined-subclass.

The Problem with the joined-subclass-solution is that - if i delete the child object (eg. doktor) the informations from the basic object are also deleted - found no possibility to prevent this. And so the basic informations are lost. No way to use the basic informations with an basic object.

The easyest way I think is, to use the joined-subclass and implement my own delete method - witch only deletes the extendet informations (from the extended table).

thx
Marso

btw: I'm using mapping.attribute.dll - no classical mapping-file


Top
 Profile  
 
 Post subject: Re: preserve Parent Object
PostPosted: Wed Mar 08, 2006 9:50 am 
Expert
Expert

Joined: Thu Jan 19, 2006 4:29 pm
Posts: 348
marso wrote:
If I load a Person it only should return a Person (doesn't matter what else extensions are stored in DB).
If I load a doctor-object it should load a doktor-object with all informations (Personinformations and extended doctor Informations - inherited Class) - As i understood the Documentation this is a classical situation for usage of subclass or joined-subclass.


AFAIK, an ORM MUST return object of correct type, no matter if base class is asked or not. So, in case of

public class Person (PK is PersonId)
public class Doctor: Person
public class Patient: Person

and a Patient with PersonId = 1 is stored in database,

Person p = session.Get(typeof(Person), 1)

then

(p is Patient) == true

Gert


Top
 Profile  
 
 Post subject: Re: preserve Parent Object
PostPosted: Wed Mar 08, 2006 10:07 am 
Newbie

Joined: Tue Mar 07, 2006 1:01 pm
Posts: 8
gert wrote:
AFAIK, an ORM MUST return object of correct type, no matter if base class is asked or not. So, in case of

public class Person (PK is PersonId)
public class Doctor: Person
public class Patient: Person

and a Patient with PersonId = 1 is stored in database,

Person p = session.Get(typeof(Person), 1)

then

(p is Patient) == true

Gert


This is true but i can't see the relation to my post?

I will try to explain by extending your sample:

public class Person (PK is PersonId)
public class Doctor: Person
public class Patient: Person

Doctor d = new Doctor();
d.firstname = "test";
session.save(d);

//person is stored with id = 1
//doctor is stored with fk = 1

session.delete(d);

person p = session.load(typeof(Person),1) --> Null (because the whole doctor object have been deleted!)

And there is my problem! - I need the basic informations for further usage!

marso


Top
 Profile  
 
 Post subject: Re: preserve Parent Object
PostPosted: Wed Mar 08, 2006 11:26 am 
Expert
Expert

Joined: Thu Jan 19, 2006 4:29 pm
Posts: 348
marso wrote:
session.delete(d);

person p = session.load(typeof(Person),1) --> Null (because the whole doctor object have been deleted!)

And there is my problem! - I need the basic informations for further usage!

You are stepping back to original question. I was talking about Your proposed solution :)

My stataments about You solution was:
1) It will duplicate person related data in database;
2) It will create a situation where Doctor "John Doe" is different person than a Patient "John Doe" (even if they have same Person Identification Number/Social Securtiy Number)

You haven't provide any glues how You intend to avoid those issues...

Gert


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 08, 2006 3:04 pm 
Contributor
Contributor

Joined: Sun Jun 26, 2005 5:03 am
Posts: 51
Location: London, UK
I'd agree with Gert here, you have mis-analysed the problem. The solution is as he says to have a Person/PersonRole scenario where a Person can take on multiple roles.

You'll get enough duplicate records in a medical system anyway without designing some more in ;-)

_________________
Paul Hatcher
NHibernate Team


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 09, 2006 5:57 am 
Newbie

Joined: Tue Mar 07, 2006 1:01 pm
Posts: 8
Thanks for your efforts to help solving my Problem

Out now there are 3 solutions:

1.) Use the Person/PersonRole - mentioned by Gert - Lot of extra work to use the informations from the base object (because each inherited object would need the informations form the base object - if not it would not be inherited)
2.) Use the solution I mentioned above - Lot of extra work for saving, load and delete - ( because it only worked if there is an helper-class which cares about the correct correlation - to avoid the problems gert mentioned (dupplicated rows - if standard features are used)).
3.) Not using nH - Do it the classic Way (everybody know how much extra work this would be).

At the moment i'm are trying to implement it by the first solution. For Cases there this solution doesn't fit i would use Solution 3 - if i have more time i will again try solution 2 in an more generic way, but at the moment 1,3 have to be sufficient.

Again Thanks for the Help


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 18 posts ]  Go to page 1, 2  Next

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.