Create a link between two nodes via OGM

Hi everyone,
I've been trying to establish a connection between spring boot and neo4j via the concept of using an OGM. I can fetch data from the so-called "Movie database," yet I couldn't find any "create" methods to build a new movie. Does any exist? How do you usually create such connections without writing a query of Cypher?

and with this you mean Spring Data Neo4j?
Just to get you the right answer.

Yes, exactly! I can even explain the situation deeper:
We've been trying to use neo4j with spring only by creating queries inside a query builder. For instance, we injected a session from the Neo4j driver class into the repository class and ran the following method to create a link between two nodes:

public void createLink(String index1, String index2, String type) { session.run("CREATE (n:source {index: '" + index1 + "'}) - [r:" + type + "] -> (m:target {index:'" + index2 + "'})"); }

Now we want to increase the readability of our code by using an OGM instead. I have explored several manuals, yet I couldn't approach anything more than a Node class (@Node). Could you please tell me whether there's a way to model the links similarly? For example, consider using the save() method taking two input parameters to find the source and target nodes and establish a link of another given type.

The best strategy would be to read essential parts up in the documentation first. SDN object mapping
Of course this reference is also good for a deeper dive into the concepts and more advanced topics.

To get you started with your case:

First of all you would need, as you have already learned, a definition of @Nodes

@Node("source") // if you want to have it lower-case
public class Source {
  @Id @GeneratedValue
  private Long id;

  @Relationship("REL_TYPE_NAME")
  private Target target; // or List<Target>

  private String index;
}
@Node("target") // if you want to have it lower-case
public class Target {
  @Id @GeneratedValue
  private Long id;

  private String index;
}

then you would typically create a repository for the an entity:

public interface SourceRepository extends Neo4jRepository<Source, Long> {}

Then you could create an instance of each, connect them and save.

class MyServiceClass { // just to have some kind of named capsulation 

// we need to get the repository injected/autowired here
private final SourceRepository repository;

@Autowired
public MyServiceClass(SourceRepository repository) {
  this.repository = repository;
}

// for new source and target
public void createLink(String index1, String index2) {
  Source source = new Source();
  source.setIndex(index1);
  Target target = new Target();
  target.setIndex(index2);
  source.setTarget(target);

  repository.save(source);
}
}

if the nodes already exists, you could also fetch them via the repository first and create the relationship. But for this you would need to also define a source repository and define a method to query for known properties. Your SourceRepository would become:

public interface SourceRepository extends Neo4jRepository<Source, Long> {
  Source findByIndex(@Param("index") String index);
}

and the method would be

public void createLink(String index1, String index2) {
  Source source = sourceRepository.findByIndex(index1);
  Target target = targetRepository.findByIndex(index2);
  source.setTarget(target);

  repository.save(source);
}

Hope this helps you for your first steps in the mapping world.
(Disclaimer: I typed all code here without syntax check etc. so there might be typos)