2012-08-10 73 views
0

這是我的hbm.xml文件結構。休眠組合鍵中的所有列 - 不返回POJO對象

<hibernate-mapping> 
<class name="customer" table="Customer" schema="Schem"> 
    <composite-id name="id" class="Customerid"> 
     <key-property name="code" type="java.lang.String"> 
      <column name="CODE" length="10" /> 
     </key-property> 
     <key-property name="name" type="java.lang.String"> 
      <column name="NAME" length="10" /> 
     </key-property> 
     <key-property name="address" type="java.lang.String"> 
      <column name="ADDRESS" length="100" /> 
     </key-property> 
     <key-property name="contactnumber" type="java.lang.String"> 
      <column name="CONTACTNUMBER" length="15" /> 
     </key-property> 
    </composite-id> 
</class> 
</hibernate-mapping> 

但在數據庫中,如果一行中的任何一列是空的。那麼hibernate不會返回任何Customer對象並返回null。我猜測是因爲所有的列都在複合標識符中,這就是爲什麼它返回空對象,如果該列中的任何一列是空的。 如何獲得休眠POJO對象客戶只有代碼值可用?

+0

我的意思是複合鍵具有不屬於主鍵的列。但我不知道如何通過僅使用'代碼'列來獲取客戶對象。 – Jignesh 2012-08-10 13:56:18

回答

0

一個ID /主鍵必須不爲空並且是唯一。只包括在compositeId映射中構成主鍵的列。要通過id獲得一個實例,請使用

Customer customer = session.get(Customer.class, new Customer(<id properties));