简体   繁体   English

如何使用 Spring Boot 的 JPA 存储库中的映射从 spring 引导中的多个表中获取数据

[英]How to fetch data from multiple tables in spring boot using mapping in Spring Boot's JPA repository

I have created four entity classes as:我创建了四个实体类:

@Entity
@Table(name = "DashboardRegionCountry")
public class DashboardRegionCountry implements Serializable {

    @Id
    @Column(name = "id")
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    @Column(name = "dashboardId")
    private long dashboardId;

    @OneToOne(targetEntity = Country.class)
    @JoinColumn(name="countryId")
    private Country country;
    
    @OneToOne(targetEntity = Region.class)
    @JoinColumn(name="regionId")
    private Region region;

    @ManyToOne()
    @JoinColumn(name="dashboardId")
    private Dashboard dashboard;
}

@Entity
@Table(name = "Dashboard")
public class Dashboard implements Serializable {

    @Id
    @Column(name = "dashboardId")
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long dashboardId;

    @Column(name = "dashboardName")
    private long dashboardName;

    @OneToMany(mappedBy= dashboard)
    private List<DashboardRegionCountry> dashboardRegionCountry;
    
}

@Entity
@Table(name = "Country")
public class Country implements Serializable {

    @Id
    @Column(name = "countryId")
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long countryId;

    @Column(name = "shortName")
    private String shortName;
    
    @Column(name = "longName")
    private String longName;
 
}

@Entity
@Table(name = "Region")
public class Region implements Serializable {

    @Id
    @Column(name = "regionId")
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long regionId;

    @Column(name = "shortName")
    private String shortName;
    
    @Column(name = "longName")
    private String longName;
}

And the table schemas for the respective entities are as follows:各个实体的表模式如下:

DashboardRegionCountry:仪表板地区国家:

+----------------+---------------+
| Field          | Type          |
+----------------+---------------+
| id(PK)         | Number(11)    |
| dashboardId(FK)| Number(11)   |
| countryId      | Number(11)    |
| regionId       | Number(11)   |
+-------------+------------------+

Dashboard:

+----------------+---------------+
| Field          | Type          |
+----------------+---------------+
| dashboardId(PK)| Number(11)    |
| dashboardName  | varchar(11)   |
+-------------+------------------+

Country:
+-------------+---------------+
| Field       | Type          |
+-------------+---------------+
| countryId(PK)| Number(11)   |
| shortName   | Varchar2(10)  |
| longName    | Varchar2(10)  |
+-------------+---------------+

Region:
+-------------+---------------+
| Field       | Type          |
+-------------+---------------+
| regionId(PK)| Number(11)    |
| shortName   | Varchar2(10   |
| longName    | Varchar2(10)  |
+-------------+---------------+

Basically, when user enters the dashboardId then we want to fetch, dashboardDetails along with the Region and respective countries presnt in that region.基本上,当用户输入dashboardId 时,我们想要获取dashboardDetails 以及该地区和该地区的各个国家。 As stated above, I only have region and country Ids in my table and their names are present in other tables.如上所述,我的表中只有地区和国家 ID,它们的名称出现在其他表中。 I want to display my sample output something like:我想显示我的示例 output 类似:

{
 "dashboardId":20,
  "DashboardRegionCountry": [{
    "Region":"ASIA",
    "dashboardId":["India","China"]
  },
  {
    "Region":"NAM",
    "dashboardId":["USA","Canada"]
  }
  ]
}

I am trying to write JPA repository but was wondering is it possible to write something like:我正在尝试编写 JPA 存储库,但想知道是否可以编写如下内容:

@Repository
public interface DashboardRegionCountryRepository extends JpaRepository<DashboardRegionCountry, Long>{

    List<Map<Long,Country>> findRegionBy_RegionId(Long dashboardId);
}

I am trying to fetch all the data in one query, any suggestion will be really helpful我正在尝试在一个查询中获取所有数据,任何建议都会非常有帮助

Just get the corresponding DashboardRegionCountry using getById (check the reference documentation ) and it will contain both associated Country and Region .只需使用getById获取相应的DashboardRegionCountry (查看参考文档),它将包含相关的CountryRegion If you then don't want to expose all Country and Region information in your entities I suggest you map them to a DTO that would be the model that you want to use while returning something on your controllers.如果您不想在您的实体中公开所有CountryRegion信息,我建议您将它们 map 提供给 DTO,该 DTO 将是您想要在控制器上返回某些内容时使用的 model。

findByRegion_regionId try this this will work. findByRegion_regionId 试试这个,这会起作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM