Hibernate 中中的formula属性


 

Property元素中的formula容许对象属性包含导出值,好比sum、average、max等的结果。如:java

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

此外,formula还能够基于当前记录的特定属性值从另外一个表检索值。例如:spring

 

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

代码数据库

<property name="notIncomeFee" type="java.lang.Double" formula="(case when fee_authorize is null then 0 else fee_authorize end)-(select (case when sum(i.fee_sum) is null then 0 else sum(i.fee_sum) end) from S_PROJECT_INCOME i where i.project_id=id and i.CHECK_STATUS_ID=2)">spa

  1. 注意:
    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操做当前对象时,把该属性对应的值自动传入进去.hibernate

相关文章
相关标签/搜索