PSQLException:在此ResultSet中找不到列名clazz_

时间:2018-04-12 18:59:34

标签: postgresql hibernate spring-data spring-data-jpa

我正在尝试获取PlaceEntity。我以前存储了一堆GooglePlaceEntity个对象

@Entity
@Table(name = "place")
@Inheritance(
        strategy = InheritanceType.JOINED
)
public class PlaceEntity extends AbstractTimestampEntity {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
}

@Entity
@Table(name = "google_place")
public class GooglePlaceEntity extends PlaceEntity {
    // Additional fields ..
}

但是,我既不想发送存储在google_place中的信息,也不想不必要地加载它。出于这个原因,我只是提取

public interface PlaceRepository extends JpaRepository<PlaceEntity, Long> {

    @Query(value = "" +
            "SELECT * " +
            "FROM place " +
            "WHERE earth_distance( " +
            "   ll_to_earth(place.latitude, place.longitude), " +
            "   ll_to_earth(:latitude, :longitude) " + 
            ") < :radius",
            nativeQuery = true)
    List<PlaceEntity> findNearby(@Param("latitude") Float latitude,
                                 @Param("longitude") Float longitude,
                                 @Param("radius") Integer radius);

}

我得到的是:

org.postgresql.util.PSQLException: The column name clazz_ was not found in this ResultSet.
    at org.postgresql.jdbc.PgResultSet.findColumn(PgResultSet.java:2588) ~[postgresql-9.4.1208-jdbc42-atlassian-hosted.jar:9.4.1208]
    at org.postgresql.jdbc.PgResultSet.getInt(PgResultSet.java:2481) ~[postgresql-9.4.1208-jdbc42-atlassian-hosted.jar:9.4.1208]
    at com.zaxxer.hikari.pool.HikariProxyResultSet.getInt(HikariProxyResultSet.java) ~[HikariCP-2.7.8.jar:na]
    at org.hibernate.type.descriptor.sql.IntegerTypeDescriptor$2.doExtract(IntegerTypeDescriptor.java:62) ~[hibernate-core-5.2.14.Final.jar:5.2.14.Final]
    at org.hibernate.type.descriptor.sql.BasicExtractor.extract(BasicExtractor.java:47) ~[hibernate-core-5.2.14.Final.jar:5.2.14.Final]
    ...

我能够在纯PostgreSQL中运行此语句:

SELECT * FROM place WHERE 
earth_distance( 
  ll_to_earth(place.latitude, place.longitude), 
  ll_to_earth(17.2592522, 25.0632745)
) < 1500;

但未使用JpaRepository

顺便说一句,提取GooglePlaceEntity正在发挥作用:

@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);

2 个答案:

答案 0 :(得分:2)

对于@Inheritance(strategy = InheritanceType.JOINED),在 JPA存储库中检索不带nativeQuery=True的数据时,休眠将执行以下SQL:

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

从上面的SQL中,您可以看到 clazz 规范。如果要将 ResultSet 映射到您的超级实例(PlaceEntity),则应自行在 SELECT 中指定 clazz _ 列。

您的情况将是:

@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)

答案 1 :(得分:0)

您应该在查询中使用类的名称而不是表名。将地点更改为 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);