Skip to content

TypeAlias annotation doesn't parced correctly for generic [DATAMONGO-2433] #3290

@spring-projects-issues

Description

@spring-projects-issues

Slimperator opened DATAMONGO-2433 and commented

Problem Description

When I tried to get an entity from my database:

GenericEntity child = mongoTemplate.findById(ENTITY_ID, GenericEntity.class); // If I using the GenericEntity repository I have the same problem

I have the following different behaviors:

Case 1(Failure):

Preparation:
If I trying to get an entity and I didn't any save operation with the child's entity,
Result:
The template parsing entity as "GenericEntity" and called its constructor. An entity has no child's field.

Case 2(Success):

Preparation:
If I trying to get an entity after I did any save operation with the child's entity
Result:
The template parsing entity correctly, using the child's constructor. An entity has a child's field.

Case 3(Success):

Preparation:
I trying to get an entity and I didn't any save operation with the child's entity, BUT I have a repository as:

public interface GenericChildRepository extends MongoRepository<NotiChild, String> {
}

And this repository injected in any bean.
Result:
The template read entity correctly, using the child's constructor. An entity has a child's field.

Possible root cause

As I understand it, the problem is how the "mappingContext.getPersistentEntities()" of the "MappingContextTypeInformationMapper" class is filled.

Because in the Case 1, the "mappingContext.getPersistentEntities()" returns list with ChildEntity, but without value of it's TypeAlias annotation.

But if

  • the Save operation was used (The TypeAlias value is cached after write operation)
  • or an injected repository exists (All entities associated with the repository filled the context with duplicates)

the "mappingContext.getPersistentEntities()" has required values.

Data Model

The parent entity:

@Getter
@Setter
@ToString
@EqualsAndHashCode
@TypeAlias("Parent")
@Document
public class GenericEntity <T extends Notification>{
    @Id
    private String id;
    private List<T> wrappers;
}

Child entity:

@Getter
@Setter
@ToString(callSuper = true)
@TypeAlias("Child")
@Document
public class GenericChild extends GenericEntity<FooNotification>{
    String technicalDescription;
}

Notification entity:

@Data
public class Notification {
    private String id;
}

Notification child entity:

@Getter
@Setter
@ToString(callSuper = true)
@TypeAlias("NotificationFoo")
public class NotiChild extends Notification {
    private String something;
}

Project dependensies

  • spring-boot-starter-parent - 2.1.2.RELEASE
  • spring-boot-starter-data-mongodb
  • mongo-java-driver - 3.11.1

Affects: Backlog

Metadata

Metadata

Labels

in: coreIssues in core supporttype: bugA general bug

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions