Return custom object from Spring Data with Native Query

后端 未结 5 2108
一生所求
一生所求 2020-12-10 04:18

My question is based on another post. How can I achieve the same with a native query? Native queries do not allow JPQL thus do not allow new instances either.

My POJ

相关标签:
5条回答
  • 2020-12-10 04:51

    This is https://jira.spring.io/browse/DATAJPA-980 and Here is a project that demonstrates the issue.

    @Query(value = "SELECT name AS name, age AS age FROM Person", nativeQuery = true)
    List<PersonSummary> findAllProjectedNativeQuery();
    

    It is fixed in the Spring Data JPA 2.0 GA (Kay) release which comes with Hibernate 5.2.11.

    The issue is also fixed for Spring Data 1.10.12 (Ingalls) and 1.11.8 (Hopper) but will need to be run on Hibernate 5.2.11 to work.

    0 讨论(0)
  • 2020-12-10 04:52

    If you are using a recent version of spring-data and also making use of the Repositories, I personally think that the answer from Itsallas leads to the right solution.

    I actually did't now about (Spring Data) Projections yet and needed a moment to understand what he was showing in his example.

    Therefore I just want to add a link to the Spring Data JPA - Reference Documentation, have a look at the Projections chapter.

    Spring Data query methods usually return one or multiple instances of the aggregate root managed by the repository. However, it might sometimes be desirable to create projections based on certain attributes of those types. Spring Data allows modeling dedicated return types, to more selectively retrieve partial views of the managed aggregates.

    0 讨论(0)
  • 2020-12-10 04:59

    You will have to use sql result set mapping which is part of JPA.

    0 讨论(0)
  • 2020-12-10 05:01

    Found the answer on another post. Basically I used SqlResultSetMapping along with ConstructorResult (no other way worked out) with a special attention to a comment on the accepted answer of the mentioned post: you need to add the @NamedNativeQuery annotation to the entity of the used interface AND prepend the entity's name with a . otherwise it won't work.

    Example:

    @Entity
    @Table(name = "grupo_setorial")
    @SqlResultSetMapping(
            name = "mapeamentoDeQuadrantes",
            classes = {
                    @ConstructorResult(
                            targetClass = Coordenada.class,
                            columns = {
                                    @ColumnResult(name = "latitude"),
                                    @ColumnResult(name = "longitude")
                            }
                    )
            }
    )
    @NamedNativeQuery(
            name = "GrupoCensitario.obterPerimetroDosSetores",
            query = "SELECT latitude as latitude, longitude as longitude FROM coordenadas where id_setor IN (:setores)",
            resultSetMapping = "mapeamentoDeQuadrantes"
    )
    public class GrupoCensitario {
    
    0 讨论(0)
  • 2020-12-10 05:04

    The answer I found:

    public interface UserEventRepository extends JpaRepository<UserEvent, Long> {
    
        List<UserEvent> findAllByUserId(Long userId);
    
        @Query(value = "SELECT user_id FROM user_event ue " +
                       "WHERE ue.user_id = :userId", nativeQuery = true)
        List<Long> findUserIdByEventId(@Param("userId") Long userId);
    }
    

    That way we return List of Long - list of ids. The key here is that we are setting the nativeQuery property to true. The value itself is the query we want to be executed.

    I hope that helps. It seems a clear solution.

    0 讨论(0)
提交回复
热议问题