您的位置:首页 > 数据库

hibernate sql命名查询

2010-03-19 16:00 363 查看
最新在做一个医院管理系统,使用到了Hibernate,数据库使用的是mysql。

碰到一个需求,需要求2个日期相差天数,并且把天数显示出来,我想这个还是利用数据库原生的日期函数比较好,于是考虑使用Hibernate对sql的支持,由于sql写在程序中不太好,如果要换数据库,找sql语句来修改都是个麻烦事情,把所有原生sql写在配置文件里面比较合理。

配置文件如下:

<sql-query name="patientNotice">

<return alias="p" class="com.realnetworks.bms.po.Patient">
<return-property name="id" column="id1"/>
<return-property name="name" column="name1"/>
<return-property name="address" column="address1"/>
<return-property name="birthday" column="birthday1"/>
<return-property name="sex" column="sex1"/>
<return-property name="days" column="days1"/>
</return>

<![CDATA[
select
pp.id as id2,pp.name as name1,pp.address as address1,pp.birthday as birthday1,pp.sex as sex1,
(to_days(CONCAT( 2010,birthday)) - to_days(curdate())) as days1
from
t_patient pp
WHERE
( to_days(CONCAT( 2010,birthday)) - to_days(curdate()) ) >= 0
AND
( to_days(CONCAT( 2010,birthday)) - to_days(curdate()) ) <6

]]>

</sql-query>

数据库本来有id,name,address,birthday,sex 这几个字段,而days 只有在com.realnetworks.bms.po.Patient里面存在,与数据库的字段没有对应,结果执行程序,结果总是days字段总是不能被赋值,而把 <return-property name="days" column="days1"/>
的days1改成 id1,发现id1能够被查询结果赋值。

id1和days1有什么区别呢,我想了很久,难道是因为一个是与数据库字段对应,一个没有

于是我把Patient.hbm.xml中加上 <property name="days" type="java.lang.Integer"> </property>, 不与任何一个数据库字段对应,再执行程序,发现能够正确赋值了。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: