Hello.
I am trying to map a many-to-many relationship in glorp.
I have a class named 'User' and a class named 'Group'. These class have two relationships, the first one is one-to-may and expresses the 'owner' feature, and the other is many-to-many and expresses which user belong to which group, in other words, an user belongs to many groups and a groups contains many users.
My problem is when I try to map the many-to-many relationship, reading some tutorials I figure out that I need to specify the association table, but I tried many ways to do that without success...
The code generate the database successfully, also the one-to-many relationship, but when I try to add a member to a group, does not throw any exception, only a log error in Posgres Admin "Se encontro fin de archivo inesperado en la conexion del cliente", something like it found an unexpected end of file was found in the client connection.
Sorry for the bad english.
My code is the following.
classModelForGroup: aClassModel
aClassModel newAttributeNamed: #id.
aClassModel newAttributeNamed: #name
classModelForUser: aClassModel
aClassModel newAttributeNamed: #id.
aClassModel newAttributeNamed: #username.
aClassModel newAttributeNamed: #password.
aClassModel newAttributeNamed: #tweets collectionOf: TwittAR.Tweet.
aClassModel newAttributeNamed: #groups collectionOf: TwittAR.Group.
aClassModel newAttributeNamed: #groupsIbelong collectionOf: TwittAR.Group
descriptorForGroup: aDescriptor
| table linkTable |
self tableNamed: 'GROUP_HAS_USER'.
linkTable := self tableNamed: 'GROUP_HAS_USER'.
table := self tableNamed: 'GROUPS'.
aDescriptor table: table.
(aDescriptor newMapping: Glorp.DirectMapping) from: #id to: (table fieldNamed: 'id').
(aDescriptor newMapping: Glorp.DirectMapping) from: #name to: (table fieldNamed: 'name').
(aDescriptor newMapping: Glorp.ManyToManyMapping)
attributeName: #members;
referenceClass: TwittAR.User;
mappingCriteria: (Glorp.Join
from: (table fieldNamed: 'id')
to: (linkTable fieldNamed: 'group_id')).
(aDescriptor newMapping: Glorp.ManyToManyMapping)
attributeName: #tweets;
referenceClass: TwittAR.Tweet
descriptorForUser: aDescriptor
| table linkTable |
table := self tableNamed: 'USERS'.
linkTable:= self tableNamed: 'GROUP_HAS_USER'.
aDescriptor table: table.
(aDescriptor newMapping: Glorp.DirectMapping) from: #id to: (table fieldNamed: 'id').
(aDescriptor newMapping: Glorp.DirectMapping) from: #username to: (table fieldNamed: 'username').
(aDescriptor newMapping: Glorp.DirectMapping) from: #password to: (table fieldNamed: 'password').
(aDescriptor newMapping: Glorp.OneToManyMapping) attributeName: #tweets.
(aDescriptor newMapping: Glorp.OneToManyMapping) attributeName: #groups.
(aDescriptor newMapping: Glorp.ManyToManyMapping)
attributeName: #groupsIbelong;
referenceClass: TwittAR.Group;
mappingCriteria: (Glorp.Join
from: (table fieldNamed: 'id')
to: (linkTable fieldNamed: 'user_id')).
tableForGROUPS: aTable
| owner |
(aTable createFieldNamed: 'id' type: platform sequence) bePrimaryKey.
aTable createFieldNamed: 'name' type: platform text.
owner := aTable createFieldNamed: 'user_id' type: platform int4.
aTable addForeignKeyFrom: owner to: ((self tableNamed: 'USERS') fieldNamed: 'id').
tableForUSERS: aTable
(aTable createFieldNamed: 'id' type: platform sequence) bePrimaryKey.
aTable createFieldNamed: 'username' type: platform text.
aTable createFieldNamed: 'password' type: platform text
tableForGROUP_HAS_USER: aTable
| groupKey userKey |
"(aTable createFieldNamed: 'id' type: platform sequence) bePrimaryKey."
userKey := aTable createFieldNamed: 'user_id' type: platform int4.
aTable addForeignKeyFrom: userKey to: ((self tableNamed: 'USERS') fieldNamed: 'id').
groupKey := aTable createFieldNamed: 'group_id' type: platform int4.
aTable addForeignKeyFrom: groupKey to: ((self tableNamed: 'GROUPS') fieldNamed: 'id'
Thank you very much!