【问题标题】:Map a view with the database result object使用数据库结果对象映射视图
【发布时间】:2017-10-18 15:44:38
【问题描述】:

我正在尝试使用在 Spring JPA 中查询 Postgres DB 后得到的结果集对象映射视图。 但我不知道该怎么做,任何帮助将不胜感激

 @Query(nativeQuery = true, value = "select e.job_number, p.tag_number, e.drawing_number, p.part_number, "
            + "eoa.operation_order, peoa.name, peoa.status, peoa.comment, peoa.last_updated_by, "
            + "peoa.last_updated_date, peoa.revtype "
            + "from brs.events e "
            + "join brs.parts p on e.id = p.event_id "
            + "join brs.event_operations_audit eoa on eoa.event_id = e.id "
            + "join brs.part_event_operations_audit peoa on peoa.part_id = p.id and peoa.event_operation_id = eoa.id "
            + "where e.id = :eventId "
            + "and e.is_received = true "
            + "and e.is_workscope_reviewed = true "
            + "order by p.tag_number, eoa.operation_order, peoa.last_updated_date ")
    Set<Object> getJobPartRecentActivityData(@Param("eventId")Long eventId);

https://pastebin.com/iDZDhpce

【问题讨论】:

    标签: java jpa spring-boot spring-data-jpa


    【解决方案1】:

    这可以通过下面的例子来实现。 创建一个类说 BookValueMappig

    @SqlResultSetMapping(
            name = "BookValueMapping",
            classes = @ConstructorResult(
                    targetClass = BookValue.class,
                    columns = {
                        @ColumnResult(name = "id", type = Long.class),
                        @ColumnResult(name = "title"),
                        @ColumnResult(name = "version", type = Long.class),
                        @ColumnResult(name = "authorName")}))
    

    然后在 JPA 中包含映射文件:

    List<BookValue> results = this.em.createNativeQuery("SELECT b.id, b.title, b.version, a.firstName || a.lastName as authorName FROM Book b JOIN Author a ON b.author_id = a.id", "BookValueMapping").getResultList();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-07-18
      • 2021-12-01
      • 2020-12-09
      • 2012-07-05
      • 2022-12-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多