Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

unknown ClassCastException #1885

Closed
yushengruohui opened this issue Apr 4, 2024 · 11 comments
Closed

unknown ClassCastException #1885

yushengruohui opened this issue Apr 4, 2024 · 11 comments
Assignees

Comments

@yushengruohui
Copy link

Description

As Querydsl recommends the use of blaze-persistence after version 5.0.0, I integrated it. Some simple single-table queries worked fine, but when I tried to execute the following query, I encountered a ClassCastException.

Steps to reproduce

code:

        QBdpDept t1 = QBdpDept.bdpDept;
        QDeptRole t2 = QDeptRole.deptRole;
        String keyword = dto.getKeyword();

        BooleanBuilder builder = new BooleanBuilder();
        builder.and(t2.id.isNull());
        builder.and(t2.roleId.eq(dto.getRoleId()));

        var query = new BlazeJPAQuery<>(entityManager, criteriaBuilderFactory)
                .select(Projections.bean(RoleDeptVo.class, t2.id, t2.deptId, t1.pid, t1.deptName, t1.deptCode))
                .from(t1)
                .leftJoin(t2)
                .on(t2.deptId.eq(t1.id))
                .where(builder)
                .orderBy(t1.id.desc())
                .offset(dto.getOffset())
                .limit(dto.getPageSize());
        long count = query.fetchCount();

exception log:

java.lang.ClassCastException : class jdk.proxy2.$Proxy233 cannot be cast to class org.hibernate.query.sqm.internal.QuerySqmImpl (jdk.proxy2.$Proxy233 is in module jdk.proxy2 of loader 'app'; org.hibernate.query.sqm.internal.QuerySqmImpl is in unnamed module of loader 'app')
        at com.blazebit.persistence.integration.hibernate.base.HibernateExtendedQuerySupport.getSql(HibernateExtendedQuerySupport.java:221)
        at com.blazebit.persistence.impl.query.CustomQuerySpecification.<init>(CustomQuerySpecification.java:107)
        at com.blazebit.persistence.impl.AbstractFullQueryBuilder.getCountQuery(AbstractFullQueryBuilder.java:715)
        at com.blazebit.persistence.impl.AbstractFullQueryBuilder.getCountQuery(AbstractFullQueryBuilder.java:642)
        at com.blazebit.persistence.querydsl.AbstractBlazeJPAQuery.createQuery(AbstractBlazeJPAQuery.java:173)
        at com.blazebit.persistence.querydsl.AbstractBlazeJPAQuery.fetchCount(AbstractBlazeJPAQuery.java:261)

maven dependency:

        <dependency>
            <groupId>com.blazebit</groupId>
            <artifactId>blaze-persistence-integration-querydsl-expressions-jakarta</artifactId>
            <version>${blaze-persistence.version}</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>com.blazebit</groupId>
            <artifactId>blaze-persistence-core-api-jakarta</artifactId>
            <version>${blaze-persistence.version}</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>com.blazebit</groupId>
            <artifactId>blaze-persistence-core-impl-jakarta</artifactId>
            <version>${blaze-persistence.version}</version>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>com.blazebit</groupId>
            <artifactId>blaze-persistence-integration-hibernate-6.2</artifactId>
            <version>${blaze-persistence.version}</version>
            <scope>runtime</scope>
        </dependency>

Environment

Version: 1.6.11
JPA-Provider: Hibernate 6.4.1
DBMS: Postgres 16
springboot: 3.2.2
jdk: 17

@beikov
Copy link
Member

beikov commented Apr 8, 2024

Hi and thanks for reporting this issue. It seems like something (Spring ORM integration?) creates a proxy for a created query which then fails to unwrap correctly. Do you think you could provide a reproducer for this?

@beikov beikov added kind: bug component: QueryDSL worth: high Implementing this has a high worth labels Apr 8, 2024
@jwgmeligmeyling
Copy link
Collaborator

IIRC Projections.bean is a factory expression that has to be used with a query transformer (query().transform(Projections.bean(…)) and cannot be used as a select expression directly. I don’t think this is specific to the integration, does it work in standard Querydsl?

@yushengruohui
Copy link
Author

IIRC Projections.bean is a factory expression that has to be used with a query transformer (query().transform(Projections.bean(…)) and cannot be used as a select expression directly. I don’t think this is specific to the integration, does it work in standard Querydsl?

In Querydsl, this code can run normally. It's only after switching to BlazeJPAQuery that I encountered this issue.

@yushengruohui
Copy link
Author

Hi and thanks for reporting this issue. It seems like something (Spring ORM integration?) creates a proxy for a created query which then fails to unwrap correctly. Do you think you could provide a reproducer for this?

I apologize, but it's not convenient for me to provide the complete code from the team project.

@beikov
Copy link
Member

beikov commented Apr 8, 2024

Hi and thanks for reporting this issue. It seems like something (Spring ORM integration?) creates a proxy for a created query which then fails to unwrap correctly. Do you think you could provide a reproducer for this?

I apologize, but it's not convenient for me to provide the complete code from the team project.

Sure, that's fine, but maybe you can create a small reproducer application e.g. based on our quickstarts?

@wangguofeng1923
Copy link

spring-examples.zip
i reproduced this issue too, and attacted my demo application

@jwgmeligmeyling jwgmeligmeyling self-assigned this May 6, 2024
@beikov
Copy link
Member

beikov commented May 6, 2024

I just checked the reproducer and this seems to be an issue with Spring Data. A proxy created by SharedEntityManagerCreator is used as EntityManager, which in turn creates a proxy for queries. This is where things go wrong, but not sure why that pops up only just now. Seems the code was like this for a long time.

@beikov
Copy link
Member

beikov commented May 6, 2024

One possible workaround is to mark the method as @Transactional to use the proper EntityManager and not this Spring Data proxy

@beikov
Copy link
Member

beikov commented May 6, 2024

@jwgmeligmeyling
Copy link
Collaborator

We can probably close this issue then right? Or do you see a way to workaround this limitation that we can implement?

@beikov
Copy link
Member

beikov commented May 6, 2024

Yes, I'm going to close this issue.

@beikov beikov closed this as completed May 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants