【问题标题】:Spring Boot Mongo DB InheritanceSpring Boot Mongodb 继承
【发布时间】:2019-03-27 22:49:42
【问题描述】:
Class A{
public String phone;
}

@Document
Class B extends A{
public String location;
///getter and setter
}

@Repository
public interface B extends MongoRepository<B, String> {

   List<B> findByphone(String wphone);

}

当使用这个 findByphone(phone) 我的 api 说这个

“状态”:500, "error": "内部服务器错误", "message": "在类 com.example.demo.model.B 上找不到属性 'phone'!您的意思是:phone,Phone?"

【问题讨论】:

  • 你在A类上试过@MapperSuperClass注解吗?
  • 您应该将findByphone 更改为findByPhone。骆驼案被忽视了。

标签: mongodb spring-boot spring-data-jpa


【解决方案1】:

尝试在您的 mongodb 字段中使用 @Field,并且您还需要为每个文档关联一个 @Id

Class A {

    @Field
    public String phone;

    // getters and setters
}

@Document(collection = "B")
Class B extends A {

    @Id
    private String id;
    @Field
    public String location;

    // getter and setter
}

同样在你的仓库界面中使用类似的方法

 List<B> findByPhone(String wphone);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-08
    • 2019-04-06
    • 2017-10-10
    • 2018-11-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多