Hola, que tal!
Estoy tratando de mapear una relacion many to many en Glorp.
Tengo la clase usuario y la clase grupo, un usuario es dueño de muchos
grupos y un grupo pertenece a un solo usuario; a su vez, tengo otra
relacion en la que un grupo contiene muchos usuario y un usuario pertenece
a muchos grupos.
Cuando intento mapear la relacion one to many, anda todo bien. El problema
viene cuando intento mapear la relacion muchos a muchos. En diversos
tutoriales de glorp explica como mapear sin especificar la tabla de
asociacion, pero buscando encontre que es necesario especificarla debido a
que tengo multiples relaciones con las mismas tablas
El codigo me genera correctamente la base de datos, y puedo crear grupos y
asignarle un dueño, el problema viene cuando quiero agregar un usuario a un
grupo determinado, no arroja ningun error, solo un "Se encontro fin de
archivo inesperado en la conexion del cliente" en el log de PGAdmin.
Como se resolveria esto? Esta bien lo que estoy haciendo?
El mapeo que realice es el siguiente:
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'
--
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
http://www.clubSmalltalk.org