我正在尝试获取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),当您在 JPA存储库中检索不带nativeQuery=True的数据时,休眠 >将执行如下所示的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.
在您的情况下,它将是:
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
发布评论