在这里,我试图找到我提供的范围内的所有实体。我的意思是,如果给定一定半径的圆,则必须显示所有位置坐标位于给定圆内的实体。
我正在使用休眠空间来实现这一目标。但是在JPA Repository接口中遇到了提到的错误。
这里是pom.xml
,
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-spatial</artifactId>
<version>5.2.12.Final</version>
</dependency>
<dependency>
<groupId>org.opengeo</groupId>
<artifactId>geodb</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>6.0.6</version>
</dependency>
Jpa存储库
public interface ResourceRepository extends ExtendedJpaRepository<Resource, String> {
@Query(value = "select resource from Resource resource where within(resource.address.location, :circle) = true")
List<Resource> test(@Param("circle") Geometry circle);
}
Resource.java
,
@Entity
@NoArgsConstructor
public class Resource extends UUIDEntity2 implements IsResource {
@Type(type = "org.hibernate.spatial.GeometryType")
@OneToOne
private Address address;
/*getters setters*/
}
Address.java
@Entity
public class Address extends UUIDEntity2 implements HasEmailAddress, HasLocation {
@Embedded
@Column(columnDefinition = "point")
private Location location;
/*getters setters*/
}
location.java
,
@Embeddable
@Value(staticConstructor = "of")
@RequiredArgsConstructor(staticName = "of")
public class Location implements Serializable {
@Column(nullable = true)
private Double lat;
@Column(nullable = true)
private Double lon;
}
测试
@Inject
private ResourceRepository resourceRepository;
public Geometry createCircle(double x, double y, double radius) {
GeometricShapeFactory shapeFactory = new GeometricShapeFactory();
shapeFactory.setNumPoints(32);
shapeFactory.setCentre(new Coordinate(x, y));
shapeFactory.setSize(radius * 2);
return shapeFactory.createCircle();
}
@Test
public void geometry(){
Geometry m = createCircle(0.0, 0.0, 5);
List<Resource> resources = resourceRepository.test(m);
}
application.properties
,
hibernate.dialect=org.hibernate.spatial.dialect.mysql.MySQL56SpatialDialect
注意:实体的所有属性未在此处显示。 我正在关注的参考文献:Hibernate-Spatial
答案 0 :(得分:1)
听起来好像您没有为MySQL配置SpatialDialect
。您可以验证是否有
hibernate.dialect=org.hibernate.spatial.dialect.mysql.MySQL56SpatialDialect
hibernate.properties
文件中的。
您还可以检查日志以查看Hibernate实际使用的方言。它应该在名称中带有“ Spatial”,以便Spatial功能可用于休眠。