在Spring Data neo4j中使用RelationshipEntity保存Node的正确方法是什么?

时间:2013-08-23 07:16:19

标签: java spring spring-data-neo4j

实际上,我试图用RelationshipEntity类保存一个节点,如下所示:

  1. 节点类

    @NodeEntity  
    public class MyEvent  
    {  
         @GraphId  
         private Long nodeId;  
         @RelatedToVia(type = "INVITED_TO")  
         @Fetch Set<EventResponse> eventResponse;  
    }
  2. RelationshipEntity Class

    @RelationshipEntity(type="INVITED_TO")  
    public class EventResponse  implements Serializable   
    {  
       @GraphId  
       Long nodeId;  
    
       @StartNode  
       MyEvent event;  
    
       @EndNode  
       User user;  
    
      //  .....  
    

    }

  3. 当我尝试保存MyEvent

    org.neo4j.graphdb.NotFoundException: '__type__' property not found for RelationshipImpl #153 of type 15 between Node[159] and Node[117].
    

    所以我猜基于上面的异常,它缺少识别任何节点类型所需的__type__属性。我没有完全得到,有没有什么方法可以保存第一个关系实体,然后是NodeEntity或反之亦然?或者

1 个答案:

答案 0 :(得分:3)

您不需要明确设置类型属性。它由spring-data-neo4j管理。

以下代码段对我有用:

事件类:

@NodeEntity
public class MyEvent
    {

    @GraphId
    private Long       nodeId;

    @RelatedToVia(type = "INVITED_TO")
    @Fetch
    Set<EventResponse> eventResponse;

    }

用户类:

@NodeEntity
public class User
    {

    @GraphId
    private Long userId;  

    }

响应类:

@RelationshipEntity(type = "INVITED_TO")
public class EventResponse
    {

    @GraphId
    private Long    nodeId;

    @StartNode
    MyEvent event;

    @EndNode
    User    user;

    }

我在短期的junit测试中使用它们:

@Autowired
    private Neo4jTemplate template;

    @Transactional
    @Test
    public void saveEvent()
        {

        User user = new User();
        MyEvent event = new MyEvent();

        EventResponse response = new EventResponse();
        response.user = user;
        response.event = event;

        this.template.save(user);
        this.template.save(event);
        this.template.save(response);

        }