天天看点

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

 代码