Create a Data Marvel — Part 7: Connecting the Graph

The ComicIssue class

package com.example.demo.comicissue;import ...@Data
@NoArgsConstructor
@RequiredArgsConstructor
@NodeEntity
public class ComicIssue {
@Id @GeneratedValue
private Long neoId;
@NonNull
private Long id;
@NonNull
private Integer pageCount;
@NonNull
private Double issueNumber;
@NonNull
private String name, thumbnail, resourceURI;
@Relationship(type = “INCLUDES”)
private List<Character> characters = new ArrayList<>();
@Relationship(type = “CREATED_BY”)
private List<Creator> creators = new ArrayList<>();
@Relationship(type = “PART_OF”)
private List<Event> events = new ArrayList<>();
@Relationship(type = “BELONGS_TO”)
private List<Series> series = new ArrayList<>();
@Relationship(type = “MADE_OF”)
private List<Story> stories = new ArrayList<>();
public List<Character> getCharacters() { return characters; }
public List<Creator> getCreators() { return creators; }
public List<Event> getEvents() { return events; }
public List<Series> getSeries() { return series; }
public List<Story> getStories() { return stories; }
}
@Relationship(type = “INCLUDES”)
private List<Character> characters = new ArrayList<>();
public List<Character> getCharacters() { return characters; }

The repository class

package com.example.demo.comicissue;import ...public interface ComicIssueRepo extends Neo4jRepository<ComicIssue, Long> {
ComicIssue findByName(@Param(“name”) String name);
Iterable<ComicIssue> findByNameLike(@Param(“name”) String name); @Query(“MATCH (i:ComicIssue)-[r]-(n) RETURN i,r,n LIMIT {limit}”)
Collection<ComicIssue> graph(@Param(“limit”) int limit);
}
@Query(“MATCH (i:ComicIssue)-[r]-(n) RETURN i,r,n LIMIT {limit}”)
Collection<ComicIssue> graph(@Param(“limit”) int limit);

What I Learned

  1. Much of the code mirrored what we did with other classes, so I could focus on the new piece — the relationships between entities. Understanding what annotations did what and which arguments were needed or unnecessary took some research and some testing. Your use case and data will not be exactly what someone else has, so you will also need to experiment a bit to ensure you don’t have any useless code. You cannot rely simply on copy/paste (nor do you probably want to).
  2. This project was a bit trickier than other existing code examples out there because we had so many entities that all connected to each other (most examples have 2 or 3 entities, ours has 6). However, this just prepares us for the variety of complex data sets in real-world scenarios. Real data will not be simple or consistently-organized, so projects like this are more applicable to real-world data scenarios.

Next Steps

Resources

--

--

Developer Content around Graph Databases, Neo4j, Cypher, Data Science, Graph Analytics, GraphQL and more.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Jennifer Reif

Jennifer Reif is an avid developer and problem-solver. She enjoys learning new technologies, sometimes on a daily basis! Her Twitter handle is @JMHReif.