gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-05 01:00:27 30 4
gpt4 key购买 nike

我正在尝试获取 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);

最佳答案

@Inheritance(strategy = InheritanceType.JOINED) 的情况下,当您在 JPA 存储库 中检索没有 nativeQuery=True 的数据时,< strong>Hibernate 将执行如下 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 映射到您的 super 实例(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)

关于postgresql - PSQLException : The column name clazz_ was not found in this ResultSet,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49804053/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com