question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

DTO Projections do not work with @Query annotations

See original GitHub issue

Task 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:closed
  • Created 2 years ago
  • Comments:8 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
dstepanovcommented, Apr 13, 2021

Superpowers…

0reactions
dstepanovcommented, Apr 21, 2021

It should use the strategy defined on the repositorie’s entity Thing

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found