Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/339.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在Spring data Mongodb中获取继承的属性_Java_Mongodb_Spring Boot_Spring Data_Spring Data Mongodb - Fatal编程技术网

Java 在Spring data Mongodb中获取继承的属性

Java 在Spring data Mongodb中获取继承的属性,java,mongodb,spring-boot,spring-data,spring-data-mongodb,Java,Mongodb,Spring Boot,Spring Data,Spring Data Mongodb,我正在使用Spring boot和mongodb。当我们查询继承的类时,我面临一个问题。为了简单起见,我使用以下结构来解释。我有三个实体BaseEntity,Bird和Human基本实体可以是独立的。但是Bird和Human扩展BaseEntity。我使用@JsonTypeInfo进行继承类型用于查找鸟类或人类 @JsonTypeInfo(use = JsonTypeInfo.Id.NAME,include = JsonTypeInfo.As.EXISTING_PROPERTY, propert

我正在使用Spring boot和mongodb。当我们查询继承的类时,我面临一个问题。为了简单起见,我使用以下结构来解释。我有三个实体
BaseEntity
Bird
Human
<代码>基本实体可以是独立的。但是
Bird
Human
扩展
BaseEntity
。我使用
@JsonTypeInfo
进行继承<代码>类型用于查找鸟类或人类

@JsonTypeInfo(use = JsonTypeInfo.Id.NAME,include = JsonTypeInfo.As.EXISTING_PROPERTY, property = "type", visible = true)
@JsonSubTypes({
        @JsonSubTypes.Type(value = Bird.class, name = "BIRD"),
        @JsonSubTypes.Type(value = Human.class, name = "HUMAN")
})
@Document(collection="base-entity")
class BaseEntity{
    @Id Integer id;
    String name;
    String type;
}

@Document(collection="base-entity")
@JsonTypeName("BIRD")
class Bird extends BaseEntity{
    Integer age;
    String flySpeed;
}

@Document(collection="base-entity")
@JsonTypeName("HUMAN")
class Human extends BaseEntity{
    Integer age;
    Sting grade;
}
基本实体集合中的样本文档

{ id:1, name:"Pine", type:"TREE" } // BaseEntity only
{ id:2, name:"Hen", type:"BIRD", age:7, flySpeed:"1" } // Bird
{ id:3, name:"John", type:"HUMAN", age:12, grade: "1st grade" } // Human
我需要得到7岁和12岁的输出

我的存储库是

public interface BaseEntityRepository extends MongoRepository<BaseEntity, Integer> {
    List<BaseEntity> findByAgeIn(List<Integer> ages);
}
但我的预期产出是

{ id:2, name:"Hen", type:"BIRD", age:7, flySpeed:"1" }
{ id:3, name:"John", type:"HUMAN", age:12, grade: "1st grade" }
我试过了,但没能成功。如何获取继承对象的所有属性。提前谢谢

{ id:2, name:"Hen", type:"BIRD", age:7, flySpeed:"1" }
{ id:3, name:"John", type:"HUMAN", age:12, grade: "1st grade" }