2015-04-28 67 views
1

我不知道爲什麼我無法獲得movie字段的值。我調試了它,發現movie爲空。我張貼了一張照片,你會看到它enter image description here。請給我一些建議。我確信我已經成功建立了這種關係,可能會在某個地方出現錯誤的建議。類或密碼?意外的空值,neo4j

public interface MovieRepository extends GraphRepository<Movie> { 


@Query("match (user:User {login: {0}})-[r:RATED]->(movie)<-[r2:RATED]-(other)-[r3:RATED]->(otherMovie) " 
     + " where r.stars >= 3 and r2.stars >= r.stars and r3.stars >= r.stars " 
     + " with otherMovie, avg(r3.stars) as rating, count(*) as cnt" 
     + " order by rating desc, cnt desc" 
     + " return otherMovie as movie, rating limit 10") 
List<MovieRecommendation> getRecommendations(String login); 

} 

recommend.class

@QueryResult 
public class MovieRecommendation { 


Movie movie; 
int rating; 
public Movie getMovie() { 
    return movie; 
} 
public void setMovie(Movie movie) { 
    this.movie = movie; 
} 
public int getRating() { 
    return rating; 
} 
public void setRating(int rating) { 
    this.rating = rating; 
    } 
} 

控制器

@RequestMapping(value = "/user", method = RequestMethod.GET) 
public String profile(Model model, HttpServletRequest request) { 

    HttpSession session = request.getSession(false); 
    User user = (User) session.getAttribute("user"); 
    model.addAttribute("user", user); 

    if (user != null) { 
     List<MovieRecommendation> mr = movieRepository.getRecommendations(user.getLogin()); 
    MovieRecommendation movie = new MovieRecommendation(); 
    Movie m = new Movie(); 
    m.setTitle("AA"); 
    movie.setMovie(m); 
    mr.add(movie); 
    model.addAttribute("recommendations", mr); 
    } 
    return "user/index"; 
} 

運行CYPHER使用的Neo4j社區

match (user:ollie)-[r:RATED]->(movie)<-[r2:RATED]-(other)-[r3:RATED]->(otherMovie) 
where r.stars >= 1 and r2.stars >= r.stars and r3.stars >= r.stars 
with otherMovie, avg(r3.stars) as rating, count(*) as cnt 
order by rating desc, cnt desc 
return otherMovie limit 10 
+0

嗨,歐泊......你有什麼想法嗎? – yang

+0

您需要弄清楚如何創建MovieRecommendation實例。無論誰創建它,都不會調用setMovie或用null來調用它。在'setMovie'方法和'Movie'構造函數上放置一個斷點。難道你的數據庫中有推薦記錄可以鏈接到不存在的電影嗎?你使用外鍵約束嗎? – Arkadiy

+0

當您手動運行查詢時,您可以分享您的實際查詢結果嗎? –

回答

0

我認爲你的查詢返回null。

所以第一個元素[0]有一個電影是空...

電影「AA」應該是[1]在您的陣列,因爲你使查詢和之後,你追加推薦

+0

但我可以得到電影的金額,AA是我執行密碼後添加的,可能我無法解決我得到的元素? – yang