【问题标题】:How to map the result from a native query result involving columns from multiple table into a Custom class using JPA Repository?如何使用 JPA 存储库将涉及来自多个表的列的本机查询结果映射到自定义类?
【发布时间】:2020-04-23 23:23:55
【问题描述】:

我确实试图找到一些类似的问题,但我无法理解我的问题。 我有一个如下的存储库

@Repository
public interface CartRepository extends JpaRepository<Cart, Long> {

    @Query(value = "Select i.item_desc, i.item_name ,sum(price) as price, count(*) as quantity from items  i, cart_items ci, cart c where ci.items_item_id=i.item_id and c.id = ci.cart_id and c.user_id = :userId group by ci.items_item_id", nativeQuery = true)
    public List<Object> getCartItemsForCustomer(long userId);

}

查询结果会有四个字段,item_desc、item_name、price和quantity。

我创建了一个 DTO 类以将结果作为 DTO 对象列表返回。

public class CartItemDto {

   private String itemName;
   private String itemDesc;
   private Long price;
   private Integer quanity;
}

我不明白如何将结果映射到自定义 dto 类中。

List<Object> objs = cartRepository.getCartItemsForCustomer(userId);

请建议将对象列表转换为自定义类列表的方法。

【问题讨论】:

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


    【解决方案1】:

    您可以像这样使用Interface-based Projections 解决这个问题:

    @Query(value = "Select i.item_desc as itemDesc, i.item_name as itemName, sum(price) as price, count(*) as quantity from items  i, cart_items ci, cart c where ci.items_item_id=i.item_id and c.id = ci.cart_id and c.user_id = ?1 group by ci.items_item_id", nativeQuery = true)
    public List<CartItemInterface> getCartItemsForCustomer(long userId);
    

    购物车界面

    public interface CartItemInterface {
    
        String getItemDesc();
        String getItemName();
        Long getPrice();
        Integer getQuantity();
    }
    

    【讨论】:

      【解决方案2】:

      您可以使用 DTO 投影并将列映射到 Java 对象。

      • 您需要创建一个接受这四个字段和 创建对象。
      • 更改选择查询以提供类的完全限定名称并调用其构造函数。

      注意:如果您使用的是静态类,请使用 $ 而不是 (.) 进行引用

      public class CartItemDto {
         private String itemName;
         private String itemDesc;
         private Long price;
         private Integer quanity;
         public CartItemDto(String itemName, String itemDesc, Long price, Integer quanity) {
            this.itemName = itemName;
            this.itemDesc = itemDesc;
            this.price = price;
            this.quantity = quantity;
         }   
      

      }

      现在更新查询以实例化结果集中每一行的 dto 对象并返回 CartItemDto 对象列表。

      @Query(value = "Select new com.package.CartItemDto(i.item_desc, i.item_name ,sum(price), count(*)) from items  i, cart_items ci, cart c where ci.items_item_id=i.item_id and c.id = ci.cart_id and c.user_id = :userId group by ci.items_item_id")
          public List<CartItemDto> getCartItemsForCustomer(long userId);
      

      注意:这不适用于本机查询。您需要更新查询并将列和表名称更改为字段和实体名称。还将 nativeQuery 标志更改为 false(默认为 false,因此只需将其删除)

      【讨论】:

      • 这如何工作?使用新的 com.package.CartItemDto 进行本机查询???
      • @Lemmy 错过了 nativeQuery 标志。更新答案。
      猜你喜欢
      • 2020-04-02
      • 2021-02-21
      • 2016-09-22
      • 2020-04-07
      • 2014-06-25
      • 1970-01-01
      • 2011-02-15
      • 2019-05-23
      • 2018-01-27
      相关资源
      最近更新 更多