hibernate 的问题啊

用DetachedCriteria类 组装查询条件,咋个生成这样的sql语句了捏? 如果没没条件的话还行 ,一加上where条件查询,就会出 where y1_.... 然后就报错:列名 'y1_' 无效。上一段打印出的sql

[code="java"]
select top 14 this_.id as y0_, this_.CODE as y1_, this_.PROJECT_NAME as y2_, this_.PROJECT_STATE as y3_, this_.PROJECT_CLASS as y4_, this_.CUSTOMER_ID as y5_, this_.CUSTOMER_DEPARTMENT_MAIN as y6_, this_.CUSTOMER_DEPARTMENT_HELP as y7_, this_.CUSTOMER_LINKMAN as y8_, this_.CUSTOMER_LINKMAN_PHONE as y9_, this_.CUSTOMER_CODE as y10_, this_.UNIT_PLAN as y11_, this_.UNIT_PLAN_LINKMAN as y12_, this_.UNIT_PLAN_LINKMAN_PHONE as y13_, this_.SUPERVISOR as y14_, this_.SUPERVISOR_LINKMAN as y15_, this_.SUPERVISOR_LINKMAN_PHONE as y16_, this_.PROJECT_CUSTODIAN as y17_, this_.PROJECT_DEPARTMENT as y18_, this_.PROJECT_DEPARTMENT_CUSTODIAN as y19_, this_.PROJECT_MANAGER as y20_, this_.SAFETY_OFFICER as y21_, this_.QUALITY_INSPECTOR as y22_, this_.LIBRARIAN as y23_, this_.BULIDERS as y24_, this_.MATERIALMAN as y25_, this_.TECHONLOGY as y26_, this_.FILE_TRANSFER as y27_, this_.BUILD_STATE as y28_, this_.COMPANYMANAGE_ID as y29_, this_.PROJECT_DATE as y30_, this_.USER_ID as y31_, this_.ACCESSORY_NAME as y32_ from project this_ where (y1_ like ? and y2_ like ?) order by y1_ desc
[/code]

5个回答

CODE列存在么?

zyn010101
zyn010101 把这一行注释掉试试detachedCriteria.setResultTransformer(Transformers.aliasToBean(Project.class));
6 年多之前 回复
znnevergiveup
znnevergiveup 嗯 不带where没问题的 带where条件 where条件就直接跟别名了 不跟数据库的列名了 不知道是不是我哪里设置不对么? 上段代码看看 DetachedCriteria detachedCriteria = DetachedCriteria.forClass(Project.class); detachedCriteria.add(Restrictions.ne(this.field2, this.param2)); detachedCriteria.setProjection(getProjectionList()); detachedCriteria.setResultTransformer(Transformers.aliasToBean(Project.class)); super.getHibernateTemplate().findByCriteria(detachedCriteria);
6 年多之前 回复
zyn010101
zyn010101 不带where条件生成的sql在数据库查询工具中能执行么?
6 年多之前 回复
znnevergiveup
znnevergiveup 首先:谢谢你的回答 ! CODE这个列是存在的 就是不知为何 在组装完转换成sql语句的时候 后面的列名都用别名来替代了 然后就报错 提示 "y1_"列不存在
6 年多之前 回复

把SQL放在sql里运行下,看报什么错,根据信息修改下。

ttyds
卧石听泉 它打印出来了,你复制下SQL,执行一下,会报错吗?
6 年多之前 回复
znnevergiveup
znnevergiveup sql语句是hibernate根据DetachedCriteria类组装生成的 生成完where条件里就用的别名
6 年多之前 回复

where语句中不能使用select中的别名呀

znnevergiveup
znnevergiveup 对呀 问题就在这里啊 可是这个sql语句是 hibernate自动生成的 怎么回事?
6 年多之前 回复

where语句中不能使用select中的别名呀 +1

仔细检查一下你hql 写的是否正确。转成sql 在数据库执行一下吧。
where中确实不能使用select 中定义的别名啊。

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!