【问题标题】:How to use group_concat in hibernate criteria?如何在休眠条件中使用 group_concat?
【发布时间】:2014-11-30 08:19:51
【问题描述】:

我在 mysql 中使用 group_concat 写了一个查询

SELECT c1,group_concat(c2) FROM table1 where sno in(1,4,8,10) group by c1;

并给出我预期的结果。

现在我想使用休眠条件编写相同的查询。

【问题讨论】:

标签: sql hibernate


【解决方案1】:

请参考以下代码sn-ps

Criteria cr = session.createCriteria(table1.class);
cr.add(Restrictions.in("sno",snoarray));
criteria.setProjection("c1");
criteria.setProjection(Projections.groupProperty("c1"));

【讨论】:

  • 这个标准对象是怎么来的。我认为它是您创建的 cr ,在这种情况下为 criteria.setProjection("c1");我认为这是错误的(我们必须像 cr.setProjection(Projections.projectionList().add(Projections.property("c1"))); 即使在这种情况下,如果我添加 Projections.groupProperty("c1" ); 它只会给一个组而不是多个记录。所以我需要多个记录。
【解决方案2】:

简单的答案是

为什么?

Hibernate 仅支持在多个数据库中使用的常用函数/语法。 Microsoft SQL Server 中没有任何group_concat 函数,并且可能在其他数据库中也是如此。

解决方案:

您必须将其作为简单 SQL 查询来执行。

【讨论】:

  • 我搜索了很多文档,但没有找到任何解决方案,所以最后我在 hibernate 中使用 createSQLQuery(query) 并得到了我的预期结果。
  • 是的。你别无选择... ;)
  • 这个功能还不支持吗?除了使用 sql 查询之外,还有什么解决方法吗?
  • 这个答案具有误导性,可能是因为它已经过时了。请查看stackoverflow.com/questions/4955580/…
【解决方案3】:

最后我通过下面的代码得到了预期的结果

String query="select c1,group_concat(c2) from table1 where sno in (:pageIds) group by c1";

SQLQuery sqlQuery= session.createSQLQuery(query);

sqlQuery.setParameterList("pageIds", myList);

列表列表= sqlQuery.list();

c1 group_concat(c2)

aaa 值 1,值 2 bbb值3 ccc value4,value5,value6

【讨论】:

    【解决方案4】:

    您有两个选择(取决于您的休眠版本)。

    覆盖方言类 任何休眠版本

    您需要子类化您的方言以添加group_concat()

    1. 引入方言覆盖类

    在您的应用中的某处创建以下类(例如 util 包)

    package com.myapp.util;
    
    import org.hibernate.dialect.MySQL5Dialect;
    import org.hibernate.dialect.function.StandardSQLFunction;
    import org.hibernate.type.StandardBasicTypes;
    
    public class MySQLCustomDialect extends MySQL5Dialect {
        public MySQLCustomDialect() {
            super();
            registerFunction("group_concat", 
                new StandardSQLFunction("group_concat", 
                    StandardBasicTypes.STRING));
        }
    }
    
    1. 将方言覆盖类映射到启动属性

    将以下属性添加到您的 application.properities

    spring.jpa.properties.hibernate.dialect = com.myapp.util.MySQLCustomDialect

    使用 JPA 元数据生成器贡献者 仅休眠 5.2.18 或更高版本

    1. 引入元数据构建器类

    创建以下类,记得添加包和解析导入。

    public class SqlFunctions implements MetadataBuilderContributor {
    
    @Override
    public void contribute(MetadataBuilder metadataBuilder) { 
        metadataBuilder.applySqlFunction( "group_concat", 
            new StandardSQLFunction( "group_concat", 
                StandardBasicTypes.STRING ) ); }
    }
    
    1. 在应用程序启动属性中映射新类

    保持方言属性不变

    spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect
    spring.jpa.properties.hibernate.metadata_builder_contributor = com.myapp.util.SqlFunctions
    

    【讨论】:

      猜你喜欢
      • 2013-12-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-02-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多