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

  • Home
  • SEARCH
  • 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 8501417
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T01:12:27+00:00 2026-06-11T01:12:27+00:00

Although I tagged this question as a Morphia issue, I think it’s more general,

  • 0

Although I tagged this question as a Morphia issue, I think it’s more general, but anyway, help is appreciated.

I have IUser interface that I don’t have control over it:

public interface IUser<T> {
    ...
    public String getFirstName();
    ...
}

I, also, have an implementation User (Morphia entity) of this interface:

@Entity
public class User implements IUser<ObjectId> {
    @Id
    private ObjectId id;

    @Property
    private String firstName;

    public String getFirstName() {
        return this.firstName;
    }
    ...
}

When Morphia asks for the classes to be mapped, I provide User.class, since this is the Morphia entity. Therefore, when I extend the BasicDAO, I provide User and ObjectId as types:

public class UserDAO extends MongoDAO<User, ObjectId> {
    public List<IUser<ObjectId>> getUsers(String id) {
        return ds.find(IUser.class, Mapper.ID_KEY, new ObjectId(id)).asList(); 
    }
}

Now, the problem appears in the getUsers method. As you can see, I want to continue working with the interface outside this class; that’s why I expect List<IUser<ObjectId>> as a return type.

The first argument of the ds.find(…) method is a Class<T> type, so I provide IUser.class. But, because of this, I cannot anymore expect List<IUser<ObjectId>> as a result, but just List<IUser>. This way, I have lost the type of IUser. Is it possible at all to force this Morphia’s method to return a list of parameterized IUser objects?

Thank you in advance,
Zlatko

  • 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-11T01:12:28+00:00Added an answer on June 11, 2026 at 1:12 am

    There are a couple of options.

    What Guava, Guice and similar libraries do is accept a TypeToken which leverages an anonymous inner class to reify the types. It would look something like this:

    return ds.find(new TypeToken<IUser<ObjectId>>(){}, ...);
    

    But obviously that’s not really an option here since you can’t change the library. It is what I’d consider to be the correct solution however.

    The second option is to just cast the class to contain the generic parameter. This results in an unchecked warning however that needs to be suppressed or ignored:

    @SuppressWarnings({"unchecked"}) 
    Class<IUser<ObjectId>> userClass = (Class<IUser<ObjectId>>) (Object) IUser.class;
    return ds.find(userClass, ...);
    

    Edit:

    It should really be noted that the suggestion above is really no better than just casting the entire list from a List<IUser> to a List<IUser<ObjectID>> before returning it. Both carry the same need to suppress warnings.

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

Sidebar

Related Questions

This question is closely related to this one but i think is more general.
Although this question fancies PostgreSQL, it is still a general DB question. I have
Although somewhat related to this question , I have what I think is a
Although I'm programming in C++, this is more of a general question about design.
Although I'm primarily interested in Java/GlassFish with this question, I would certainly think it
Although this is django related, it's really just a general, programming efficiency question. I
Although I have found partial and indirect answers to this question (see, e.g., this
Although this code is from an algorithm text, I have a bit of a
Although I have found some solutions to this problem, none of them refer to
Although I have read a few previously answered questions regarding a similar issue, I

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.