hibernate的formula

Property元素中的formula允許對象屬性包含導出值,比如sum、average、max等的結果。如:

  1. <property name="averagePrice" formula="(select avg(pc.price) from PriceCatalogue pc, SelectedItems si where si.priceRefID=pc.priceID)"/>  

 

此外,formula還可以基於當前記錄的特定屬性值從另一個表檢索值。例如:

代碼
  1. <property name="currencyName" formula="(select cur.name from currency cur where cur.id= currencyID)"/>  

 

代碼
  1. <property name="schNum" formula="(select max(a.schoolNumb) from sys_act_code as a)"/>  

注意:
1,formula="()",裏面的是sql語句,字段和表名都應該和數據庫相應,而不是字段,若帶有參數如cur.id= currencyID,這個currencyID纔是對象的東東.
2,formula="( sql )",這個括號不能少,不然會報錯,我試了幾次,沒括號就報錯,添上就沒問題
3,操作字段一定要用別名

問題:
 1,org.springframework.orm.hibernate3.HibernateSystemException: Null value was assigned to a property of primitive type setter of
沒用別名,會出現這個錯誤,添個別名就好了

2,如果我要用obj.getSchNum()得到想要的值,該對象(obj)必須是hibernate取得的對象,

3,如果要傳入參數,如上面那個,currencyID是該對象的屬性,它的值也是有hibernate操作當前對象時,把該屬性對應的值自動傳入進去.

發佈了6 篇原創文章 · 獲贊 7 · 訪問量 9萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章