hibernate在我们的项目中确实提供了很大的方便,提高了项目的开发进度,它的缓存机制也提高了我们的查询效率;但它在一定程度上也降低了程序的运行性能,在进行复杂的查询时,为了提高查询效率,我们还会不知觉的去使用sql进行查询,使用hibernate的native sql时,当我们的查询过于复杂时,将会出现各种各样的问题。
最近做一个项目时就遇到了一个很头疼的问题,报的是s0022错误,column “name” not found。一开始我认为这是因为查询的sql语句中有重复的名字引起的,但添加了别名后,仍是报这个错误,经过我不懈的努力终于解决了这个问题。
有两张表:
A 字段 id,name,sex,typeId ,B 字段 id,typeName
我的sql语句写在hbm.xml文件中:
未修改前的语句:
<sql-query>
select a.name as stuName,a.sex as sex,b.id as stuId,b.type as type from A as a inner join B b on a.typeId=b.id
</sql-query>
修改之后的语句:
<sql-query>
<return-type name="stuName" type="int"/>
<return-type name="sex" type="string"/>
<return-type name="stuId" type="int"/>
<return-type name="type" type="string"/>
select a.name as stuName,a.sex as sex,a.id as stuId,b.type as typeName from A as a inner join B b on a.typeId=b.id
</sql-query>
这样问题就解决了:因为hibernate会在ResultSetMetadata类中判断数据库中实际查询出来的数据的顺序和类型,如果类型不一致,就会报s0022 column "name" not found 的错误。
1168

被折叠的 条评论
为什么被折叠?



