英文:
How to retrieve a single result from 2 entity
问题
| 报告1   | 报告2     |
| ------ | --------  |
| sid    | sid       |
| name   | name      |
| salary | department|
报告1.sid == 报告2.sid
我的目标是一起查看两个不同实体的结果。这两个 sid 报告将是用户相同且唯一的。我想要显示的结果是 sid、name、salary、department。我可以分别查看它们,因为我正在使用 findAll(),但我希望在单个 findAll() 中查找所有数据。我尝试使用联接列,但它向我的数据库表添加了另一列。
实体
报告1
@AllArgsConstructor
@NoArgsConstructor
@ToString
@Data
@Entity
@Table(name = "REPORT1")
public class Report1 {
    @Id
    private String SID;
    private String NAME;
    private double AMOUNT;
}
-------------------------------
报告2
@AllArgsConstructor
@NoArgsConstructor
@ToString
@Data
@Entity
@Table(name = "REPORT2")
public class Report2 {
    @Id
    private String SID;
    private String DEPARTMENT;
    private String NAME;
}
repo
public interface Report1Repository extends JpaRepository<Report1, Integer> {
}
public interface Report1Repository extends JpaRepository<Report2, Integer> {
}
英文:
| Report1 | Report2   |
| ------  | --------  |
| sid     | sid       |
| name    | name      |
| salary  | department|
report1.sid == report2.sid
My goal is to view the result of 2 different entity data together. Both sid report will be same and unique to the user. The result I wanted to show would sid,name,salary,department. I am able to view them separately as I am using the findAll() however I wish to find all the data in a single findAll()
I have tried using join columns however it added another column to my database table.
Entity
Report1
@AllArgsConstructor
@NoArgsConstructor
@ToString
@Data
@Entity
@Table(name = "REPORT1")
public class Report1 {
    @Id
    private String SID;
    private String NAME;
    private double AMOUNT;
}
-------------------------------
Report2
@AllArgsConstructor
@NoArgsConstructor
@ToString
@Data
@Entity
@Table(name = "REPORT2")
public class Report2 {
    @Id
    private String SID;
    private String DEPARTMENT;
    private String NAME;
}
repo
public interface Report1Repository extends JpaRepository<Report1,Integer> {
}
public interface Report1Repository extends JpaRepository<Report2,Integer> {
}
答案1
得分: 1
你可以像这个示例一样使用元组和自定义查询:
对于你的仓库:
public interface Report1Repository extends JpaRepository<Report1, Integer> {
    @Query(native = true, value = 'select r1.sid sid, r1.name name, r1.salary salary, r2.department department from report1 r1 left join report2 r2 on r1.sid = r2.sid')
    List<ReportTuple> findAllReports();
}
然后创建一个元组接口如下:
public interface ReportTuple {
    
    Long getSid();
    String getName();
    String getDepartment();
    Long getSalary();
    
}
英文:
you can use tuple and custom query like this example :
for your repository :
public interface Report1Repository extends JpaRepository<Report1,Integer> {
@Query(native=true, value='select r1.sid sid, r1.name name, r1.salary salary, r2.department department from report1 r1 left join report2 r2 on r1.sid = r2.sid')
List<ReportTuple> findAllReports();
}
and create a tuple interface like this :
public interface ReportTuple{
    
    Long getSid();
    String getName();
    String getDepartment();
    Long getSalary();
    
    }
答案2
得分: 0
创建自定义类,类似于DTO或VO,以表示所有数据,并创建自定义查询,返回所有记录并相应地准备DTO列表。
英文:
Create Custom Class like DTO or VO to represent all the data and create custom query to which returns all the records and prepare DTO list accordingly
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论