Sql 如何在hibernate标准中使用group_concat?

Sql 如何在hibernate标准中使用group_concat?,sql,hibernate,Sql,Hibernate,我在mysql中使用group_concat编写了一个查询 SELECT c1,group_concat(c2) FROM table1 where sno in(1,4,8,10) group by c1; 并给出了我的预期结果 SELECT c1,group_concat(c2) FROM table1 where sno in(1,4,8,10) group by c1; 现在,我想使用hibernate条件编写相同的查询。请参考以下代码片段 SELECT c1,group_conca

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

SELECT c1,group_concat(c2) FROM table1 where sno in(1,4,8,10) group by c1;
并给出了我的预期结果

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

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

请参考以下代码片段

SELECT c1,group_concat(c2) FROM table1 where sno in(1,4,8,10) group by c1;
Criteria cr = session.createCriteria(table1.class);
cr.add(Restrictions.in("sno",snoarray));
criteria.setProjection("c1");
criteria.setProjection(Projections.groupProperty("c1"));

简单的答案是否定

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

SELECT c1,group_concat(c2) FROM table1 where sno in(1,4,8,10) group by c1;
Hibernate只支持在多个数据库中使用的通用函数/语法。Microsoft SQL Server中没有任何
group\u concat
函数,也可能在其他数据库中

SELECT c1,group_concat(c2) FROM table1 where sno in(1,4,8,10) group by c1;
解决方案:

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

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

最后,我完成了如下代码,并得到了预期的结果

SELECT c1,group_concat(c2) FROM table1 where sno in(1,4,8,10) group by c1;
String query=“从表1中选择c1,group_concat(c2),其中sno在(:PageID)中按c1分组”

SELECT c1,group_concat(c2) FROM table1 where sno in(1,4,8,10) group by c1;
SQLQuery=session.createSQLQuery(查询)

SELECT c1,group_concat(c2) FROM table1 where sno in(1,4,8,10) group by c1;
setParameterList(“PageID”,myList)

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

SELECT c1,group_concat(c2) FROM table1 where sno in(1,4,8,10) group by c1;
c1组混凝土(c2) aaa值1,值2 bbb值3 ccc值4、值5、值6

您有两个选项(取决于您的hibernate版本)

SELECT c1,group_concat(c2) FROM table1 where sno in(1,4,8,10) group by c1;
覆盖方言类 任何hibernate版本

SELECT c1,group_concat(c2) FROM table1 where sno in(1,4,8,10) group by c1;
您需要对方言进行子类化以添加
group\u concat()

SELECT c1,group_concat(c2) FROM table1 where sno in(1,4,8,10) group by c1;
  • 介绍方言覆盖类
  • 在应用程序中的某个位置创建以下类(例如util包)

    SELECT c1,group_concat(c2) FROM table1 where sno in(1,4,8,10) group by c1;
    
  • 将方言覆盖类映射到启动属性
  • 将以下属性添加到应用程序中。属性

    SELECT c1,group_concat(c2) FROM table1 where sno in(1,4,8,10) group by c1;
    
    spring.jpa.properties.hibernate.dial=com.myapp.util.mysqlcustomdial

    SELECT c1,group_concat(c2) FROM table1 where sno in(1,4,8,10) group by c1;
    
    使用JPA元数据生成器贡献者 仅限hibernate 5.2.18或更新版本

    SELECT c1,group_concat(c2) FROM table1 where sno in(1,4,8,10) group by c1;
    
  • 介绍元数据生成器类
  • 创建以下类,请记住添加包并解析导入

    SELECT c1,group_concat(c2) FROM table1 where sno in(1,4,8,10) group by c1;
    
    public class SqlFunctions implements MetadataBuilderContributor {
    
    @Override
    public void contribute(MetadataBuilder metadataBuilder) { 
        metadataBuilder.applySqlFunction( "group_concat", 
            new StandardSQLFunction( "group_concat", 
                StandardBasicTypes.STRING ) ); }
    }
    
  • 在应用程序启动属性中映射新类
  • 保持方言属性不变

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

    这个标准对象是如何产生的。我认为这是您创建的cr,在本例中是criteria.setProjection(“c1”);我认为这是错误的(我们必须给出像cr.setProjection(Projections.projectionList().add(Projections.property(“c1”))这样的投影);即使在这种情况下,如果我添加Projections.groupProperty(“c1”)也是如此;作为一个组,它将只提供一条记录,而不是多条记录。因此我需要多条记录。我搜索了许多文档,但没有找到任何解决方案,因此最后我在hibernate中使用了createSQLQuery(query),并获得了预期的结果。是的,您没有任何选择…;)此功能仍然不受支持吗?除了使用sql查询,还有什么解决方法吗?这个答案有误导性,可能是因为它已经过时了。请检查有点旧,但实际上,如果你使用MySQL,你可以做到这一点。您需要将其添加到MYSQL方言中。查看更多