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

Reference properties of null referenced object in HQL

这里我有两个Entityclass.

@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_MASTERtable中有10行,其中AC_VOUCHER_MASTERtable中有4drAc条数据null

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

上面查询returns我10个结果(虽然4个drAc都是null)。但是当我输入名称引用 (v.drAc.name) 时,它 returns 只有 drAc 不是 null.

的那些行(6 行)
"select v.id, v.amount, v.drAc.id, v.drAc.name, v.crAc.id , v.crAc.name  from Voucher v"

我现在应该做什么?有没有使用 coalesce() 或其他东西?

使用左连接:

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