question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

GROUP_CONCAT does NOT work in QueryDSL

See original GitHub issue

Hi there,

I am struggling to figure out how to use ‘GROUP_CONCAT’ in QueryDSL.

final QDataSourceSmallEliminate eliminate = QDataSourceSmallEliminate.dataSourceSmallEliminate;
		final QLattitudeVisitor lattitude = LattitudeVisitorServiceImpl.$;
//		Expressions.stringOperation(SQLOps.GROUP_CONCAT, lattitude.tagName)
		
		return QuerydslUtils.newQuery(entityManager).select(Projections.constructor(EliminateLattitude.class, 
				eliminate.id, 
				JPAExpressions.selectDistinct( SQLExpressions.groupConcat(lattitude.tagName)).from(lattitude).where(lattitude.urlCrc.eq(eliminate.urlCrc))))
			.from(eliminate)
			.fetch();

Exception:

org.springframework.dao.InvalidDataAccessApiUsageException: No pattern found for GROUP_CONCAT; nested exception is java.lang.IllegalArgumentException: No pattern found for GROUP_CONCAT

...

Caused by: java.lang.IllegalArgumentException: No pattern found for GROUP_CONCAT
	at com.querydsl.core.support.SerializerBase.visitOperation(SerializerBase.java:280)
	at com.querydsl.jpa.JPQLSerializer.visitOperation(JPQLSerializer.java:437)
	at com.querydsl.core.support.SerializerBase.visit(SerializerBase.java:231)
	at com.querydsl.core.support.SerializerBase.visit(SerializerBase.java:31)
	at com.querydsl.core.types.OperationImpl.accept(OperationImpl.java:83)
	at com.querydsl.core.support.SerializerBase.handle(SerializerBase.java:92)
	at com.querydsl.jpa.JPQLSerializer.serialize(JPQLSerializer.java:203)
	at com.querydsl.jpa.JPQLSerializer.visit(JPQLSerializer.java:358)
	at com.querydsl.jpa.JPQLSerializer.visit(JPQLSerializer.java:39)
	at com.querydsl.core.types.SubQueryExpressionImpl.accept(SubQueryExpressionImpl.java:57)
	at com.querydsl.core.support.FetchableSubQueryBase.accept(FetchableSubQueryBase.java:150)
	at com.querydsl.core.support.SerializerBase.handle(SerializerBase.java:92)
	at com.querydsl.core.support.SerializerBase.handle(SerializerBase.java:119)
	at com.querydsl.core.support.SerializerBase.visit(SerializerBase.java:225)
	at com.querydsl.core.support.SerializerBase.visit(SerializerBase.java:31)
	at com.querydsl.core.types.ConstructorExpression.accept(ConstructorExpression.java:112)
	at com.querydsl.core.support.SerializerBase.handle(SerializerBase.java:92)
	at com.querydsl.jpa.JPQLSerializer.serialize(JPQLSerializer.java:203)
	at com.querydsl.jpa.JPAQueryBase.serialize(JPAQueryBase.java:60)
	at com.querydsl.jpa.JPAQueryBase.serialize(JPAQueryBase.java:50)
	at com.querydsl.jpa.impl.AbstractJPAQuery.createQuery(AbstractJPAQuery.java:98)
	at com.querydsl.jpa.impl.AbstractJPAQuery.createQuery(AbstractJPAQuery.java:94)
	at com.querydsl.jpa.impl.AbstractJPAQuery.fetch(AbstractJPAQuery.java:201)
	at com.moraydata.general.secondary.repository.impl.DataSourceSmallEliminateRepositoryImpl.findObject(DataSourceSmallEliminateRepositoryImpl.java:98)
	at com.moraydata.general.secondary.repository.impl.DataSourceSmallEliminateRepositoryImpl$$FastClassBySpringCGLIB$$b42d875d.invoke(<generated>)
	at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204)

QueryDSL 4.2.1 & JDK1.8 & Spring Boot2.0 & MySQL 5.7.

I read through the latest reference doc still cannot find the answer, so what should I do to make it work ?

Please help. Thanks in advance. @Shredder121

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

4reactions
giraffe-treecommented, Jan 25, 2019

@beamofsoul

if you use spring boot data jpa and querydsl-jpa , You can follow these steps

  1. create a custom DB dialect and register function
//  org.hibernate.dialect.MySQL5Dialect
public class CustomMysqlDialect extends MySQL5Dialect {
    public CustomMysqlDialect() {
        super();
        // register custom/inner function here
        this.registerFunction("group_concat", new SQLFunctionTemplate(StandardBasicTypes.STRING, "group_concat(?1)"));
    }
}
  1. config JpaVendorAdapter
@Configuration
public class JpaConfiguration {
    @Bean
    public JpaVendorAdapter jpaVendorAdapter() {
        AbstractJpaVendorAdapter adapter = new HibernateJpaVendorAdapter();
        adapter.setShowSql(true);
        adapter.setDatabase(Database.MYSQL);
       // package to CustomMysqlDialect
        adapter.setDatabasePlatform("com.xxx.xxx.config.CustomMysqlDialect");
        adapter.setGenerateDdl(false);
        return adapter;
    }
}
  1. use query-dsl
// before do this , autowird JPAQueryFactory at first 
        QReportDoctorTag qReportDoctorTag = QReportDoctorTag.reportDoctorTag;
        SimpleTemplate<String> simpleTemplate = Expressions.simpleTemplate(String.class, "group_concat({0})", qReportDoctorTag.tag);
        JPAQuery<Tuple> tupleJPAQuery = queryFactory.select(qReportDoctorTag.reportId, simpleTemplate)
                .from(qReportDoctorTag)
                .groupBy(qReportDoctorTag.reportId);

        List<Tuple> fetch = tupleJPAQuery.fetch();
0reactions
jwgmeligmeylingcommented, Oct 28, 2020

In JPA this is unsupported. You’ll have to ask your ORM vendor to implement it first (for example Hibernate).

You could consider using the blaze-persistence-querydsl extension to fill the gap: https://persistence.blazebit.com/documentation/1.5/core/manual/en_US/index.html#querydsl-integration

Which has the following methods:

  • JPQLNextExpressions.groupConcat(Expression<?> expression, String separator, OrderSpecifier<?>... orderSpecifiers)
  • JPQLNextExpressions.groupConcat(Expression<?> expression, Expression<String> separator, OrderSpecifier<?>... orderSpecifiers)
  • JPQLNextExpressions.groupConcat(boolean distinct, Expression<?> expression, String separator, OrderSpecifier<?>... orderSpecifiers)
  • JPQLNextExpressions.groupConcat(boolean distinct, Expression<?> expression, Expression<String> separator, OrderSpecifier<?>... orderSpecifiers)
Read more comments on GitHub >

github_iconTop Results From Across the Web

Group concat equivalent in queryDSL - java - Stack Overflow
I am using queryDSL with spring JPA, and it is updated to the latest version, and I was not able to find such...
Read more >
GroupConcat equivalent in SQLSubquery querydsl
Hi all I am using nested quries in query dsl. One of the subquery is returning a single column. I want to concat...
Read more >
QueryDsl QuerydslRepositorySupport에서 group_concat사용 ...
출처 https://github.com/querydsl/querydsl/issues/2377 · GROUP_CONCAT does NOT work in QueryDSL · Issue #2377 · querydsl/querydsl.
Read more >
Uses of Interface com.querydsl.core.types.Expression
Wrap a Querydsl expression into a Guava function ... Create a left is not null expression ... Get a group_concat(expr, separator) expression.
Read more >
Criteria API for JPA backends - Blaze Persistence
When you work with Querydsl you can additionally have first class integration by using the ... Some features like CTEs simply can not...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found