Creating dynamic relation names with Java

(Martin Lewis Gr) #1

I'm trying to create relations between already created nodes. I'm able to create normal relations (fixed names) without any problems but I'm not able to create relation names dynamically.

I explain:

I'm trying to replicate the model explained in this interesting website:

  1. I create Airports Nodes (success):"CREATE (a: Airport {airportCode: {airportCode}, countryName: {countryName}, cityName: {cityName}})", parameters("airportCode", airportCode, "countryName", countryName, "cityName", cityName));

  2. I create AirportDay Nodes (success):"CREATE (ad: AirportDay {Code: {Code}, AirportCode: {AirportCode}, DateCode: {DateCode}})", parameters("Code", code, "AirportCode", airportCode, "DateCode", dateCode));

  3. I create Leg (or Flight) Nodes (success)"CREATE (l: Leg {LegCode: {LegCode}, Origin: {Origin}, Destination: {Destination}, LegFlightNum: {LegFlightNum}, FlightNum: {FlightNum}, DepartureDate: {DepartureDate}, DepartureTime: {DepartureTime}, DepartureDateTime: {DepartureDateTime}, ArrivalDate: {ArrivalDate}, ArrivalTime: {ArrivalTime}, ArrivalDateTime: {ArrivalDateTime} })", parameters("LegCode", legCode, "Origin", origin, "Destination", destination, "LegFlightNum", legFlightNum, "FlightNum", flightNum, "DepartureDate", departureDate, "DepartureTime", departureTime, "DepartureDateTime", departureDateTime, "ArrivalDate", arrivalDate, "ArrivalTime", arrivalTime, "ArrivalDateTime", arrivalDateTime));

  4. I link correctly Airport & AirportDay (success):"MATCH (a:Airport), (ad:AirportDay) WHERE a.airportCode = ad.AirportCode MERGE (a)-[:HAS_DAY]->(ad)");

  5. I'm trying to link AirportDay & Leg but this way, relation names are not dynamic (I would like to have the name of the destination airport +_FLIGHT instead of HAS_FLIGHT):"MATCH (ad:AirportDay), (l:Leg) WHERE ad.DateCode = l.DepartureDate MERGE (ad)-[:HAS_FLIGHT]->(l)");

My problem is that the code from this website gives an example with an embedded Neo4j database, here is the code of this website:
departureAirportDayNode.createRelationshipTo(leg, RelationshipType.withName(arrivalCity + "_FLIGHT"));

But in my case, I'm using the Neo4J Java Driver and I found no example on how to deal with my problem.

I understand that I need to build the name of the relation dynamically but how to do this... ?

Any help will be appreciated,
(I'm a beginner with Neo4J and not an expert in Java...)
Don't hesitate if any information is missing.

(Stefan Armbruster) #2

Cypher on its own cannot use dynamic relationship types. However the apoc library solves this by call apoc.create.relationship(from, type, props, to)

(Michael Hunger) #3

if you use the java API you can also create relationships with:

Relationship rel = node1.createRelationshipTo(node2, RelationshipType.withName(relTypeName)

(Martin Lewis Gr) #4

@stefan: Many thanks for these useful information. I will see if I can use APOC library.
@michael: Not sure what is the difference between using Java Driver code and Java API. I will read about this before commenting. Big thanks for your help.

(Michael Hunger) #5

Driver is client side in your own application.

Java API is if you write a procedure or server extension.