我想找到所有参加过所有指定电影的人,例如,在2部电影中:“终结者”,“真实谎言”
我有以下查询:
MATCH (t:Title)-[:ACTS_IN]-(p:Principal)-[:ACTS_BY]->(n:Name)
WHERE t.originalTitle IN ["The Terminator", "True Lies"]
WITH n, collect(n) as names
WHERE SIZE(names) >= 2
RETURN n.primaryName
如果每个人在每部电影中只参与一次(:ACTS_BY
关系),则效果很好。但是根据我的数据库架构设计,每个人在Principal和Name节点之间可以具有0-N :ACTS_BY
关系(例如,同一个人可以同时是电影的制片人和演员)。
问题是上述提及的Cypher查询也会返回person(Name
节点),以防某人在一部电影中参与2次以上,在另一部电影中参与0次,但是我只需要返回{{1 }}节点,以防该人参加了每部电影。
请帮助改进查询以实现它。
答案 0 :(得分:1)
要解决此问题,您将需要获取@Stateless
public class CustomerHandler {
@PersistenceContext
private EntityManager em;
public Response borrowMovie(List<RequestMovieDto> borrow) {
borrow.forEach(movies -> {
final Movie movie = findMovieByName(movies.getName());
movie.setAvailableCopies((movie.getAvailableCopies()) - movies.getAmount());
em.merge(movie);
});
return Response.ok(borrow).build();
}
public Movie findMovieByName(String name) {
return em.createQuery("SELECT m FROM Movie m "
+ "WHERE m.name = :name", Movie.class)
.setParameter("name", name)
.getSingleResult();
}
}
and this is my test class
@RunWith(MockitoJUnitRunner.class)
public class MovieHandlerTest {
@Mock
EntityManager manager;
private List<RequestMovieDto> request;
@InjectMocks
CustomerHandler handler;
@Before
public void setUp() {
final Movie first = new Movie();
first.setName("MISSION IMPOSSIBLE");
first.setAvailableCopies(10);
first.setIsbn("ABC788900");
manager.persist(first);
final Movie second = new Movie();
first.setName("JAMES BOND");
first.setAvailableCopies(10);
first.setIsbn("ABC788999");
manager.persist(second);
final Movie last = new Movie();
first.setName("HARRY POTTER");
first.setAvailableCopies(10);
first.setIsbn("ABC7882000");
manager.persist(last);
}
@Test
public void borrowMovie() {
RequestMovieDto first = new RequestMovieDto();
first.setName("MISSION IMPOSSIBLE");
first.setAmount(2);
RequestMovieDto second = new RequestMovieDto();
second.setName("JAMES BOND");
second.setAmount(1);
request = Arrays.asList(first, second);
final var response = handler.borrowMovie(request);
assertEquals(Response.Status.OK, response.getStatusInfo().toEnum());
}
}
的不同值以清除重复项,然后再进行计数:
t, p, n