2012-01-19 9 views
0

私は1対1マッピングを含むオブジェクトを持っています。null値にかかわらずすべての行を返すselect文をHQLに持つ方法

Customer.hbm.xml:

<class name="com.asi.shared.Customer" table="Customer"> 
    <id name="id" column="CustomerID"> 
     <generator class="assigned"/> 
    </id> 
    <property name="customerName" column="CustomerName" /> 
    ... 
    <one-to-one name="corp" class="com.asi.shared.Corp"/> 
</class> 

Corp.hbm.xml

<class name="com.asi.shared.Corp" table="Corp"> 
    <id name="id" column="CustomerID"> 
     <generator class="assigned"/> 
    </id> 
    <property name="customerName" column="CustomerName" /> 
    <property name="storeNumber" column="StoreNumber"/> 
    <property name="corpCustomerId" column="CorpCustomerId"/> 
</class> 

私は次のクエリを実行し、テーブルを返す貸衣装のすべての行を持っていると思います。

select customerName, id, support, corp.corpCustomerId 
    from com.asi.shared.Customer ORDER BY customerName 

ただし、corpテーブルに何かがある行だけを返します。すべての顧客がCORPテーブルに何かを持っているわけではありません。私はcorp.corpCustomerId = nullを持っても構いません。

私は間違っていますか?

答えて

1

customer.corp.corpCustomerIdを選択すると、暗黙的に両方のテーブル間で内部結合が行われますが、必要なのは左結合です。であるとして、あなたのクエリを書き換え:

select customer.customerName, customer.id, corp.corpCustomerId 
from Customer customer 
left join customer.corp corp 
order by customer.customerName 

をサイドノート:あなたはプロパティ名に、これらすべてのredendanciesを持っていなかった場合、あなたのコードが読みやすくなります。 customerNameプロパティを呼び出す必要はありません。これはCustomerクラスのプロパティなので、customer.nameで十分です。 corp.corpCustomerIdと同じですが、これはcorp.customerIdと読みやすくなります。

+0

ありがとう、トリックでした! – Angel

関連する問題