DTO Projections do not work with @Query annotations
See original GitHub issueTask List
- Steps to reproduce provided
- Stacktrace (if present) provided
- Example that reproduces the problem uploaded to Github
- Full description of the issue provided (see below)
Steps to Reproduce
@Introspected
@MappedEntity
data class Thing(
@field:GeneratedValue
@field:Id
var id: Int? = null,
var name: String
var score: Int,
var site: String
)
@Introspected
data class ThingDTO(
val thingId: Int,
val thingName: String
)
@Repository
@JdbcRepository(dialect = Dialect.MYSQL)
interface CasbinRuleRepository : CrudRepository<Thing, Int> {
fun findByName(name: String): List<Thing> // works - of course
@Query("""
SELECT thing.id AS thingId, thing.name AS thingName
FROM things thing
WHERE thing.id = :id
""")
fun findThingDTOsByThingId(id: Int): List<ThingDTO> // fails!
}
Expected Behaviour
It should project the two fields onto the DTO
Actual Behaviour
Compilation Error:
error: Unable to implement Repository method: ThingRepository.findThingDTOsByThingId(Integer id). Property thingId is not present in entity: com.example.entities.Thing
public abstract java.lang.Object findThingDTOsByThingId(@org.jetbrains.annotations.NotNull()
Environment Information
- Operating System: Mac/Linux
- Micronaut Version: 2.4.2
- Micronaut Data Version: 2.3.1
- JDK Version: 11
Should this work?
I’m not actually sure if this is something that is supported. So this could be a bug or a feature!
Issue Analytics
- State:
- Created 2 years ago
- Comments:8 (5 by maintainers)
Top Results From Across the Web
Why, When and How to Use DTO Projections with JPA and ...
DTO projections are the most efficient ones for read operations. Let me show you how to use them in JPQL, Criteria and native...
Read more >java - Spring Data JPA - Nested DTO projection - Stack Overflow
I am trying to use DTO projection to obtain my desire output. Here are my domain models. @Entity @Table public class User {...
Read more >The best way to fetch a Spring Data JPA DTO Projection
Introduction. In this article, I'm going to show you what is the best way to fetch a Spring Data JPA DTO Projection.
Read more >Spring Data JPA Projections - Baeldung
Open projections do have a drawback though; Spring Data can't optimize query execution, as it doesn't know in advance which properties will ...
Read more >Spring Data JPA – How to Return DTOs from Native Queries
You can use an interface-based DTO projection with a native query in the same way you use it with a derived or custom...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Superpowers…
It should use the strategy defined on the repositorie’s entity
Thing