Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8002725
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T16:26:54+00:00 2026-06-04T16:26:54+00:00

I’ve got two kinds of documents in my MongoDB: clients and codes. Each code

  • 0

I’ve got two kinds of documents in my MongoDB: clients and codes. Each code refers to one client. Clients have to be stored explicitly by an administrator, therefore I have to store them separate and cannot put them into a code document.

code -> client

Now MongoDB / Morphia saves technical ids of clients as ObjectId, whereas codes refer to clients with a technical id of type String. I am able to search a code by a given client id, but at the runtime I’ll get a error message, because Morphia cannot inject the client. I assume it’s because of the different id types.

code { client.$id: String }
client { _id: ObjectId }

Any ideas how to fix this?

Exception

com.google.code.morphia.mapping.MappingException: The reference({ “$ref” : “clients”, “$id” : “123456789abcdef” }) could not be fetched for org.example.Code.client

On the internet I found that exception message. It was suggested to use ObjectId instead of String in the model, but I have the requirement to use String. This is not my own project.

Entities:

@Entity("codes")
public class Code implements Comparable<Code> {
    @Id
    private String id;

    @Reference
    private Client client;

    [...]
}

@Entity("clients")
public class Client {
    @Id
    private String id;
}

Storing:

To store the objects I use com.google.code.morphia.dao.DAO.save(T entity).

Search:

public class CodeRepository extends BasicDAO<Code, String> {
    [... constructor ...]

    @Override
    public Code findByCode(String type, String clientId, String code) {
        return findOne(createQuery()
                .field("type")
                .equal(type)
                .field("value")
                .equal(code)
                .field("client")
                .equal(new Key<Client>(Client.class, clientId)));
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T16:26:56+00:00Added an answer on June 4, 2026 at 4:26 pm

    not sure if this is solved yet. I had the same problem. The solution for me was to set the id myself.

    @Id
    private String id = new ObjectId().toString();
    

    Now you can treat the id field like any other string field.

    Hope this helps.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have a reasonable size flat file database of text documents mostly saved in
I have a bunch of posts stored in text files formatted in yaml/textile (from
I am trying to loop through a bunch of documents I have to put
I have just tried to save a simple *.rtf file with some websites and
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a jquery bug and I've been looking for hours now, I can't
Basically, what I'm trying to create is a page of div tags, each has

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.