@NamedNativeQuery - How can I bind it to a repository method?

前端 未结 2 1373
别那么骄傲
别那么骄傲 2020-12-17 14:44

I am using Spring + Hibernate and I have a particular case where I need to obtain (a list of) non-Entity objects as a result of the qu

相关标签:
2条回答
  • 2020-12-17 15:11

    Although the answer is correct, for someone to arrived here for similar cuestions this works for me without used @NamedNativeQuery:

    public class Example {
    
    private Long someProperty;
    // getters and setters
    }
    

    To call a query or stored procedure and populate my object:

    @Repository
    public interface ExampleRepository extends 
    JpaRepository<Example,Long> {
    
    /**
     * Search Example by someProperty
     *
     * @param property
     * @return
     */
    @Query( nativeQuery = true, value = "SELECT * FROM public.my_stored_procedure(:property)")
    List<Example> findByProperty(@Param("property") Long property);
    
    }
    

    I know this is another approach and the code is decoupled, but we get the same result.

    I hope that can be useful for someone. Regards.

    0 讨论(0)
  • 2020-12-17 15:19

    The name value on the @NamedNativeQuery needs to be set to "AdDailyDataEntity.aggregateRevenue". The first part (before the dot) needs to match the entity class name.

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