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

BadJpqlGrammarException no viable alternative at input for reserved keyword: cannot escape #3056

Closed
multanis63 opened this issue Jul 7, 2023 · 4 comments
Assignees
Labels
status: duplicate A duplicate of another issue

Comments

@multanis63
Copy link

multanis63 commented Jul 7, 2023

Hello,

Since 3.1.1, the following entity / query is not working anymore:

@Entity
@Table
public class TestEntity {

    @Id
    @Column(name = "ID")
    private Long id;

    @NotNull
    @Column(name = "TEST_TYPE", nullable = false)
    private String type;
    }
}
@Query(value = "select te from TestEntity te where te.type = :type")
List<TestEntity> findByType(@Param("type") String type);

Result: org.springframework.data.jpa.repository.query.BadJpqlGrammarException: Line 1:34 no viable alternative at input 'te.type'; Bad JPQL grammar

Seems related to #2791 but I cannot make it work.
I'm not using Hibernate but eclipselink with h2.

What I've tried (all not working):

@Query(value = "select te from TestEntity te where te.`type` = :type")
List<TestEntity> findByType(@Param("type") String type);

@Query(value = "select te from TestEntity te where te.\"type\" = :type")
List<TestEntity> findByType(@Param("type") String type);

@Query(value = "select te from TestEntity te where te.type = :testType")
List<TestEntity> findByType(@Param("testType") String type);

@Query(value = "select te from TestEntity te where te.type2 = :type")
List<TestEntity> findByType(@Param("type") String type);

The only way to make it work is by renaming BOTH entity field and query param:

@Query(value = "select te from TestEntity te where te.type2 = :testType")
List<TestEntity> findByType(@Param("testType") String type);

Or to not use JPQL (but that's not the goal):

List<TestEntity> findByType(String type);

Thanks,

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Jul 7, 2023
@gregturn
Copy link
Contributor

Possible duplicate of #3062

@mp911de mp911de added status: duplicate A duplicate of another issue and removed status: waiting-for-triage An issue we've not yet triaged labels Jul 10, 2023
@gregturn gregturn self-assigned this Jul 10, 2023
@gregturn gregturn added this to the 3.1.2 (2023.0.2) milestone Jul 10, 2023
@gregturn
Copy link
Contributor

The fix was merged to main and backported to 3.1.x.

@SergeVolodin
Copy link

@gregturn , Good Day.
Recently we encountered a similar problem, but for the lateral keyword, when it is an entity field and need to build a jpql query.
We encountered this issue while migrating from Spring Boot 3.0 to Spring Boot 3.1 (spring-data-jpa 3.1.*)

Thanks.

@christophstrobl
Copy link
Member

@SergeVolodin thanks for raising awareness - I've created #3496 to tackle the issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: duplicate A duplicate of another issue
Projects
None yet
Development

No branches or pull requests

6 participants