使用Criteria方式查询,如果使用like字段,当关联的表里面有重复字段,则会报 where clause is ambiguous 异常
例如
假设 ModelA 中有一个关联ModelB,且两个Model中都有同样的字段 name,如果对name使用like方式查询,如下
Session session = HibernateManager.getSession();
String str = "aaaaa";
Criteria cri = session.createCriteria(ModelA.class);
cri.add(Expression.or(Expression.sql("name like '%"+str+"%'"), Expression.sql("address like '%"+str+"%'")));
return cri.list();
则会报: ERROR JDBCExceptionReporter.java Column 'name' in where clause is ambiguous
解决办法:
在 name前加上'this_.'就可以了,告诉mysql你使用 ModelA所对应的表
cri.add(Expression.or(Expression.sql("this_.name like '%"+str+"%'"), Expression.sql("this_.address like '%"+str+"%'")));
注:如果你想使用 ModelB所对应的表,把配置文件中的show_sql打开
<property name="show_sql">true</property>
看看ModelB 所使用的别名,填到name前面就可以
分享到:
相关推荐
[Err] 1055 – Expression #1 of ORDER BY clause is not in GROUP BY clause and contains nonaggregated column ‘information_schema.PROFILING.SEQ’ which is not functionally dependent on columns in GROUP ...
在静态SQL中实现动态WHERE子句 博文中 的demo 案例
Clause37 GE自协商原理,自协商状态图详细讲解; • Location in the OSI Stack • Interface to the PCS • Auto-Negotiation functionality
Bug with SQL statements containing a CONTAINS predicate in the WHERE clause is fixed Bug with application freezing when executing a stored procedure is fixed Bug with editing the ConnectString ...
Bug with SQL statements containing a CONTAINS predicate in the WHERE clause is fixed Bug with application freezing when executing a stored procedure is fixed Bug with editing the ConnectString ...
time editor is fixed Bug with XML field types when clearing data is fixed Bug with setting "MSOLEDBSQL.1" value for the TUniConnection.ConnectString property is fixed MySQL data provider Support for ...
time editor is fixed Bug with XML field types when clearing data is fixed Bug with setting "MSOLEDBSQL.1" value for the TUniConnection.ConnectString property is fixed MySQL data provider Support for ...
1000BASE-X IEEE 802.3-2008 Clause 36 - Physical Coding Sublayer (PCS)_latest.tar.gz
14.8. The where clause 14.9. Expressions 14.10. The order by clause 14.11. The group by clause 14.12. Subqueries 14.13. HQL examples 14.14. Bulk update and delete 14.15. Tips & Tricks 15. Criteria ...
资源分类:Python库 所属语言:Python 资源全名:clause-1.1.2.tar.gz 资源来源:官方 安装方法:https://lanzao.blog.csdn.net/article/details/101784059
新安装的MySQL5.7,登录时提示密码错误,安装的时候并没有更改密码,后来通过免密码登录的方式更改密码,输入update mysql.user set password=password(‘root’) where user=’root’时提示ERROR 1054 (42S22): ...
mysql遇见Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggre的问题
ISO (the International Organization for Standardization) is a worldwide federation of national standards bodies (ISO member bodies). The work of preparing International Standards is normally carried ...
主要介绍了mysql中You can’t specify target table for update in FROM clause错误解决方法,需要的朋友可以参考下
使用 Hibernate Criteria && DetachedCriteria Queries演示Exists Clause非常简单的项目 安装 git clone https://github.com/RameshRM/hibernate-sample.git 跑步 mvn install 这是一个maven项目,依赖项是 ...
主要介绍了解决大于5.7版本mysql的分组报错Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated问题,需要的朋友可以参考下
在正常项目开发过程中,如果MySQL版本从5.6升级到5.7版本。作为DBA在考虑数据库版本升级带来的影响时,一般会有几个注意点...ERROR 1055 (42000): Expression #3 of XXXXXX list is not in GROUP BY clause and contain
Bug with complex WHERE clauses that have several string fields in TVirtualQuery is fixed Bug with selecting from a dataset with a single record in TVirtualQuery is fixed Bug with generating SQL for ...
Bug with complex WHERE clauses that have several string fields in TVirtualQuery is fixed Bug with selecting from a dataset with a single record in TVirtualQuery is fixed Bug with generating SQL for ...