Home > database >  Why does Spring JPA @Query return an error when I omit the table alias?
Why does Spring JPA @Query return an error when I omit the table alias?

Time:02-01

I'm querying a PostgreSQL table with a very simple query using Spring Data JPA:

@Query(value = "SELECT id FROM location l", nativeQuery = true)

This works fine. However, if I omit the table alias and use:

@Query(value = "SELECT id FROM location", nativeQuery = true)

I get an error saying No Dialect mapping for JDBC type: 1111. ID is a UUID in both PostgreSQL and the DTO.

Why does this happen? As far as I can tell, that's a perfectly valid query, and I'm not even using the table alias in the working version. Adding @Type and @TypeMappings made no difference. I checked the data I'm querying, and all the IDs are valid.

I'm happy to use an alias now that I've figured this out, but I'm curious about what's happening behind the scenes that causes this to fail.

CodePudding user response:

Spring Data JPA tries to parse your query in order to derive a count query or add/modify and ORDER BY clause. This to some extend relies on having an alias for the main table/entity of the query.

It should give you a proper error that it can't find an alias instead of the weird error you are seeing.

  •  Tags:  
  • Related