Shisoft
Shisoft

Reputation: 4245

I got a missing reference in mongodb with morphia

I got two class, one reference another by using @Reference When inserting I will insert the referenced one first and insert the object with the reference field later. Everything works fine when I fetch them in the most of time.But sometimes, I got exceptions like

SEVERE: java.lang.RuntimeException: com.google.code.morphia.mapping.MappingException: The reference({ "$ref" : "UserContactLink", "$id" : "50e92481cde5dadc12ff854b" }) could not be fetched for net.shisoft.db.obj.UserContact.ucs

When I checked the id in UserContactLink and there is no such document with this id. I think this is because I terminate the progress of mongod last time and the transaction (in my viewpoint) didn't finished and the data relation has been corrupted. Seems mongodb don't have transaction feature, what can I do with this issue?

Upvotes: 0

Views: 872

Answers (1)

xeraa
xeraa

Reputation: 10859

  1. There are no transactions. In many cases you can restructure your documents to avoid problems with that (embedding documents,...)

  2. You will always need to insert the referenced document first. Upon insert, the MongoDB server creates the ObjectId of the entity which is then used in the reference. You might want to check for the ID before you reference (simple check for null).

Upvotes: 1

Related Questions