Convert Node to @NodeEntity?

19 views
Skip to first unread message

Zesen Qian

unread,
Jul 28, 2015, 3:54:03 AM7/28/15
to Neo4j
Hello, I 'am using Spring data neo4j to manage the database, but sometimes I get some Node and Relationship from Neo4jTemplate, and I would like to convert these to @NodeEntity and @RelationshipEntity objects. How can I do this?

Michael Hunger

unread,
Jul 28, 2015, 4:31:42 AM7/28/15
to ne...@googlegroups.com
template.projectTo(node, Entity.class)


Am 28.07.2015 um 09:47 schrieb Zesen Qian <qzs...@gmail.com>:

Hello, I 'am using Spring data neo4j to manage the database, but sometimes I get some Node and Relationship from Neo4jTemplate, and I would like to convert these to @NodeEntity and @RelationshipEntity objects. How can I do this?

--
You received this message because you are subscribed to the Google Groups "Neo4j" group.
To unsubscribe from this group and stop receiving emails from it, send an email to neo4j+un...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Zesen Qian

unread,
Jul 28, 2015, 7:03:07 AM7/28/15
to Neo4j, michael...@neotechnology.com
Hello Michael,
Thanks for your reply. I tried this and got the following exception:

Caused by: org.neo4j.graphdb.NotFoundException: '__type__' on http://dorm.riaqn.com:7474/db/data/relationship/856
    at org.neo4j.rest.graphdb.entity.RestEntity.getProperty(RestEntity.java:101)
    at org.springframework.data.neo4j.support.typerepresentation.AbstractIndexBasedTypeRepresentationStrategy.readAliasFrom(AbstractIndexBasedTypeRepresentationStrategy.java:126)
    at org.springframework.data.neo4j.support.mapping.TRSTypeAliasAccessor.readAliasFrom(TRSTypeAliasAccessor.java:36)
    at org.springframework.data.neo4j.support.mapping.TRSTypeAliasAccessor.readAliasFrom(TRSTypeAliasAccessor.java:26)
    at org.springframework.data.convert.DefaultTypeMapper.readType(DefaultTypeMapper.java:102)
    at org.springframework.data.convert.DefaultTypeMapper.getDefaultedTypeToBeUsed(DefaultTypeMapper.java:165)
    at org.springframework.data.convert.DefaultTypeMapper.readType(DefaultTypeMapper.java:142)
    at org.springframework.data.neo4j.support.mapping.Neo4jEntityConverterImpl.read(Neo4jEntityConverterImpl.java:78)
    at org.springframework.data.neo4j.support.mapping.Neo4jEntityPersister$CachedConverter.read(Neo4jEntityPersister.java:170)
    at org.springframework.data.neo4j.support.mapping.Neo4jEntityPersister.createEntityFromState(Neo4jEntityPersister.java:192)
    at org.springframework.data.neo4j.support.mapping.Neo4jEntityPersister.projectTo(Neo4jEntityPersister.java:216)
    at org.springframework.data.neo4j.support.mapping.Neo4jEntityPersister.projectTo(Neo4jEntityPersister.java:206)
    at org.springframework.data.neo4j.support.Neo4jTemplate.projectTo(Neo4jTemplate.java:236)

A simple curl give me the following:
➜  ~  curl http://dorm.riaqn.com:7474/db/data/relationship/856
{
  "extensions" : { },
  "metadata" : {
    "id" : 856,
    "type" : "FOLLOW"
  },
  "property" : "http://dorm.riaqn.com:7474/db/data/relationship/856/properties/{key}",
  "start" : "http://dorm.riaqn.com:7474/db/data/node/333",
  "self" : "http://dorm.riaqn.com:7474/db/data/relationship/856",
  "end" : "http://dorm.riaqn.com:7474/db/data/node/81",
  "type" : "FOLLOW",
  "properties" : "http://dorm.riaqn.com:7474/db/data/relationship/856/properties",
  "data" : { }
}                                                                                                                                      

在 2015年7月28日星期二 UTC+8下午4:31:42,Michael Hunger写道:

Zesen Qian

unread,
Jul 28, 2015, 7:21:47 AM7/28/15
to Neo4j, michael...@neotechnology.com, qzs...@gmail.com
I found the reason of this exception. Because I use template.getorcreaterelationship to create the relation(instead of repository.save), so the property "__type__" is not persisted.
I add __type__=Follow to the properties when creating relationships, and the problem is solved.
Thanks for your help.

在 2015年7月28日星期二 UTC+8下午7:03:07,Zesen Qian写道:

Michael Hunger

unread,
Jul 28, 2015, 6:52:21 PM7/28/15
to Zesen Qian, ne...@googlegroups.com
there is also 
template.postEntityCreation(node, Entity.class) to do that for you

Michael

Zesen Qian

unread,
Jul 28, 2015, 7:39:58 PM7/28/15
to Neo4j, michael...@neotechnology.com
Seems that the method is deprecated and may be removed in the future, and it will introduce overhead to the operation(since it do an extra api query to add the property).

在 2015年7月29日星期三 UTC+8上午6:52:21,Michael Hunger写道:

Michael Hunger

unread,
Jul 29, 2015, 4:38:32 AM7/29/15
to Zesen Qian, ne...@googlegroups.com
it does the right thing depending on the configured type representation strategy.

As I deprecated it it was just to document that it should not be part of the public API
Reply all
Reply to author
Forward
0 new messages