This is a migrated thread and some comments may be shown as answers.

[Solved] How do I: Write in [Persisten] Entity1 - [Persistent] Entity2 without any links (foreign key)

3 Answers 86 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Andrey
Top achievements
Rank 1
Andrey asked on 07 Sep 2010, 10:10 AM
I have 2 [Persistent] classes: Person and Test:
01.[Persistent]
02.class Person
03.{
04.  string Name {get; set;}
05.  float Weight {get; set;}
06.}
07.  
08.[Persistent]
09.class Test
10.{
11.  string Name {get; set;}
12.  Person PersonData {get; set;}
13.}

Each Person makes a Load Test (in bicycle).

Person is saved in Test. But I need to save it without any links. So if in table [Person] current Person will be changed (Name or Weight) - in table [Test] field PersonData should stay in initial state (with not changed Name and Weight).

Should I use BLOB for PersonData?

Or something else?

3 Answers, 1 is accepted

Sort by
0
IT-Als
Top achievements
Rank 1
answered on 07 Sep 2010, 12:18 PM
Hi Andrey,

I am not sure what you're are trying to solve with your model of Person and Test classes (please elaborate on the use case), so forgive me if the answer is out of scope.

You state that one Person can take one (at most)  Test. Therefore, I think it will be more natural to make and instance of the Person class have a reference to an instance of the Test class. The association between them would be "testTaken" and be a 1:1.
This way Test is navigable from Person via the TestTaken property. However, Person is not navigable from Test since it has no field/property that exposes that reference.

Again, I don't know what you're trying to achieve (business goal)..

Still, if you want to make the Test a real composite of a Person you can define it as a struct (that is the Test class). Doing so, will only create one table (the Person table) that will embed information (in columns) on the Test struct composite.

Let me know if you have more question..if so please elaborate on the business goal...

Regards

Henrik



0
Andrey
Top achievements
Rank 1
answered on 07 Sep 2010, 01:48 PM

Hi Henrik,

Human can do many tests. As usual - more than one. Because It can be used in hospitals, sports organisations, football clubs and so on. Person do the first Load-Test. After it he get some results about his physical state. Also he can take an Exercise Schedule with some plans of trainings. After some trainings he do one test again - and get new results.

I have [Persons] in my DB

And I have [Tests] too.

Person knows about his Tests. And Test knows Person (Who did it)

And when Test is saved - I should save Person as is. With his now Weight, Height and other more and more information.

And This Test should store Info about Person on the Test doing moment.

By these parameters (Weight, Height, Age and so on) Test calculates many parameters, Print many Reports with this Info.

So, If These data will be changed (Weight, Height, Age and so on) Test will be calculate another (invalid) parameters.

As you see I need both [Persistent] classes - [PERSON] and [TEST]

But in [TEST] I need UNlinked [Person]

0
IT-Als
Top achievements
Rank 1
answered on 07 Sep 2010, 03:27 PM
Hi Andrey,

Thanks for elaborating. I would do it this way:

3 classes: Person, PersonInfo (or whatever you prefer for a name) and Test

Person: Will hold name and so on
PersonInfo:  Will hold the weigth, heigth, age, etc (this class is referenced from both Person and Test)
Test: Will hold information about the test (date taken, etc..)

-
One Person instance will reference one PersoInfo instance in a 1:1 association (this reference describes the current personal info of the person)
-
One Person instance will reference a list of Test instances in a 1:m association. (navigable in both directions, thus you also have a reference from Test to Person to fulfil this.)
-
One Test instance will reference one PersonInfo instance in a 1:1 association (this reference describes the personal info at the time of taking the test). Note that this is a complete new instance of PersonInfo that lives and dies with the Test instance owning it.

So for the business logic:

To create a Person:
You create a Person instance and PersonInfo instance to capture the current information about a person. Make Person reference one PersonInfo instance.

To create a Test:
You create a Test instance and a PersonInfo instance to capture the information about the personal info (weight, etc.) at the time of taking the test. Initially you can fill the new PersonInfo instance (to be assoicated with the Test instance) with the current PersonInfo instance (referenced from the Person class). Make the Test instance reference the new PersonInfo (copy) instance and add the Test instance to the list of Test instances in the Person class.

Doing so you reuse the PersonInfo class for the current information (since it is in a 1:1 to the Person class) and for the test related information (since a new instance of PersonInfo is in a 1:1 to the Test class)

Update:
You could also make the PersonInfo a struct. Doing so, you will end up with only 2 tables in the database (Person and Test) each having the fields of the struct (age, weigth, etc.) embedded within them. Doing so will make it a composite, also on database level.

Regards

Henrik
Tags
General Discussions
Asked by
Andrey
Top achievements
Rank 1
Answers by
IT-Als
Top achievements
Rank 1
Andrey
Top achievements
Rank 1
Share this question
or