javamongodbjongo

hierarchy structure in mongodb with jongo in java?


I have a java project with the following structure:

An abstract class Event and other 3 sub classes EventA, EventB and EventB.

The code is something like this.

public abstract class Event {

    @MongoId
    @MongoObjectId
    private String id;

    private Place place;
    private Person person;

    public Event() { }

    //more code
}

public class EventA extends Event {

    private OtherObject other;

    public EventA() { }
    //more code
}

//more classes 

In a service class, I have to query for a List , so...

How can I do to model the correct Mongo-hierarchy/inheritance in java?


Solution

  • I found the solution, maybe it could be useful for someone with the same problem:

    So, you can declare every subclass over the abstract class

    @JsonTypeInfo(use= JsonTypeInfo.Id.CLASS, property="_class")
    @JsonSubTypes({
        @JsonSubTypes.Type(value = EventA.class),
        @JsonSubTypes.Type(value = EventB.class),
        @JsonSubTypes.Type(value = EventC.class)
    }) 
    public abstract class Event(){
       //code
    }