HQL中引用空引用对象的引用属性

时间:2017-03-06 16:57:24

标签: java hibernate hql criteriaquery

这里我有两个Entity课程。

@Table(name = "AC_ACCOUNT_MASTER")
public abstract class Account implements Serializable {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    @Column(name = "a_name")
    private String name;
}

@Table(name = "AC_VOUCHER_MASTER")
public class Voucher implements Serializable {

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

    private Double amt;

    @ManyToOne
    private Account drAc;

    @ManyToOne
    private Account crAc;

}

AC_VOUCHER_MASTER表格中有10行,其中4 drAc个数据在null表格中为AC_VOUCHER_MASTER

session.createQuery("select v.id, v.amount, v.drAc.id,  v.crAc.id  from Voucher v").list();

以上查询返回10个结果(尽管4 drAcnull)。但是当我把名称引用(v.drAc.name)时,它只返回drAc不是null的那些行(6行)。

"select v.id, v.amount, v.drAc.id, v.drAc.name, v.crAc.id , v.crAc.name  from Voucher v"

我现在该怎么办?是否可以使用coalesce()或其他内容?

1 个答案:

答案 0 :(得分:1)

使用左连接:

select v.id, v.amount, drAc.id, drAc.name, crAc.id , crAc.name 
from Voucher v
left join v.drAc drAc
left join v.crAc crAc