2009-05-21 46 views
0

我有以下映射:Hibernate基于相关实体属性是否为空来查询实体?

<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> 

使用这种映射,customer.specialProperty为空时,有在special_properties表中的特定客户没有条目。(使用常规的一对一映射结果在持有代理对象的specialProperty中,所以我无法测试null)因此,在代码中,我可以简单地执行customer.specialProperty == null以查看Customer是否具有SpecialProperty。

我试图编写一个查询,它将返回所有拥有非空SpecialProperty的客户,以及另一个将返回所有拥有null SpecialProperty的客户的查询。

我能得到谁拥有非空SpecialProperty像这样的客户:

from Customer customer inner join customer.specialProperty 

但是,我不能让没有SpecialProperty(如customer.specialProperty == NULL)

谁的客户

我试过几件事。基本上我想要的是像

from Customer customer where customer.specialProperty is null 

但这生成SQL,对于customer.id测试无论出于何种原因被空。

对此提出建议?

回答

0

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

+0

好吧,这就解释了为什么它在生成的查询中检查customer.id。然而,该列=“id”是必要的,因为SpecialProperty实际上是由该表的主键表示的。 (SpecialProperty的主键是对客户PK的参考) – Boden 2009-05-22 03:26:16