I have a problem about saving route with City in Spring Boot. I think my issue is located at CityRepository and RouteRepository.
public interface CityRepository extends Neo4jRepository<City,UUID> {
@Query("MATCH (city:City) OPTIONAL MATCH (city)-\[r:ROUTES\]->(route:Route) RETURN city, collect(r), collect(route)")
List<City> listAll();
@Query("MATCH (city:City {id: $cityId}) OPTIONAL MATCH (city)-\[r:ROUTES\]->(route:Route) RETURN city, collect(r), collect(route)")
City getById(UUID cityId);
@Query("MATCH (city:City {name: $cityName}) RETURN city")
City getByCityName(String cityName);
@Query("CREATE (city:City {id: randomUUID(), name: $cityName}) RETURN city")
City saveCity(String cityName);
@Query("MATCH (city:City {id: $cityId}) SET city.name = $cityName RETURN city")
City updateCity(UUID cityId, String cityName);
@Query("MATCH (city:City {id: $cityId}) DELETE city")
void deleteCity(UUID cityId);
}
public interface RouteRepository extends Neo4jRepository<Route,UUID> {
@Query("MATCH (city:City {id: $cityId})-\[:ROUTES\]->(route:Route) RETURN route")
List<Route> listAllByCityId(UUID cityId);
@Query("MATCH (route:Route {id: $routeId}) RETURN route")
Route getById(UUID routeId);
@Query("CREATE (city:City {id: $cityId})-\[:ROUTES\]->(route:Route {id: randomUUID(), from: $from, destination: $destination, departureTime: $departureTime," +
"arriveTime: $arriveTime, duration: $duration}) " +
"RETURN route")
Route saveRoute(UUID cityId, String from, String destination, String departureTime,
String arriveTime, double duration);
@Query("MATCH (city:City {id: $cityId})-\[:ROUTES\]->(route:Route {id: $routeId}) " +
"SET route.from = $from, route.destination = $destination,route.departureTime = $departureTime," +
"route.arriveTime = $arriveTime, route.duration = $duration RETURN route")
Route updateRoute(UUID cityId, UUID routeId, String from, String destination,String departureTime,
String arriveTime,double duration);
@Query("MATCH (city:City {id: $cityId})-\[r:ROUTES\]->(route:Route {id: $routeId}) DELETE r, route")
void deleteRoute(UUID cityId, UUID routeId);
}
After calling saveRoute of RouteRepository and listAll of CityRepository , I get this result shown below.
[
{
"id": "2c83b1c1-7f62-4584-b271-d40986ea6414",
"name": "London",
"routes": \[\]
},
{
"id": "2c83b1c1-7f62-4584-b271-d40986ea6414",
"name": null,
"routes": \[
{
"id": "8c694742-8311-4b1b-a8b6-0724b019d002",
"from": "London",
"destination": "Berlin",
"departureTime": "9:00",
"arriveTime": "11:30",
"duration": 2.5
}
\]
}
]
Here is my stackoverflow question : Question Link
Here is my github repository : Project Link