spring-data-jpa 创建模型并使用@Query插入值

hmmo2u0o  于 2022-11-10  发布在  Spring
关注(0)|答案(1)|浏览(150)

我有这样的课:

@Getter
@Setter
@ToString
@AllArgsConstructor
@NoArgsConstructor
public class ViewOrderRTO {
    private List<LineProductModel> products;
    private OrderModel order;
}

我用来显示保存在db. LineProductModel中数据的类是一个具有以下字段的类:总价、数量、id、与productModel的@多对一关系以及与OrderModel的@多对一关系。问题是:我是否可以在类似于“SELECT new alongpath.ViewOrderRTO(....)...”这样的语句中使用@Query,并在一个查询中插入lineProduct列表和其他字段?或者我必须拆分工作并获取所有单个LineProductModel,首先将其放入列表中,然后创建ViewModel?
顺便说一句,这是一个Spring Boot 项目,我正在使用mysql

zxlwwiss

zxlwwiss1#

我“解决”了在LineProductModel中删除@ManyToOneMap并在Order Model中添加@OneToManyMap的问题,现在我有了:

@Entity
    @Table(name = "order_commission")
    @Getter
    @Setter
    @ToString
    @AllArgsConstructor
    @NoArgsConstructor
    public class OrderModel {
        @Id
        @SequenceGenerator(name = "order_sequence",
                sequenceName = "order_sequence",
                initialValue = 0,
                allocationSize = 1
        )
        @GeneratedValue(strategy = GenerationType.SEQUENCE,
                generator = "order_sequence")
        private Long id;
        @OneToMany(cascade = CascadeType.ALL,
                fetch = FetchType.EAGER,
                orphanRemoval = true)
        private List<LineProductModel> linePoducts;

        private BigDecimal total;
        private LocalDateTime orderedAt;
        @ManyToOne(fetch = FetchType.EAGER)
        @JoinColumn(name = "fk_user")
        private UserModel user;

        public OrderModel(List<LineProductModel> lineProducts,
                          BigDecimal total,
                          LocalDateTime orderedAt,
                          UserModel user) {
           this.linePoducts = lineProducts;
            this.total = total;
            this.orderedAt = orderedAt;
            this.user = user;
        }
    }

and this is the json i get while performing a @GetMapping:
{
  "id": 0,
  "linePoducts": [
    {
      "id": 0,
      "quantity": 2,
      "lineProductPrice": 6,
      "product": {
        "id": 0,
        "eanCode": "1111111111111",
        "name": "pipo",
        "price": 3,
        "quantity": 8,
        "weight": 10,
        "description": "test",
        "category": {
          "id": 0,
          "name": "cibo"
        }
      }
    },
    {
      "id": 1,
      "quantity": 7,
      "lineProductPrice": 21,
      "product": {
        "id": 1,
        "eanCode": "2111111111111",
        "name": "pipobevanda",
        "price": 3,
        "quantity": 3,
        "weight": 10,
        "description": "test",
        "category": {
          "id": 1,
          "name": "bevanda"
        }
      }
    },
    {
      "id": 2,
      "quantity": 1,
      "lineProductPrice": 3,
      "product": {
        "id": 2,
        "eanCode": "3111111111111",
        "name": "pipoverdura",
        "price": 3,
        "quantity": 9,
        "weight": 10,
        "description": "test",
        "category": {
          "id": 2,
          "name": "verdura"
        }
      }
    }
  ],
  "total": 0,
  "orderedAt": "2022-08-10T20:48:15",
  "user": {
    "id": 0,
    "firstName": "-------",
    "lastName": "------",
    "email": "--------",
    "password": "------",
    "countryOfResidence": "italy",
    "birthdate": "-------",
    "balance": 100
  }
}

我必须对显示的信息进行一些更改,但是否有更好的解决方案或此解决方案可以?

相关问题