MorphiaOrg / MorphiaOrg/morphia

getEntityModel on @ExernalEntity anntotated class fails when called more than once

Open
#3,227 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug Waiting for Response
Dominant language
Java
Stars
1.7k
Forks
449
Avg merge
7m
Merged PRs (30d)
10

Description

Description
Suppose we have the following:

public class ThirdPartyEntity {
    public String field;
    public Long number;
}

and

@ExternalEntity(target = ThirdPartyEntity.class)
public class ThirdPartyEntityMixIn {
    public String field;
    @Id
    public Long number;
}

When calling datastore.getMapper().getEntityModel(ThirdPartyEntityMixIn.class), the entity model of ThirdPartyEntity is returned correctly the first time. However, on the second call, getEntityModel fails due to a duplicated discriminator error:
Two entities have been mapped using the same discriminator value (com.example.ThirdPartyEntityMixIn): com.example.ThirdPartyEntity and com.example.ThirdPartyEntity

Cause
The issue occurs because when entities are being put into mappedEntities, the key used is entityModel.getType(), which refers to ThirdPartyEntity.class. But when retrieving the model in getEntityModel, the key used is ThirdPartyEntityMixIn.class, leading to a conflict when Morphia attempts to register the entity again.

Morphia Version:
2.4.4

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the issue with the ThirdPartyEntity and ThirdPartyEntityMixIn examples, calling getEntityModel twice. Inspect getEntityModel and the mappedEntities registration and lookup paths. Done means repeated calls no longer trigger the duplicate discriminator error and return the expected entity model.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, mongodb
Domain
databases
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.