java—用于在spring中从多个表中获取数据的自定义查询

vuktfyat  于 2021-06-30  发布在  Java
关注(0)|答案(2)|浏览(377)

实体:

@Entity(name = "ap_order_paid")
@Component
public class ApOrderPaid {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    private Long order_number;

    @Transient
    private Long invoiceNumber;
    private Long cheqe_number;

    //getter setter
}

public class Orders {
    private long order_number;
    private String invoice_number;
}

我在mysql编辑器中运行下面的查询,它正在工作

select
   v.order_number,
   v.id,
   o.invoice_number as invoiceNumber
from ap_order_paid as v, orders o
where v.cheqe_number like '%1234%' and v.order_number = o.order_number

但是当我在Spring和 @Query 不提供发票号码数据的注解

@Query(value="select v.*, o.invoice_number from ap_order_paid as v, orders o where v.cheqe_number like %?1% and v.order_number = o.order_number", nativeQuery = true)
List<ApOrderPaid> getOrderTaskPaidByCheqNo(String cheqe_number);
7ivaypg9

7ivaypg91#

您没有在任何列上提到任何类型的@column注解。您正在尝试获取“invoicenumber”,这是一个“临时”字段。使用适当的注解,以便hibernate能够用实体对象Mapdb列。

9gm1akwq

9gm1akwq2#

将关联Map为 @ManyToOne 这样地:

@Entity(name = "ap_order_paid")
public class ApOrderPaid {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    @ManyToOne(fetch = LAZY)
    @JoinColumn(name = "order_number")
    private Orders order;

    private Long cheqe_number;

    //getter setter
}

@Entity
public class Orders {
    @Id
    private long order_number;
    private String invoice_number;
}

使用hql/jpql查询,如下所示:

@Query("select v from ap_order_paid as v join fetch v.order where v.cheqe_number like %?1%")
List<ApOrderPaid> getOrderTaskPaidByCheqNo(String cheqe_number);

相关问题