首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 其他教程 > 开源软件 >

hibernate查询,多条数据同样

2012-09-27 
hibernate查询,多条数据一样用hibernate查询一个表的数据,结果条数正确,但是所有记录均为最后一条记录的数

hibernate查询,多条数据一样

用hibernate查询一个表的数据,结果条数正确,但是所有记录均为最后一条记录的数据。

?

数据库表和数据如下:

?

dm????? xh????????? mc

0001?? 01????????? 张三

0001?? 02??????????李四

0001?? 03????????? 李雷

?

有实体

User.java

public class User {       private String dm;       private String xh;       private String mc;       public String getDm() {           return dm;       }       public void setDm(String dm) {             this.dm = dm;       }       public String getXh() {              return xh;        }        public void setXh(String xh) {              this.xh = xh;        }        public String getMc() {               return mc;          }        public void setMc(String mc) {              this.mc = mc;        }}

?

hibernate映射文件:

<class name="User" table="user" schema="user" lazy="false">        <id column="dm" length="6" name="xh" type="string"><generator /></id><property column="xh" length="2" name="xh" type="string" />        <property column="mc" length="20" name="xh" type="string" />                 </class>

?

程序里的hql语句为? from User where dm = '0001'

结果为:

dm????? xh????????? mc

0001?? 03????????? 李雷

0001?? 03????????? 李雷

0001?? 03????????? 李雷

?

最后在网上查询相关信息得知,是因为主键设置不正确,以dm为主键不能唯一标识一条记录,这样hibernate在检索时即认为该条数据已经存在缓存中,只会更新上一条记录,不会作为不同的记录返回。

最后修改hibernate映射问件,以dm和xh作为联合主键后,查询结果正确。

热点排行