作者热门文章
- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
有没有办法在 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;
问候。
最佳答案
我找到了答案,可以使用 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();
关于java - 如何在 jpa 中创建两列左连接?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9015158/
我是一名优秀的程序员,十分优秀!