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

编程入门 行业动态 更新时间:2024-10-21 17:41:48
本文介绍了PSQLException:在此 ResultSet 中找不到列名 clazz_的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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; }

and

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

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

and what I get is this:

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] ...

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;

but not using the JpaRepository.

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

解决方案

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)

更多推荐

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

本文发布于:2023-10-18 10:56:57,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1504033.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:在此   中找   PSQLException   clazz   ResultSet

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!