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?
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
>
>
> >
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---