Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

300
Views
Morphia/Mongo not able to link @Reference, giving : The reference could not be fetched

I have set up a simple datastore to store a Version and a Build. Here are their classes-

@Entity("Version")
public class Version {
    @Id
    private ObjectId id = new ObjectId();
    public Version() {}
    private String name;
}
@Entity("Build")
public class Build {
    public Build() {}
    @Id
    ObjectId id = new ObjectId();
    @Reference
    Version version;
    String name;    
}

(the fields in the classes having their getters, setters, constructors and toString implemented).

Now I'm trying to simply store the Version first and then try to store a Build and then read all the builds there are present.

Version version = new Version("first");
Version version2 = new Version("second");

VersionDAO.saveVersion(version);
VersionDAO.saveVersion(version2);
VersionDAO.printAllVersions();

Build build = new Build(version, "Hello");
BuildDAO.saveBuild(build);
BuildDAO.getAllBuilds();

VersionDAO.getAllVersions() works fine and gets me the list of versions stored but BuildDAO.getAllBuilds() throws an error saying-

Exception in thread "main" dev.morphia.mapping.MappingException: Could not map trying_morphia.Build with ID: 602f4e9ff760cd5638698273 in database 'Builds'

Caused by: dev.morphia.mapping.MappingException: The reference ({ "$ref" : "Version", "$id" : "602f4e7b96f28d54bd1cc883" }) could not be fetched for trying_morphia.Build.version

There seems to be some reference linking issue that I cant seem to figure out. I have to use the @Reference as this piece of code is a part of a bigger piece and I cant change the whole of it.

The MongoDB Compass shows the value of a Build entry as-

_id: ObjectId("602f4e9ff760cd5638698273")  
className: "trying_morphia.Build"  
version: DBRef(Version, 602f4e7b96f28d54bd1cc883, undefined)  
name:"Hello"  

What am I missing in the reference?

What I've tried: Changing the id of both Version and Build to string.

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Cross database references are not supported in Morphia. You will need to manage those references manually.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!