Hibernate HQL:与OnetoOne一起加入

时间:2014-01-29 20:44:07

标签: java hibernate hql

我有以下实体销售产品:

@Id 
@SequenceGenerator(name = "Product_seq", sequenceName = "PRODUCT_SEQ", allocationSize = 1)
@GeneratedValue(strategy=GenerationType.SEQUENCE, generator = "Product_seq")
@Column(name = "ID")
private Long id;

@OneToOne
@JoinColumn(name = "CAS_ID")
private CAS cas;
...

CAS实体:

@Id
@SequenceGenerator(name = "CAS_seq", sequenceName = "CAS_SEQ", allocationSize = 1)
@GeneratedValue(strategy=GenerationType.SEQUENCE, generator = "CAS_seq")
@Column(name = "ID", unique = true, nullable = false, scale = 0)
private Long id;

@Column(name = "CAS_NUMBER", length = 6)
private String casNumber;

@Column(name = "CAS_DESCRIPTION")
private String casDescription;
...

在我的应用程序中,我有以下HQL语句:

select p from Product p where p.approve IS NULL order by p.cas.casNumber desc

这里的问题是p.cas.casNumber可以为NULL,我也需要返回这些行。我不知道如何使用p.cas.casNumber在此表上编写LEFT JOIN。

感谢您提供的任何帮助。谢谢!

1 个答案:

答案 0 :(得分:2)

你可以尝试如下:

select p from Product p left join p.cas as a where p.approve IS NULL order by a.casNumber desc