简体   繁体   English

Spring Boot JPA with Hibernate 空间自参考导致循环

[英]Spring Boot JPA with Hibernate Spatial self-reference leading to cycle

I am currently writing a service where I can store a geospatial point with some data.我目前正在编写一项服务,我可以在其中存储带有一些数据的地理空间点。 I have a "dataPoint" class that looks like this:我有一个“数据点”class,看起来像这样:

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

I am trying to use Spring Boot to simply add a point with some information to a PostGIS database via an API path.我正在尝试使用 Spring Boot 通过 API 路径简单地将带有一些信息的点添加到 PostGIS 数据库。 I have built a controller that looks like this:我构建了一个 controller,如下所示:

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

Which is in turn linked to a DataPointService which just acts as a middle man between the controller where saveDataPoint() looks like this:它又链接到一个DataPointService ,它只是充当 controller 之间的中间人,其中saveDataPoint()如下所示:

public void saveDataPoint(DataPoint myPoint) {
    dataPointRepository.save(myPoint);
}

and the DataPointRepository , which looks like this:DataPointRepository ,它看起来像这样:

@Repository
public interface DataPointRepository extends JpaRepository<DataPoint, Integer> {
}

However, when I visit my add link, I get this error:但是,当我访问我的添加链接时,出现此错误:

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"])

I have seen the @JsonBackReference and its dual used in some examples, however, that has been used in situations where entities are being linked back and forth, which I do not see happening here, in fact, the error does not even seem to be cyclic, so what is happening here?我已经在某些示例中看到了@JsonBackReference及其双重使用,但是,它已用于实体来回链接的情况,我在这里看不到这种情况,事实上,错误似乎甚至没有循环,所以这里发生了什么?

I ran into the same issue.我遇到了同样的问题。 It's cyclic because Point has a field firstPoint that reference to Point again.它是循环的,因为 Point 有一个字段 firstPoint 再次引用 Point。 I was able to resolve the problem by installing this postgis-geojson: https://jitpack.io/p/stephenbrough/postgis-geojson我能够通过安装此 postgis-geojson 来解决问题: https://jitpack.io/p/stephenbrough/postgis-geojson

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM