`

[hibernate]where clause is ambiguous 解决办法

阅读更多
使用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前面就可以
分享到:
评论

相关推荐

    Hibernate几个常见错误解决方式

    错误信息:`Column 'df' in where clause is ambiguous` 这个错误表示在SQL查询中的`df`字段无法明确它属于哪个表。在联接查询中,当多个表都有相同的字段名时,需要明确指定表名来避免歧义。例如,应将`df&lt;&gt;1`...

    Dynamic-WHERE-Clause

    在SQL编程中,"Dynamic-WHERE-Clause"是一个关键概念,特别是在处理复杂的查询需求时。它涉及到动态构建SQL语句的WHERE子句,使得查询条件可以根据不同的业务需求灵活变化。这通常在我们无法预知所有可能的查询条件...

    MySQL异常:[Err] 1055 – Expression #1 of ORDER BY clause is not in GROUP BY clause and contains nona…

    在MySQL中,错误提示 "[Err] 1055 – Expression #1 of ORDER BY clause is not in GROUP BY clause and contains nonaggregated column…” 指的是在执行包含`GROUP BY`子句的查询时,你试图对未在`GROUP BY`子句中...

    Clause_37_Auto-Negotiation.pdf

    《Clause 37 自协商原理详解》 在现代网络通信技术中,自协商(Auto-Negotiation)是一项关键功能,特别是在千兆以太网(Gigabit Ethernet, GE)环境中。Clause 37 自协商机制是IEEE 802.3标准的一部分,它允许网络...

    MySQL5.7中的sql_mode默认值带来的坑及解决方法

    在正常项目开发过程中,如果MySQL版本从5.6升级到5.7版本。作为DBA在考虑数据库版本升级带来的影响时,一般会有几个注意点...ERROR 1055 (42000): Expression #3 of XXXXXX list is not in GROUP BY clause and contain

    mysql中You can’t specify target table for update in FROM clause错误解决方法.docx

    ### MySQL中“You can’t specify target table for update in FROM clause”错误解决方法 在MySQL数据库管理过程中,遇到SQL语法错误是家常便饭,其中一种较为常见的错误是“You can’t specify target table for ...

    unidac 7.4.11 pro for RAD Studio 10.3

    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 ...

    Devart UniDAC 7.4.11 Professional for RAD Studio 10.3 Rio

    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 ...

    UnidacPro8.0.1Rio.rar

    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 ...

    UnidacProv8.0.1Tokyo.rar

    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 ...

    Hibernate Reference Documentation3.1

    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库 | clause-1.1.2.tar.gz

    资源分类:Python库 所属语言:Python 资源全名:clause-1.1.2.tar.gz 资源来源:官方 安装方法:https://lanzao.blog.csdn.net/article/details/101784059

    1000BASE-X IEEE 802.3-2008 Clause 36 - Physical Coding Sublayer (PCS)

    本文将深入探讨1000BASE-X和IEEE 802.3-2008 Clause 36中关于PCS的详细内容。 首先,我们要理解1000BASE-X的含义。"1000"代表1 Gbps的速率,"BASE"表示基带传输,"X"则意味着该标准采用了光纤或铜线介质的多种实现...

    struts2+HIbernate 多对一关系 及部分删除

    同时,`@Where(clause = "isDeleted = false")`注解确保我们在查询时只返回未被删除的学生。 在Struts2中,我们可以创建一个Action类来处理用户的请求,如删除操作。Action类会调用服务层的方法,服务层方法再...

    Clause_by_Clause_Explanation_of_ISO_13485_2016_EN.pdf

    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错误解决方法

    在MySQL数据库中,"You can’t specify target table for update in FROM clause"是一个常见的错误提示,意味着在同一个SQL语句中,你不能直接在一个`UPDATE`或`DELETE`语句的`FROM`子句中引用你想要更新或删除的表...

    delphi编译错误.txt

    - **解决办法**: 移除分号或将条件语句结构调整正确。 2. **`&lt;clause&gt;` clause not allowed in OLE automation section** - **含义**: 在 OLE 自动化部分中不允许出现 `&lt;clause&gt;` 子句。 - **解决办法**: 移除...

    mysql中错误:1093-You can’t specify target table for update in FROM clause的解决方法

    在MySQL中,错误1093 - "You can't specify target table for update in FROM clause" 是一个常见的问题,它发生在尝试在`UPDATE`语句的`FROM`子句中直接引用要更新的同一张表时。这个错误表示MySQL不支持在`UPDATE`...

    MySQL5.7更改密码时出现ERROR 1054 (42S22)的解决方法

    新安装的MySQL5.7,登录时提示密码错误,安装的时候并没有更改密码,后来通过免密码登录的方式更改密码,输入update mysql.user set password=password(‘root’) where user=’root’时提示ERROR 1054 (42S22): ...

Global site tag (gtag.js) - Google Analytics