Hello,
>From what I've seen so far, looks like what I am trying to do can't be
done, but hopefully I'm wrong and someone can explain it to me.
First of all, I am using GLORP with VA Smalltalk 8.6.1 and a Microsoft SQL
Server 2005 database.
My intention is to map objects of a specific class to one table, let's say
instances of Person to the table PEOPLE, embedding the names (instances of
Name) for each Person on the same table.
*Model classes and its methods:*
*Object subclass: #PersoninstanceVariableNames: 'name age
id'classVariableNames: ''poolDictionaries: ''“Person Class Methods”first:
firstNameString last: lastNameString ^self new setFirst:
firstNameString last: lastNameString; yourself“Person
Instance Methods”setFirst: firstNameString last: lastNameString name :=
Name first: firstNameString last: lastNameString Object subclass:
#NameinstanceVariableNames: 'first last 'classVariableNames:
''poolDictionaries: ''“Name Class Methods”first: firstNameString last:
lastNameString ^self new setFirst: firstNameString last:
lastNameString; yourself“Name Instance Methods”setFirst:
firstNameString last: lastNameString first := firstNameString. last
:= lastNameString*
*The descriptor system methods:*
*DescriptorSystem subclass: #GlorpTutorialDescriptorinstanceVariableNames:
''classVariableNames: ''poolDictionaries: '' allTableNames ^#( 'PEOPLE'
) constructAllClasses ^(super constructAllClasses) add:
Person; add: Name; yourself classModelForName: aClassModel
aClassModel newAttributeNamed: #first. aClassModel newAttributeNamed:
#last. classModelForPerson: aClassModel aClassModel newAttributeNamed:
#id. aClassModel newAttributeNamed: #name type: Name. aClassModel
newAttributeNamed: #age. descriptorForName: aDescriptor | personTable
| personTable := self tableNamed: 'PEOPLE'. aDescriptor table:
personTable. (aDescriptor newMapping: DirectMapping) from: #first to:
(personTable fieldNamed: 'first_name'). (aDescriptor newMapping:
DirectMapping) from: #last to: (personTable fieldNamed:
'last_name'). descriptorForPerson: aDescriptor | personTable |
personTable := self tableNamed: 'PEOPLE'. aDescriptor table:
personTable. (aDescriptor newMapping: DirectMapping) from: #age to:
(personTable fieldNamed: 'age'). (aDescriptor newMapping: DirectMapping)
from: #id to: (personTable fieldNamed: 'id'). (aDescriptor newMapping:
EmbeddedValueOneToOneMapping) attributeName: #name. tableForPEOPLE:
aTable (aTable createFieldNamed: 'id' type: platform sequence)
bePrimaryKey. (aTable createFieldNamed: 'first_name' type: (platform
varChar: 50)). (aTable createFieldNamed: 'last_name' type: (platform
varChar: 50)). (aTable createFieldNamed: 'age' type: (platform int))
defaultValue: 39.*
So far no problem, in fact that is one of the examples on the GLORP
tutorial included in the VA Smalltalk documentation.
But now suppose I want to map objects of a another class which reuses the
concept of Name, let's say Animals, to their own table ANIMALS, and again
embedding the names for each Animal on the table ANIMALS.
Can that be done? Because it implies having two descriptors for the class
Name (one for the table PEOPLE and another for the table ANIMAL) but the
descriptor system stores descriptors in a Dictionary where the keys are the
classes.
I tried using my own objects (types) instead of classes keys for that
Dictionary, so that there are no collisions, and I got to a point where
reading from the database worked, but writing didn't, and it got too
complex from there, because on most places in the code, Glorp does "self
descriptorFor: anObject class".
So I thought I should stop and ask, because it doesn't seem like something
too complex and I may be missing something.
Thanks,
Mariano
|