0

I have started working with grails recently and it is awesome. In my application I am using morphia plugin to communicate with mongodb. Let me give you an overview of the system and then get into the problem I am facing

Overview I have three domain classes which are as follows

A. Project.groovy
B. Customer.groovy
C. User.groovy

Project.groovy has the following

@Reference Customer customer
String projectName
@Id ObjectId projectId

Customer.groovy has the following

@Reference List<Project> projects
String customerName
@Id ObjectId customerId

User.groovy has the following

@Reference List<Project> userResponsibleProjects
String userName
@Id String userEmail

some sample data so that I can explain the problem much more clear.

customers -> customerA and customerB
projects -> projectA, ProjectB and projectC
projectA and projectB has reference to customerA and vice versa
projectC has reference to customerC and vice versa
Users -> userA and userB
userA has reference to projectA and projectB.
userB has reference to projectC

now some information about the problem and how the problem came into existence. Users of out system have an option to delete projects which they do not use any more. so when delete project is triggered I remove the reference to that project and save the customer and if the customer save was successful I delete the project. Since I am new I did not check for the project references in the User.groovy. So say we delete projectB, I did

customer.remove(projectB)
if(customer.save())
    projectB.remove()

now the initial problem that I was facing was

"grails exception could not get project reference for user.userResponsibleProjects". So to solve this I went into the database and did the following

db.User.update({_id: userA},{$pull: {$ref: "Project", $id: ObjectId("projectB")}})

and that worked. Now the real problem.

If the user tries to go into any of the customer the system throws the same exception i,e, could not get reference for user.userResponsibleProjects and on further investigation I found the following, I get selected customer from mongo using

Customer customer = Customer.get(params.customerId)

which is failing even if there is a customer document with the same id in mongodb.

but if I do something like,

Customer customer = Customer.list().toList().find { it.id.toString() == params.customerId}

it works.

Any idea why this is happening? I am not sure if this is a problem with mongodb or morphia. Any help is greatly appreciated.

Thanks in Advance :)

Pazuzu
  • 23
  • 7
  • I haven't used Morphia/Grails but I have worked with Grails and Mongo (Gorm)...you look to be having a String/ObjectId mismatch. Does Customer.get() take a string or an ObjectId? – Todd Murray Mar 15 '13 at 14:45
  • it takes an ObjectId, but when you use domain.get() it does not matter and the strangest thing is that, the same code works for CustomerA. This is Strange – Pazuzu Mar 15 '13 at 15:04
  • if it was what you are saying i,e, mismatch String/ObjectId it shouldn't work for any other customer objects.. but this does! – Pazuzu Mar 15 '13 at 15:06

1 Answers1

0

I found the problem, after many hours of debugging :), the problem was there were cross references to deleted documents in mongo, this usually happens if you have a copy of any of the object, which has reference to other documents in mongo. To avoid this problem make sure there are no copies, if there are make sure they are either embedded or referenced copies.

Pazuzu
  • 23
  • 7
  • look at my another post on how to correct this problem [here](http://stackoverflow.com/questions/16414872/updating-nested-emdedded-list-containing-basicdbobject-to-dbref-in-mongodb-using) – Pazuzu May 15 '13 at 12:34