Hibernate空间jpa查询不起作用

时间:2017-10-09 10:53:23

标签: spring spring-boot hibernate-spatial

我编写了空间查询来查找距离某个位置最近的点。但是hibernate得到了异常。 我的Spring Boot 1.5.7应用程序

@Repository
@Transactional(readOnly = true)
public interface PlaceRepository extends JpaRepository<PlaceEntity, Long> {
    PlaceEntity findByCode(String code);
    PlaceEntity findByName(String name);
    @Query("select l from PlaceEntity l where within(l.location, :filter) = true")
    List<PlaceEntity> findLocationWithin(Geometry filter);
}

这是我的实体类参数

@Data
@Entity(name = "Place")
public class PlaceEntity extends BaseEntity {

@Type(type = "org.hibernate.spatial.GeometryType")
@Column(name = "LOCATION",columnDefinition = "POINT")
    private Point location;
}

其他参数

@Id
@GeneratedValue
@Column(name = "ID")
private long id;
@Column(name = "NAME")
private String name;
@Column(name = "CODE")
private String code;
@Column(name = "LONGITUDE")
private Double longitude;
@Column(name = "LATITUDE")
private Double latitude;

这是我的build.gradle。

compile('org.springframework.boot:spring-boot-starter-actuator')
compile('org.springframework.boot:spring-boot-starter-data-jpa')
compile('org.hibernate:hibernate-core:4.3.0.Final')
compile("org.hibernate:hibernate-spatial:4.3")
compile("org.hibernate:hibernate-entitymanager:4.3.0.Final")
compile('mysql:mysql-connector-java')
compile('org.projectlombok:lombok:1.14.8')
compile('org.springframework.boot:spring-boot-starter-web')
compile('com.vividsolutions:jts:1.13')
testCompile('org.springframework.boot:spring-boot-starter-test')

配置

@EnableWebMvc
@Configuration
public class AppConfig {

}

但是当我部署服务器然后获得异常。

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'pathController': Unsatisfied dependency expressed through field 'pathService'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'pathServiceImpl': Unsatisfied dependency expressed through field 'placeRepository'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'placeRepository': Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Validation failed for query for method public abstract java.util.List com.fm.assignment.core.dao.PlaceRepository.findLocationWithin(com.vividsolutions.jts.geom.Geometry)!
        at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:588)



Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'pathServiceImpl': Unsatisfied dependency expressed through field 'placeRepository'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'placeRepository': Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Validation failed for query for method public abstract java.util.List com.fm.assignment.core.dao.PlaceRepository.findLocationWithin(com.vividsolutions.jts.geom.Geometry)!
        at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:58


Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'placeRepository': Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Validation failed for query for method public abstract java.util.List com.fm.assignment.core.dao.PlaceRepository.findLocationWithin(com.vividsolutions.jts.geom.Geometry)!
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1628)



Caused by: java.lang.IllegalArgumentException: Validation failed for query for method public abstract java.util.List com.fm.assignment.core.dao.PlaceRepository.findLocationWithin(com.vividsolutions.jts.geom.Geometry)!
        at org.springframework.data.jpa.repository.query.SimpleJpaQuery.validateQuery(SimpleJpaQuery.java:92)
        at org.springframework.data.jpa.repository.query.SimpleJpaQuery.<init>(SimpleJpaQuery.java:62)


Caused by: java.lang.IllegalArgumentException: org.hibernate.hql.internal.ast.QuerySyntaxException: PlaceEntity is not mapped [select l from PlaceEntity l where within(l.location, :filter) = true]
        at org.hibernate.jpa.spi.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1750)
        at org.hibernate.jpa.spi.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1677)



Caused by: org.hibernate.hql.internal.ast.QuerySyntaxException: PlaceEntity is not mapped [select l from PlaceEntity l where within(l.location, :filter) = true]
        at org.hibernate.hql.internal.ast.QuerySyntaxException.generateQueryException(QuerySyntaxException.java:96)
        at org.hibernate.QueryException.wrapWithQueryString(QueryException.java:120)




Caused by: org.hibernate.hql.internal.ast.QuerySyntaxException: PlaceEntity is not mapped
        at org.hibernate.hql.internal.ast.util.SessionFactoryHelper.requireClassPersister(SessionFactoryHelper.java:189)
        at org.hibernate.hql.internal.ast.tree.FromElementFactory.addFromElement(FromElementFactory.java:109)
        at org.hibernate.hql.internal.ast.tree.FromClause.addFromElement(FromClause.java:95)

我的配置是否有任何错误?

2 个答案:

答案 0 :(得分:1)

您已将实体命名为地点@Entity(name = "Place")),但仍在jpql查询中使用 PlaceEntity

试试这样;

select l from Place l where within(l.location, :filter) = true

或继续在查询中使用PlaceEntity并使用以下注释;

@Data
@Entity
@Table(name ="Place")
public class PlaceEntity extends BaseEntity {}

答案 1 :(得分:1)

将来在spring-data-jpa存储库中使用变量,例如#{#entityName} ,以避免此类错误

@Query("select l from #{#entityName} l where within(l.location, :filter) = true")