Spring Data Projection and Error: “No aliases found in result tuple! Make sure your query defines aliases!”

前端 未结 3 965
迷失自我
迷失自我 2021-01-05 16:06

The below approach to get a Spring Data Projection from a JPA Query doesn\'t work for me:

https://stackoverflow.com/a/45443776/1005607

My table:

相关标签:
3条回答
  • 2021-01-05 16:27

    I encounter the same problem. After try several changes, I found we just need to add "as" for each column(even the column name is not changed) in NativeQuery. For you here, change your sql like :

        @Query("select a.activityTitle **as activityTitle**, l.description as category, " + 
           "l.displayOrderNum as categoryDisplayOrderNum " + 
           "from ActivitiesT a, LookupT l " + 
           "where a.lookupT.id = l.id order by l.displayOrderNum asc ")
    
    0 讨论(0)
  • 2021-01-05 16:31

    I think you must define exact name methods in the interface, but i'm not sure this approach apply to your case.

    The important bit here is that the properties defined here exactly match properties in the aggregate root. This allows a query method to be added like this

    In your example, you could try an Open Projection

    0 讨论(0)
  • 2021-01-05 16:40

    I had the same problem and i think i solve it. What i did is to use alias in all field, even if they have the same name. Use alias in activityTitle too. Like this:

    @Repository
    public interface ActivitiesDAO extends JpaRepository<ActivitiesT, Integer> {
    
        @Query("select a.activityTitle as activityTitle, l.description as category, " + 
               "l.displayOrderNum as categoryDisplayOrderNum " + 
               "from ActivitiesT a, LookupT l " + 
               "where a.lookupT.id = l.id order by l.displayOrderNum asc ")
        public List<MySpringDataProjection> findCustom();
    
    }
    
    0 讨论(0)
提交回复
热议问题