infobip/infobip-spring-data-querydsl

Class cannot be cast to class com.querydsl.sql.RelationalPathBase

Closed this issue · 8 comments

I have the same problem as issue#67 (#67)

Repository class:

@org.springframework.stereotype.Repository
public interface MediaCreativeReportQuerydslRepository extends QuerydslJdbcRepository<MediaCreativeReportDb, UUID> {
}

MediaCreativeReportDb class:

@org.springframework.data.relational.core.mapping.Table
@Data
@EqualsAndHashCode
@NoArgsConstructor
@AllArgsConstructor
@com.querydsl.core.annotations.QueryEntity
@com.infobip.spring.data.jdbc.annotation.processor.ProjectTableCaseFormat(CaseFormat.LOWER_UNDERSCORE)
@com.infobip.spring.data.jdbc.annotation.processor.ProjectColumnCaseFormat(CaseFormat.LOWER_UNDERSCORE)
public class MediaCreativeReportDb {
    @org.springframework.data.annotation.Id
    private UUID id;
    // some fields
}

Generated by annotation processor class:

@Generated("com.querydsl.codegen.DefaultEntitySerializer")
public class QMediaCreativeReportDb extends EntityPathBase<MediaCreativeReportDb> {
    // ...
}

Service class:

@Service
@RequiredArgsConstructor
public class MediaCreativeReportServiceImpl implements MediaCreativeReportService {

    private final MediaCreativeReportQuerydslRepository querydslRepository;

    @Override
    public Page<MediaCreativeReportResponseDto> filter(MediaCreativeReportRequestDto dto) {
        QMediaCreativeReportDb report = QMediaCreativeReportDb.mediaCreativeReportDb;

        SQLQuery<MediaCreativeReportDb> where = querydslRepository.query(sqlQuery -> sqlQuery.select(querydslRepository.entityProjection()))
                .from(report)
                .where(whereStatementStringPath(report.creative_id, dto.creativeId()))
                .where(whereStatementComparablePath(report.ord_id, dto.ordId()));
        System.out.println(where);
        return null;
    }

But i have this error message:

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'VMediaCreativeReportController' defined in file [C:\Users\akonyukhov\IdeaProjects\register-service\build\classes\java\main\ru\atc\rkn\service\register\controller\inner\VMediaCreativeReportController.class]: Unsatisfied dependency expressed through constructor parameter 1; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'mediaCreativeReportServiceImpl' defined in file [C:\Users\akonyukhov\IdeaProjects\register-service\build\classes\java\main\ru\atc\rkn\service\register\querydsl\MediaCreativeReportServiceImpl.class]: Unsatisfied dependency expressed through constructor parameter 1; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'mediaCreativeReportQuerydslRepository' defined in ru.atc.rkn.service.register.querydsl.MediaCreativeReportQuerydslRepository defined in @EnableJdbcRepositories declared on QuerydslJdbcRepositoriesRegistrar.EnableJdbcRepositoriesConfiguration: Invocation of init method failed; nested exception is java.lang.ClassCastException: class ru.atc.rkn.service.register.querydsl.QMediaCreativeReportDb cannot be cast to class com.querydsl.sql.RelationalPathBase (ru.atc.rkn.service.register.querydsl.QMediaCreativeReportDb and com.querydsl.sql.RelationalPathBase are in unnamed module of loader 'app')
	at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:800) ~[spring-beans-5.3.18.jar:5.3.18]
	at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:229) ~[spring-beans-5.3.18.jar:5.3.18]
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1372) ~[spring-beans-5.3.18.jar:5.3.18]
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1222) ~[spring-beans-5.3.18.jar:5.3.18]
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:582) ~[spring-beans-5.3.18.jar:5.3.18]
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:542) ~[spring-beans-5.3.18.jar:5.3.18]
	at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:335) ~[spring-beans-5.3.18.jar:5.3.18]
	at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234) ~[spring-beans-5.3.18.jar:5.3.18]
	at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:333) ~[spring-beans-5.3.18.jar:5.3.18]
	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:208) ~[spring-beans-5.3.18.jar:5.3.18]
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:953) ~[spring-beans-5.3.18.jar:5.3.18]
	at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:918) ~[spring-context-5.3.18.jar:5.3.18]
	at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:583) ~[spring-context-5.3.18.jar:5.3.18]
	at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:145) ~[spring-boot-2.6.6.jar:2.6.6]
	at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:740) ~[spring-boot-2.6.6.jar:2.6.6]
	at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:415) ~[spring-boot-2.6.6.jar:2.6.6]
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:303) ~[spring-boot-2.6.6.jar:2.6.6]
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:1312) ~[spring-boot-2.6.6.jar:2.6.6]
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:1301) ~[spring-boot-2.6.6.jar:2.6.6]
	at ru.atc.rkn.service.register.RegisterService.main(RegisterService.java:20) ~[main/:na]

Can you provide a project that reproduces the issue?

Based on everything posted it suggests something was misconfigured - #67 (comment)

Or in other words, genereated Q class should extend RelationalPathBase, not EntityPathBase. RelationalPathBase is used for Querydsl SQL module (spring data jdbc), and EntityPathBase is used for Querydsl JPA module (spring data jpa).

My problem is that two datasources are connected at once in the project. I go to one database through JPA and to the other through JDBC
With the use of JdbcTemplate everything worked successfully. But QueryDsl could not be configured.
I abandoned this idea when I saw that QueryDsl does not have a dialect for ClickHouse.

That shouldn't be a problem - you can use JDBC and JPA in the same project - the only question is how you're generating Q classes. JDBC annotation processor only detects @Id annotated classes and JPA uses JPA annotations.
Can you maybe provide a project that reproduces the issue or do you want to close this issue?

I can't share the project. It has commercial value.
But i can provide an entity example to fetch data using JPA:

@javax.persistence.Entity
@javax.persistence.Table(schema = "register", catalog = "rkn_erir_2", name = "v_media_creative_report")
@NoArgsConstructor
@Getter
@Setter
public class VMediaCreativeReportEntity {
    @javax.persistence.Id
    private UUID id;
    //...
}

This class uses javax.persistence.Id and class for querying by jdbc uses org.springframework.data.annotation.Id
I think the problem is not @id

I can't share the project. It has commercial value.

You don't need to share the project - only replicate the problem in project agnostic manner.
The code you shared isn't enough for me to replicate the issue - there are entity classes like that already that work within tests in the project itself.

Also, note that in last comment your code states @javax.persistence.Id and in the first comment it is @org.springframework.data.annotation.Id. That's the only clue I can see so far that can potentially cause issues depending on which infobip-spring-data-querydsl dependecies your project uses and this is once again why I ask for a minimum project that reproduces the issue.
Can you checkout this project and add try replicating your issue in a test?

I'm closing this down as stale. Feel free to reopen if you can provide more info or a reproduction.