基于相关实体属性是否为null,实体的Hibernate查询?

时间:2009-05-21 23:07:54

标签: hibernate hql

我有以下映射:

<class name="Customer">
  <!-- actually one-to-one for all intents and purposes-->
  <many-to-one name="specialProperty" class="SpecialProperty" cascade="all" not-found="ignore" insert="false" update="false" column="id" unique="true"/>
</class

<class name="SpecialProperty" lazy="false">
        <id name="id" column="customer_id">
            <generator class="foreign">
                <param name="property">customer</param>
            </generator>
        <one-to-one name="customer" class="Customer" constrained="true"></one-to-one>
</class>

使用此映射时,如果special_properties表中没有特定客户的条目,则customer.specialProperty为空。(使用常规的一对一映射会导致特殊属性持有代理对象,所以我无法测试null)所以在代码中我可以简单地做customer.specialProperty == null来查看Customer是否有SpecialProperty。

我正在尝试编写一个查询,该查询将返回所有具有非null SpecialProperty的客户,以及另一个将返回所有具有null SpecialProperty的客户的查询。

我可以获得具有非null SpecialProperty的客户:

from Customer customer inner join customer.specialProperty

但是,我无法获得没有SpecialProperty的客户(例如customer.specialProperty == null)

我尝试了一些事情。基本上我想要的是

from Customer customer where customer.specialProperty is null

但是这会生成测试customer.id的sql因为某种原因而为null。

建议?

1 个答案:

答案 0 :(得分:0)

你的specialProperty定义中有column =“id”