【问题标题】:How to do a Findby In Springboot Mongo repository for Nested Objects如何在 Spring Boot Mongo 存储库中为嵌套对象执行 Findby
【发布时间】:2021-09-28 07:16:10
【问题描述】:

在 Spring Data CrudRepository 中需要您的帮助,了解如何对嵌套字段进行 findby。 我的类具有以下结构,我需要使用嵌套对象的truckId 进行查询

@Document(collection = "unt-truck")
public class TruckModelDTO {
    private String type;
    private TestDTO testDTO;

}

班级TestDTO.java

public class TestDTO{

    private TruckDTO truckDTO;
    Private String version;

}

班级TruckDTO.java

public class TruckDTO {

    private String truckId;
    private String legacySystem;

}

班级TruckRepository.java

@Repository
public interface TruckRepository extends MongoRepository<TruckModelDTO, String> {


    // TruckModelDTO findByTruckId(String truckid);

}

那么我应该如何将findby 用于嵌套类中的truckId

【问题讨论】:

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


    【解决方案1】:

    您可以使用 Entity 或 DTO 作为响应。但您必须根据您的要求提及两个或三个实体之间的关系。

    我创建了实体。您必须基于实体创建具有相同的 DTO,然后在 JPA 存储库查询中应用主 DTO 作为返回类型。

    1.实体

    @Entity
    public class TruckModel {
        private String type;
    
        // mention the relation based on your requirement
        private Test test;
    
    }
    
    1. 实体
    @Entity
    public class Test {
        // here mention the relation based on you requirement
        private Truck truck;
        Private String version;
    
    }
    

    3.实体

    @Entity
    public class Truck {
    
        private String truckId;
        private String legacySystem;
    
    }
    
    1. 存储库

    @Repository 公共接口 TruckModelRepository 扩展 MongoRepository {

     TruckModelDTO findByTestTruckTruckId(String truckid);
    

    }

    【讨论】:

      猜你喜欢
      • 2023-03-27
      • 2012-09-25
      • 2020-11-28
      • 1970-01-01
      • 2020-07-19
      • 1970-01-01
      • 1970-01-01
      • 2015-05-31
      • 2019-06-19
      相关资源
      最近更新 更多