【问题标题】:JPA Left joining multiple OneToMany entitiesJPA Left 加入多个 OneToMany 实体
【发布时间】:2016-05-30 02:17:44
【问题描述】:

您好,我正在尝试为我工作的公司制作订单跟踪系统。

目前我需要在启动时从我的 mySQL 数据库加载 PO 实体,这些 PO 实体的发票为空或非“已交付”状态(枚举)。

我的实体如下

@Entity
public class PO {

@Id
@GeneratedValue
private Long id;

@Column(name = "Customer", nullable = false)    
private String customer;

@Column(name = "PO_number", nullable = false)
private String PONUMBER;

@OneToMany(mappedBy = "po",cascade = CascadeType.PERSIST,fetch = FetchType.EAGER)
private  List<CatogeryEntires> poCatogories = new ArrayList<>();

@Column(name = "entered_on", nullable = false)
private LocalDateTime entryDate;

@Column(name = "Delivery_Date", nullable = false)
private LocalDate dd;

@Transient
private ObjectProperty<LocalDate> deliveryDate;

@Column(name = "Deliver_Before", nullable = false)
private LocalTime dt;

@Transient
private ObjectProperty<LocalTime> deliveryTime;

@Entity
public class CatogeryEntires {

 @Id
    @GeneratedValue
    private Long id;

    @Enumerated(EnumType.STRING)
    private PO_CATOGERY poCatogery;

    @ManyToOne
    @JoinColumn(name = "po_id", nullable = false)
    private PO po;

    @OneToMany(mappedBy = "catogeryEntries", cascade = CascadeType.PERSIST)
    private Set<Invoice> invoices = new HashSet<>();

@Entity
public class Invoice {

@Id
@GeneratedValue
private Long id;

private int invoiceNumber;

@Column(name = "delivered_on", nullable = true)
private LocalDateTime deliveredOn;

@Enumerated(EnumType.STRING)
@Column(name = "status", nullable = true)
private InvoiceStatus invoiceStatus;

@ManyToOne
@JoinColumn(name = "catogery_entry_id")
private CatogeryEntires catogeryEntries;

我目前在我的数据库中使用以下本机 SQL 语句来获得所需的结果

SELECT po_app.po.id,Customer,PO_number,Delivery_Date,Deliver_Before ,poCatagery,invoiceNumber,状态

FROM(po_app.po left join po_app.catogeryentires on po.id = catogeryentires.po_id)

LEFT JOIN po_app.invoice on catogeryentires.id = invoice.catogery_entry_id

WHERE 状态为 null 或 status != 'DELIVERED';

我不知道通过 JPQL 复制它 我尝试阅读参考书,例如:Java persistence and hibernate 2nd editions”,但仍然没有运气。

【问题讨论】:

    标签: hibernate jpa


    【解决方案1】:

    我用过同一本书,我认为这段代码应该可以工作:

        EntityManagerFactory emf = Persistence.createEntityManagerFactory("Your PU Name Here");
        EntityManager em = emf.createEntityManager();
    
        try {
    
            String jpql = "SELECT p.id, p.customer, p.PONUMBER, p.deliveryDate, p.dt, c.poCatogery, i.invoiceNumber, i.status "
                        + "FROM PO p "
                        + "JOIN p.poCatogories  c "
                        + "JOIN c.invoices i WHERE i.status IS NULL or i.status != :status)";
    
            Query query = em.createQuery(jpql);
            query.setParameter("status", InvoiceStatus.DELIVERED);
            List<Object> resultList = query.getResultList();
    
            System.out.println(resultList.size() + " invoice(s) found:" );
    
            for (Object singleResult : resultList) {
                Object[] singleRow = (Object[]) singleResult;
                ...
            }
    
    
        } finally {
            em.close();
            emf.close();
        }
    

    您可以通过细微的更改对其进行测试。替换 EntityManagerFactory 上的 PU 名称,并可能修复查询中的一些可能的拼写错误。

    请注意,您将获得一个带有两个 INNER JOIN 的 SQL 查询。我一直在阅读有关该主题的一些文章和书籍,并且使用带有 where 子句的左连接提取无效。引用 Gavin King 等人的“Java Persistence with Hibernate”:“查询 left join fetch i.bids b where b.amount ... 无效。你不能说,“加载 Item 实例并初始化它们的投标集合,但只能使用具有一定数量”。

    希望这会有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-12-21
      • 2022-01-05
      • 1970-01-01
      • 2015-07-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多