一个很郁闷的Hibenate Query问题,多表查询时,HQL 采用占位符访问不到数据,也不报错,返回空数组,用HQL拼接的方式能够读取到目标数据!请博客里的高人留迹为小生解惑,非常感谢。
在dao层访问的关键封装为(采用占位符):
String hql = "from UploadSheet obj where 1=1 and obj.uploadType=?"; List values = new ArrayList(); values.add(type); if(user.getId() != null){ hql = hql + " and obj.uploader.id=?"; //多表隐式查询HQL,也是访问不到数据代码段 values.add(user.getId()); } return this.listByParamQuery(hql, values.toArray(), pageNum, pageSize, "obj.uploadDate", false);
Hibernate 配置采用配置文件的方式 UploadSheet.hbm.xml (部份无相关的字段映射已省略)文件代码:
<hibernate-mapping> <class name="com.botwave.sss.business.entity.UploadSheet" table="t_edu_uploadSheet" lazy="true" optimistic-lock="version" > <id name="id" column="id" type="java.lang.String" length="32" unsaved-value="null" > <generator class="uuid.hex"> </generator> </id> <!--关键映射代码段--> <many-to-one name="uploader" class="com.botwave.core.business.entity.User" cascade="none" outer-join="auto" column="uploader" /> <property name="status" type="java.lang.Integer" update="true" insert="true" column="status" /> <property name="uploadDate" type="java.util.Date" column="uploadDate" /> <property name="errorRows" type="java.lang.Integer" update="true" insert="true" column="errorRows" /> <property name="uploadType" type="java.lang.Integer" update="true" insert="true" column="uploadType" /> </class> </hibernate-mapping>
User.hbm.xml 配置文件:
<hibernate-mapping> <class name="com.botwave.core.business.entity.User" table="t_edu_user" lazy="true" optimistic-lock="version" > <id name="id" column="id" type="java.lang.String" length="32" unsaved-value="null" > <generator class="uuid.hex"> </generator> </id> <property name="description" type="java.lang.String" update="true" insert="true" column="description" length="100" /> <property name="email" type="java.lang.String" update="true" insert="true" column="email" length="20" /> <property name="fullName" type="java.lang.String" update="true" insert="true" column="fullName" length="32" not-null="true" /> <property name="password" type="java.lang.String" update="true" insert="true" column="password" /> </class> </hibernate-mapping>