合并两个实体并获取数据

时间:2019-03-29 09:08:49

标签: hibernate jpa

我有两个实体类FlcDrawingRequests和FlcDrawingRequestStats。 从两个表中获取数据,我尝试使用下面的query从两个表中获取数据。无法获取数据。能否请任何一个人帮我这个忙。

 private static final String querystat = "select dr, ds from 
 FlcDrawingRequests AS dr,FlcDrawingRequestStats AS ds where dr.id=ds.request_id";

下面是两个实体类

FlcDrawingRequests

@Entity(name = "FlcDrawingRequests")
@Table(name = "drawing_requests", catalog = FlcWsDao.DB_SCHEMA_NAME)
public class FlcDrawingRequests {

    @Id
    private String id;
    @Column(name = "creation_time", nullable = false, updatable = false)
    private Date creation_time;
    @Column(name = "drawing_spec_format", length = 16)
    private String drawing_spec_format;
    @Column(name = "end_time")
    private Date end_time;
}

FlcDrawingRequests

@Entity(name = "FlcDrawingRequestStats")
@Table(name = "drawing_request_stats", catalog = FlcWsDao.DB_SCHEMA_NAME)
public class FlcDrawingRequestStats {
    @Id
    private String request_id;
    @Column(name = "bb", length = 11)
    private Integer bb;
    @Column(name = "car_offset_g", length = 11)
    private Integer car_offset_g;
    @Column(name = "car_sling_type", length = 32)
    private String car_sling_type;
    @Column(name = "car_type", length = 16)
    private String car_type;
    @Column(name = "ch", length = 11)
    private Integer ch;
}

下面是查询执行:

StringBuilder queryBuilder = new StringBuilder(querystat);
System.out.println("startDate--->" + startDate);
Query query =getEntityManager().createQuery(queryBuilder.toString());
System.out.println("query.list();--->" + query.getResultList().size());
return query.getResultList();

1 个答案:

答案 0 :(得分:0)

 private static final String querystat = "SELECT dr, ds FROM 
 FlcDrawingRequests dr,FlcDrawingRequestStats ds WHERE dr.id LIKE ds.request_id";

并且结果必须存储在List<Object[2]>中,因此您必须具有:

List<Object[2]> result=query.getResultList();

因此,这里有一个表的列表,该表的大小等于2,列表中的每个元素都包含两个Object,第一个是FlcDrawingRequests,第二个是FlcDrawingRequestStats。