PSQLException: The column name clazz_ was not found in this ResultSet

后端 未结 2 1499
北海茫月
北海茫月 2021-01-27 15:03

I am trying to fetch a PlaceEntity. I\'ve previously stored a bunch of GooglePlaceEntity objects where



        
相关标签:
2条回答
  • 2021-01-27 15:19

    In case of @Inheritance(strategy = InheritanceType.JOINED), when you retrieve data without nativeQuery=True in JPA repository, Hibernate will execute SQL like the following:

    SELECT
    table0_.id as id1_1_,
    table0_.column2 as column2_2_1_,
    ... (main_table cols)
    table0_1_.column1 as column1_1_0_,
    ... (table1 to N-1 cols)
    table0_N_.column1 as column1_1_9_,
    ... (tableN-th cols)
    CASE WHEN table0_1_.id is not null then 1
        ... (table1 to N-1 cols)
        WHEN table0_N_.id is not null then N
        WHEN table0_.id is not null then 0
        END as clazz_
    FROM table table0_
    left outer join table1 table0_1_ on table0_.id=table0_1_.id
    ... (other tables join)
    left outer join table2 table0_N_ on table0_.id=table0_N_.id
    

    From the above SQL you can see clazz specification. If you want to map ResultSet to your super instance (PlaceEntity), you should specify clazz_ column in SELECT by yourself.

    In your case it will be:

    @Query(value = "" +
                "SELECT *, 0 AS clazz_ " +
                "FROM place " +
                "WHERE earth_distance( " +
                "   ll_to_earth(place.latitude, place.longitude), " +
                "   ll_to_earth(:latitude, :longitude) " + 
                ") < :radius",
                nativeQuery = true)
    
    0 讨论(0)
  • 2021-01-27 15:37

    You should use the name of the class instead of the table name on the query. Change place to PlaceEntity.

    @Query(value = "" +
        "SELECT * " +
        "FROM place JOIN google_place ON google_place.id = place.id " +
        "WHERE earth_distance( " +
        "   ll_to_earth(place.latitude, place.longitude), " +
        "   ll_to_earth(:latitude, :longitude) " + 
        ") < :radius",
        nativeQuery = true)
    List<GooglePlaceEntity> findNearby(@Param("latitude") Float latitude,
                                   @Param("longitude") Float longitude,
                                   @Param("radius") Integer radius);
    
    0 讨论(0)
提交回复
热议问题