【问题标题】:HQL not working "in" with multiple valuesHQL 不能与多个值一起工作
【发布时间】:2014-10-28 07:05:28
【问题描述】:

我有类似这样的休眠模式。

String queryString = "from MFunction as mFunction where mFunction.functionKey in " 
            + "((select mRole.enterprise  from MRole as mRole where mRole.roleKey=:role), " 
            + "(select mRole2.project from MRole as mRole2 where mRole2.roleKey=:role ), " 
            + "(select mRole3.technology  from MRole  as mRole3 where mRole3.roleKey=:role ))";

但是休眠查询只接受第一个值。

Hibernate: select mfunction0_.FunctionKey as Function1_73_, mfunction0_.`Add` as Add2_73_, mfunction0_.`Audit` as Audit3_73_, mfunction0_.ClientKey as ClientKey73_, mfunction0_.CreatedBy as CreatedBy73_, mfunction0_.CreatedTs as CreatedTs73_, mfunction0_.`Delete` as Delete7_73_, mfunction0_.`Edit` as Edit8_73_, mfunction0_.`Financial` as Financial9_73_, mfunction0_.FunctionName as Functio10_73_, mfunction0_.`General` as General11_73_, mfunction0_.Level as Level73_, mfunction0_.LevelKey as LevelKey73_, mfunction0_.LogicalDeleteTms as Logical14_73_, mfunction0_.UpdatedBy as UpdatedBy73_, mfunction0_.UpdatedTs as UpdatedTs73_, mfunction0_.`View` as View17_73_ from appanalytixdb.M_Function mfunction0_ where mfunction0_.FunctionKey in (select mrole1_.Enterprise from appanalytixdb.M_Role mrole1_ where mrole1_.RoleKey=?)

休眠版本:4.1.8.Final

【问题讨论】:

  • 逗号在引号内。下面的查询将起作用。 String queryString = "from MFunction as mFunction where mFunction.functionKey in " + "((select mRole.enterprise from MRole as mRole where mRole.roleKey=:role) ", + "(select mRole2.project from MRole as mRole2 where mRole2. roleKey=:role ) ", + "(select mRole3.technology from MRole as mRole3 where mRole3.roleKey=:role ))";

标签: java hibernate


【解决方案1】:

您的问题是您创建了一个列表列表,您应该创建一个功能键列表。

我只看到 2 个选项:

  1. 将 1 个“in”更改为 3 个与 or 连接的语句。

String queryString = "from MFunction as mFunction where mFunction.functionKey in (select mRole.enterprise from MRole as mRole where mRole.roleKey=:role) or mFunction.functionKey in (select mRole2.project from MRole as mRole2 where mRole2. roleKey=:role ) 或 mFunction.functionKey in (select mRole3.technology from MRole as mRole3 where mRole3.roleKey=:role )";

  1. 最好的解决方案是预先选择所有项目、技术、企业并将它们联合在一起(以避免重复)。但是根据这篇帖子Hibernate Union alternatives,hibernate 还不支持联合。 如果您的表中没有太多条目,我将创建一个视图,并在其上使用我的查询。在这种情况下,视图可能非常便宜且易于使用。

【讨论】:

    猜你喜欢
    • 2018-06-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-06
    • 1970-01-01
    • 1970-01-01
    • 2022-08-09
    • 2015-08-21
    相关资源
    最近更新 更多