Hi Tim, SDN uses the internal Neo4j node or relationship ID to track domain entities. This is the same as <id> in the Neo4j browser.
The @GraphId <http://docs.spring.io/spring-data/neo4j/docs/4.1.0.RC1/reference/html/#__graphid_neo4j_id_field> annotation is used to indicate which java.lang.Long field this Neo4j node or relationship ID should map to. The annotation is not required- if you have a Long field named id, SDN will use it as the graph id. Since this is an internal id, user code should never set it (Entity.java in the sdn university example unfortunately has a setter, and I've made a note to remove it). When you have your own id, then that is simply a property on your entity. Note that it does not replace the @GraphId. Your Integer contentId can thus be represented in two ways- 1. As a property with key "contentId" on the node: @GraphId Long id; //still the internal node id, @GraphId annotation is optional Integer contentId; //a property on the node 2. As a property with key "id" on the node: @GraphId Long internalId; //the internal id is still required, and now must be annotated since it is no longer called id Integer id; //a property on the node To retrieve entities by content id rather than internal node id, the simplest thing would be to use a derived finder in the repository: findByContentId (case #1 above) or findById (case #2 above) The FIXME is rather outdated (I'll get that removed or updated as well), but what it should be saying is that when you override equals()/hashCode(), be aware that the @GraphId property can be null. Entities not yet persisted will not have an id assigned to them yet. SDN however, does not depend on the implementation of equals or hashCode <http://docs.spring.io/spring-data/neo4j/docs/4.1.0.RC1/reference/html/#_entity_equality> . As for the JSOGGenerator, Vince has a nice explanation here <https://github.com/neo4j/neo4j-ogm/issues/100#issuecomment-171585354> @NodeEntity annotations are optional, which is why you don't see them in the example project. If a @NodeEntity isn't specified, SDN will use the simple class name of the entity as the node label. Recommend that you specify the label always so that you can safely refactor entities without having a large impact on the underlying graph. Finally, the loading of Jon- the 'id' is indeed the internal node id. Hope this helps. Regards Luanne On Fri, Mar 18, 2016 at 6:39 AM, Tim Colson <[email protected]> wrote: > I feel like this is a basic concept, but I'm still not grokking how SpringData > Neo4j 4.1 <https://github.com/spring-projects/spring-data-neo4j> works > with Entity IDs. > I also am confused on how I should work with natural IDs for my domain > objects. > > Background: I can run the sdn4-university example > <https://github.com/neo4j-examples/sdn4-university/tree/4.1>, and I > created a working Spring Boot app with two of my own domain objects based > on the sdn-uni classes (no Angular), but do have relationships and do save > to Neo4j. yay! > > I heard loud and clear at GraphConnect to leave node/graph IDs alone, but > how do they related to the Entity "id" property, and the "<id>" shown in > Neo4j for each node? How does the JSOGGenerator fit in here? Maybe there is > more info to share about that curious FIXME message that will help me > understand? > > > @JsonIdentityInfo(generator=JSOGGenerator.class) > public abstract class Entity { > > @JsonProperty("id") > private Long id; > > > /** > * FIXME: > * This is the default mechanism for providing entity identity to the OGM > * > * It is required because the OGM can currently accept objects with NO > * id value set. This is a restriction that must be changed > * > * @param o the object to compare, either or both may not yet be persisted. > * @return > */ > @Override > public boolean equals(Object o) { > if (this == o) return true; > if (o == null || id == null || getClass() != o.getClass()) return false; > > Entity entity = (Entity) o; > > if (!id.equals(entity.id)) return false; > > return true; > } > > > > > FYI - I'm loading wiki space and a hierarchy of page objects for analysis > into Neo4j. Each page has a unique integer ContentID that I want to use to > store/retrieve the pages. Spaces have a unique string "key" that I want to > use to store & retrieve them. > > Puzzle #2: none of the domain objects in sdn4-university have @NodeEntity > annotations as shown on the SpringData Neo4j 4.1 > <https://github.com/spring-projects/spring-data-neo4j> readme. That page > also shows loading "Jon" by "id" - but doesn't show where we got "id". > > I'm starting to think "id" != "node id" and that I need to implement my > own id property in Entity or sub-classes? > > Thanks for explanation/feedback. :) > -Tim > > -- > 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 [email protected]. > For more options, visit https://groups.google.com/d/optout. > -- 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 [email protected]. For more options, visit https://groups.google.com/d/optout.
