从2个实体中检索单个结果。

huangapple go评论69阅读模式
英文:

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 = &quot;REPORT1&quot;)
public class Report1 {
    @Id
    private String SID;
    private String NAME;
    private double AMOUNT;
}
-------------------------------
Report2
@AllArgsConstructor
@NoArgsConstructor
@ToString
@Data
@Entity
@Table(name = &quot;REPORT2&quot;)
public class Report2 {
    @Id
    private String SID;
    private String DEPARTMENT;
    private String NAME;
}

repo

public interface Report1Repository extends JpaRepository&lt;Report1,Integer&gt; {
}
public interface Report1Repository extends JpaRepository&lt;Report2,Integer&gt; {
}

答案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&lt;Report1,Integer&gt; {
@Query(native=true, value=&#39;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&#39;)
List&lt;ReportTuple&gt; 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

huangapple
  • 本文由 发表于 2023年5月22日 19:45:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/76305835.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定