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 :)