关系未保存在SDN 6中(Spring Data neo4j)

qoefvg9y  于 2022-11-05  发布在  Spring
关注(0)|答案(2)|浏览(168)

Hello在保存与NEO4J和Spring的关系时遇到问题,当我不使用@RelationProperties的类时,我可以保存关系,但每当我想使用该类时,甚至没有创建关系。
我在Docker容器中使用Neo4j 4.3.7-社区版,在我的pom.xml中,我有

<dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-neo4j</artifactId>
            <version>6.1.5</version>
        </dependency>

我想做类似Image的事情
我有一段代码:
起始节点

...
    @Relationship(type = "VISITS",direction = Relationship.Direction.OUTGOING)
    public Set<Visits> visits;

    public void visitPOI(Visits visit) {
        if(visits == null) {
            visits = new HashSet<>();
        }
        visits.add(visit);
    }
...

我的亲戚

@RelationshipProperties
public class Visits {
    @Id @GeneratedValue
    private long id;

    @Property private String description;

    @TargetNode private PointOfInterest poi;

    public Visits(String description, PointOfInterest p){
        this.description = description;
        this.poi = p;
    }
...

并且目的节点

...
@Node
public class PointOfInterest {

    @Id @GeneratedValue(generatorClass = UUIDStringGenerator.class)
    private String id;

    @Property private String name;
    @Property private double lat;
    @Property private double lng;

    public PointOfInterest() { }

//getters and setters
...

而这在一个服务

...
    public void saveRelation(String username, Visits v ){

        User u = userRepository.findByUserName(username);

        Optional<PointOfInterest> poi = pointOfInterestRepository.findById(v.getPoi().getId());
        v.setPoi(poi.get());
        u.visitPOI(v);
        userRepository.save(u);
    }
...

和我发布的JSON

{
    "description":"Nice trip",
    "poi":{
        "id":"Uster"
    }
}
s4n0splo

s4n0splo1#

@RelationshipProperties的id字段上使用@RelationshipId,如下所示

@RelationshipProperties
public class Visits {
    @RelationshipId
    private long id;

    @Property private String description;
    .....
}
5q4ezhmt

5q4ezhmt2#

我在使用@RelationshipProperty注解类持久化关系属性时遇到了同样的问题。最后,我发现这个问题是由于long和Long之间的隐式转换造成的。在@RelationshipProperty注解类中将@RelationshipId private long id;更改为@RelationshipId private Long id;将解决这个问题。这似乎是SDN中的一个bug,它应该报告一个错误。

相关问题