首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >带Hibernate空间自引用的弹簧引导JPA导致循环

带Hibernate空间自引用的弹簧引导JPA导致循环
EN

Stack Overflow用户
提问于 2016-06-08 17:41:10
回答 1查看 750关注 0票数 2

我目前正在编写一个服务,在那里我可以用一些数据存储一个地理空间点。我有一个"dataPoint“类,如下所示:

代码语言:javascript
复制
@Entity
@Table(name = "datapoint")
public class DataPoint {
    @Id
    int dataPoint_id;

    @Column(name = "body")
    String body;

    @Column(name = "location", columnDefinition = "Geometry")
    PGgeometry location;

    @Column(name = "deleted")
    boolean deleted;

    //Getters and Setters...

我试图使用Spring通过API路径向PostGIS数据库添加一个带有一些信息的点。我已经建立了一个控制器,它看起来像这样:

代码语言:javascript
复制
@RestController
@RequestMapping(value = "/dataPoint")
public class DataPointController {

    @Autowired
    private DataPointService myPointService;

    @RequestMapping(value = "/add/{body}/{latitude}/{longitude}/")
    public DataPoint addDataPoint(@PathVariable String body, @PathVariable double latitude, @PathVariable double longitude){
        DataPoint myPoint = new DataPoint();
        myPoint.setBody(body);
        PGgeometry geometry = new PGgeometry();
        try {
            geometry.setValue("POINT("+longitude +" " + latitude+")");
            geometry.setType("POINT");
            // Debugging Stuff
            System.out.println("GEOMETRY VALUE LOOK: {{{{ " + geometry.getValue() + "   " + geometry.getType());
        } catch (SQLException e) {
            e.printStackTrace();
        }
        myPoint.setLocation(geometry);
        myPointService.saveDataPoint(myPoint);
        return myPoint;
    }

它反过来链接到一个DataPointService,它只是充当控制器之间的中间角色,saveDataPoint()看起来是这样的:

代码语言:javascript
复制
public void saveDataPoint(DataPoint myPoint) {
    dataPointRepository.save(myPoint);
}

以及DataPointRepository,它看起来是这样的:

代码语言:javascript
复制
@Repository
public interface DataPointRepository extends JpaRepository<DataPoint, Integer> {
}

但是,当我访问add链接时,会得到以下错误:

代码语言:javascript
复制
Failed to write HTTP message: org.springframework.http.converter.HttpMessageNotWritableException: Could not write content: Direct self-reference leading to cycle (through reference chain: com.testing.model.DataPoint["location"]->org.postgis.PGgeometry["geometry"]->org.postgis.Point["firstPoint"]); nested exception is com.fasterxml.jackson.databind.JsonMappingException: Direct self-reference leading to cycle (through reference chain: com.testing.model.DataPoint["location"]->org.postgis.PGgeometry["geometry"]->org.postgis.Point["firstPoint"])

我在一些例子中看到了@JsonBackReference及其对偶的用法,然而,在实体被来回链接的情况下,它被使用了,我在这里没有看到这种情况,事实上,错误似乎并不是循环的,那么这里发生了什么呢?

EN

回答 1

Stack Overflow用户

发布于 2022-02-10 19:47:30

我遇到了同样的问题。它是循环的,因为Point有一个字段firstPoint,它再次引用Point。我能够通过安装postgis-geojson:https://jitpack.io/p/stephenbrough/postgis-geojson来解决这个问题。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37709340

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档