PSQLException: 在这个 ResultSet 中找不到列名 clazz_ [英] PSQLException: The column name clazz_ was not found in this ResultSet

查看:61
本文介绍了PSQLException: 在这个 ResultSet 中找不到列名 clazz_的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

@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 中的信息,也不想不必要地加载它.出于这个原因,我只获取


However, neither do I want to send information stored in google_place nor do I want to load it unnecessarily. For this reason I am only fetching

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 中运行这个语句:

I am able to run this statement in pure PostgreSQL:

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

但不使用 JpaRepository.

顺便说一下,获取 GooglePlaceEntity 是有效的:

And by the way, fetching a GooglePlaceEntity is working however:

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

推荐答案

@Inheritance(strategy = InheritanceType.JOINED)的情况下,当你在没有nativeQuery=True的情况下检索数据时JPA 存储库中的代码>,Hibernate将执行如下SQL:

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

从上面的 SQL 你可以看到 clazz 规范.如果您想将 ResultSet 映射到您的超级实例 (PlaceEntity),您应该自己在 SELECT 中指定 clazz_ 列.

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.

就您而言,它将是:

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

这篇关于PSQLException: 在这个 ResultSet 中找不到列名 clazz_的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆