0

I want to use Spring Data JDBC with QueryDSL support. According to Spring documentation (https://docs.spring.io/spring-data/jdbc/docs/current/reference/html/#core.extensions.querydsl) it is supported, but I couldn't make it working.

I use MariaDB as database and my version of SpringBoot is 2.6.0.

My dependencies in pom.xml:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-jdbc</artifactId>
</dependency>
<dependency> 
    <groupId>com.querydsl</groupId> 
    <artifactId>querydsl-apt</artifactId>
</dependency>
<dependency> 
    <groupId>com.querydsl</groupId> 
    <artifactId>querydsl-sql-spring</artifactId>
</dependency>

Configuration:

@Configuration
@EnableJdbcRepositories
@EnableTransactionManagement
public class DBConfig {

}

My entity class:

package com.test.model.metadata;

import org.springframework.data.annotation.Id;
import org.springframework.data.relational.core.mapping.Table;

@Table
public class Action {

    @Id
    private long id;

    private String name;

...

}

My repository class:

package com.test.repository;

import java.util.List;
import org.springframework.data.querydsl.QuerydslPredicateExecutor;
import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.stereotype.Repository;
import com.test.model.Action;
import com.querydsl.core.types.Predicate;
        
@Repository
public interface ActionRepository extends PagingAndSortingRepository<Action, Long>, QuerydslPredicateExecutor<Action> {

    @Override
    List<Action> findAll(Predicate predicate);
}

QueryDSL predicate usage:

QAction action = QAction.action;
Predicate predicate = action.name.like("%Accept%");
List<Action> actions = actionRepository.findAll(predicate);

Q-classes are generated properly by preprocessor, compilation suceeds but during application startup I am getting error: org.springframework.data.repository.core.support.UnsupportedFragmentException: Repository com.atende.db.metadata.jdbcrepository.ActionRepository implements org.springframework.data.querydsl.QuerydslPredicateExecutor but JdbcRepositoryFactory does not support Querydsl!

What yet could be missing in my solution?

When I am using QueryDSL directly with JdbcTemplate it works:

QAction action = QAction.action;
SQLTemplates dialect = new MySQLTemplates();
Connection connection = DataSourceUtils.getConnection(jdbcTemplate.getDataSource());

SQLQuery<String> query = new SQLQuery<>(connection, dialect);
List<Tuple> actions = query.select(action.id, action.name)
        .from(action)
        .where(action.name.like("%Action%"))
        .fetch();

I also tried to use infobip querydsl library (https://github.com/infobip/infobip-spring-data-querydsl) but got the same error.

manix
  • 11
  • 4
  • Hi! Did you figure out how to deal with data-jdbc + queryDSL? – Waka Waka Jun 28 '23 at 15:32
  • 1
    Spring Data JDBC still does not provide Querydsl support, but you can use infobip querydsl library (https://github.com/infobip/infobip-spring-data-querydsl) which supports it. Or you can try to use QueryDSL directly from com.querydsl.sql.SQLQueryFactory. – manix Jul 07 '23 at 15:17

2 Answers2

0

This is a misinterpretation of the documentation (not your fault, it is structured in an unfortunate way).

You are looking at the preface which describes the general way to work with Spring Data modules. This does not indicate that the module at hand (JDBC in this case) supports all the features mentioned.

Jens Schauder
  • 77,657
  • 34
  • 181
  • 348
  • So you mean that QueryDSL is not supported for Spring Data JDBC? Maybe documentation is not clear but from my perspective it seems this feature is used by some, however. E.g. mentioned library for QueryDSL (https://github.com/infobip/infobip-spring-data-querydsl) has module for Spring Data JDBC - it would be strange to use sth which is not supported yet – manix Jun 14 '22 at 09:13
0

Spring Data JDBC does not provide Querydsl support. That means that the @EnableJdbcRepositories which activates spring-data-jdbc repository implementation does not provide Querydsl support.

If you have infobip-spring-data-jdbc-querydsl-boot-starter on classpath and remove @EnableJdbcRepositories it should work. QuerydslJdbcPredicateExecutor is the fragment implementation.

Lovro Pandžić
  • 5,920
  • 4
  • 43
  • 51
  • I removed `@EnableJdbcRepositories` and it helped. But now I have another problem: Whenever I execute any querydsl query e.g: `userRepository.findAll(qUser.userName.eq("John"));` or `userRepository.query( query -> query.select(userRepository.entityProjection()).from(qUser).where(qUser.userName.eq("John")).fetch());` I get error: **org.springframework.dao.TransientDataAccessResourceException: null; SQL [ from USER_DETAILS User where User.USER_NAME = ?]; Statement.executeQuery() cannot issue statements that do not produce result sets.** Do you have any idea what can be still wrong? – manix Aug 12 '22 at 11:30
  • Hard to tell without more information, first question I'd ask is what is the value of qUser and how are q classes generated? Maybe best continue through the github issue thread, if possible attach a github project that reproduces the issue in minimum steps. – Lovro Pandžić Aug 19 '22 at 18:56