Thanks for the answer.
I understand now. But I have a follow up question. This is the code
for my relationship table, which I've done following some examples and
previous discussions about this subject:
class UserDevice extends LongKeyedMapper[UserDevice] with IdPK {
def getSingleton = UserDevice
object userId extends MappedLongForeignKey(this, User)
object deviceId extends MappedLongForeignKey(this, Device)
}
object UserDevice extends UserDevice with LongKeyedMetaMapper
[UserDevice]
The question is:
Shouldn't be both fields (userID and deviceID) be the primary key of
this table? Why am I using IdPK? Couldn't I just remove it?
Thanks,
GA
On Oct 26, 2009, at 7:49 PM, Jim Barrows wrote:
> On Mon, Oct 26, 2009 at 11:43 AM, GA <[email protected]> wrote:
> I have made a test that worked. I have modified my code like this:
>
> newUser.save
> newUser.devices += newDevice
> newUser.save
>
> The newDevice was already saved. It looks like both parents must be
> saved before I save the relationship. Am I right? or I am doing
> something wrong?
>
> No, you always have to save the parents before the relationship.
> Well.. as long as the relationship table has the parents PK as it's
> PK's anyway.
> If you remove that restriction, then nah.. it doesn't matter. Of
> course that way lies madness....
>
>
> Thanks,
>
> GA
>
>
>
>
> On Oct 26, 2009, at 6:55 PM, GA wrote:
>
>> Hello guys,
>>
>> I have a many-to-many relationship between two mappers called,
>> Users and Devices.
>>
>> There is also an API that receives and XML message that contains
>> one user and one device.
>>
>> The API could create the Device and User with the relationship or
>> it could create only the relationship in case the users and/or
>> device already exists.
>>
>> The problem I have is that the API creates the User and the Device,
>> but the not the relationship. This is the code that saves the
>> records (just a test for a now):
>>
>> def addUser(req: Req): LiftResponse = {
>>
>> var tempUserName = ""
>> var tempDeviceName = ""
>> var deviceAlreadyExists = false
>>
>> val newUser = new User
>> val newDevice = new Device
>> req.xml match {
>> case Full(<person>{parameters @_*}</person>) => {
>> for(parameter <- parameters){ parameter match {
>> case <userName>{userName}</userName> =>
>> tempUserName = userName.text
>> case <firstName>{firstName}</firstName>
>> => newUser.firstName(firstName.text)
>> case <lastName>{lastName}</lastName> =>
>> newUser.lastName(lastName.text)
>> case <password>{password}</password> =>
>> newUser.password(password.text)
>> case <email>{email}</email> =>
>> newUser.email(email.text)
>> case <createdon>{createdOn}</createdon>
>> => newUser.createdOn(new java.util.Date(createdOn.text))
>> case <updatedon>{updatedOn}</updatedon>
>> => newUser.updatedOn(new java.util.Date(updatedOn.text))
>> case <device>{deviceName}</device> =>
>> tempDeviceName = deviceName.text
>> case _ =>
>> }
>> }
>> try {
>> Device.find(By
>> (Device.deviceName,tempDeviceName)) match {
>> case Full(deviceRequested) =>
>> deviceAlreadyExists = true
>> case _ => {
>> newDevice.deviceName
>> (tempDeviceName)
>> newDevice.createdBy(tempUserName)
>> newDevice.createdOn
>> (newUser.createdOn)
>> newDevice.updatedBy(tempUserName)
>> newDevice.updatedOn
>> (newUser.updatedOn)
>> newDevice.save
>> }
>> }
>> User.find(By(User.userName, tempUserName))
>> match {
>> case Full(userRequested) =>
>> CreatedResponse(wrapXmlBody
>> (<operation id="addPerson" success="1"></operation>), "text/xml")
>> case _ => {
>> newUser.userName(tempUserName)
>> newUser.createdBy(tempUserName)
>> newUser.updatedBy(tempUserName)
>> newUser.devices.clear
>> newUser.validated(true)
>> newUser.devices += newDevice
>> newUser.save
>> CreatedResponse(wrapXmlBody
>> (<operation id="addPerson" success="0"></operation>), "text/xml")
>> }
>> }
>> }
>> catch {
>> case e => Log.error("Could not add person/
>> device", e); BadResponse()
>> }
>> }
>> case _ => Log.error("Request was malformed "+req.view);
>> BadResponse()
>> }
>> }
>>
>> The field "devices" is the MappedManyToMany field within the User
>> mapper.
>>
>> What am I doing wrong?
>>
>> Thanks in advance,
>>
>> GA
>>
>>
>>
>
>
>
>
>
>
>
> --
> James A Barrows
>
>
> >
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Lift" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/liftweb?hl=en
-~----------~----~----~----~------~----~------~--~---