如何在jpa中创建两列左连接?

时间:2012-01-26 07:46:48

标签: java hibernate jpa

有没有办法在jpa中使用左连接在两列上进行查询,如下面的普通sql?

select
    combinedin1_.SHIPMENTNUMBER ,
    shipmentin3_.INBOUNDDELIVERYITEM ,
    combinedin1_.PURCHASEORDERNUMBER,
    combinedin1_.PURCHASEORDERITEMNUMBER ,
    purchaseor2_.FACTORY 
from
    REPORT.REPORTLINK this_
left outer join
    REPORT.COMBINEDINFO combinedin1_
        on this_.COMBINEDINFOID=combinedin1_.COMBINEDINFOID
left outer join
    REPORT.PURCHASEORDERINFO purchaseor2_
        on this_.PURCHASEORDERINFOID=purchaseor2_.PURCHASEORDERINFOID and purchaseor2_.DELETED='N'
left outer join
    REPORT.SHIPMENTINFO shipmentin3_
        on this_.SHIPMENTINFOID=shipmentin3_.SHIPMENTINFOID and shipmentin3_.DELETED='N'

更具体地说,这是用纯SQL编写的左连接:

left outer join
    REPORT.SHIPMENTINFO shipmentin3_
        on this_.SHIPMENTINFOID=shipmentin3_.SHIPMENTINFOID and shipmentin3_.DELETED='N'

这是我在JPA中的reportLink实体:

@Entity
@Table(name = "REPORTLINK")
public class ReportLinkFull implements Serializable {

private static final long serialVersionUID = -2330676941570524461L;

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "REPORTLINKID")
private Integer id;

@ManyToOne
@JoinColumn(name = "SALESORDERINFOID")
private SalesOrderInfo salesOrderInfo;

@ManyToOne
@JoinColumn(name = "PURCHASEORDERINFOID")
private PurchaseOrderInfo purchaseOrderInfo;

@ManyToOne
@JoinColumn(name = "SHIPMENTINFOID")
private ShipmentInfo shipmentInfo;

@ManyToOne
@JoinColumn(name = "MATERIALINFOID")
private MaterialInfo materialInfo;

@ManyToOne
@JoinColumn(name = "CUSTOMERINFOID")
private CustomerInfo customerInfo;

@ManyToOne(cascade={CascadeType.MERGE, CascadeType.PERSIST, CascadeType.REMOVE})
@JoinColumn(name = "COMBINEDINFOID")
private CombinedInfo combinedInfo;

@Version
private Integer version;

问候。

1 个答案:

答案 0 :(得分:6)

我找到了答案,可以使用WITH运算符:

em.createQuery("SELECT rl FROM ReportLinkFull rl LEFT OUTER JOIN rl.shipmentInfo sh WITH sh.deleted = 'N' LEFT OUTER JOIN rl.purchaseOrderInfo po WITH po.deleted = 'N'").getResultList();