Spring 弹簧数据neo4j,我可以';我不能解决它
我使用spring数据neo4j,我有Spring 弹簧数据neo4j,我可以';我不能解决它,spring,neo4j,spring-data-neo4j,Spring,Neo4j,Spring Data Neo4j,我使用spring数据neo4j,我有user.class.movie.class,rating.class,我创建了电影和评级之间的关系,当我运行程序时,我可以获得评级(明星,评论),但无法获得电影的标题(null) 电影节 package com.oberon.fm.domain; @NodeEntity public class Movie { @GraphId Long nodeId; @Indexed(indexType = IndexType.FULLTEXT, indexName
user.class.movie.class,rating.class
,我创建了电影和评级之间的关系,当我运行程序时,我可以获得评级(明星,评论),但无法获得电影的标题(null)
电影节
package com.oberon.fm.domain;
@NodeEntity
public class Movie {
@GraphId
Long nodeId;
@Indexed(indexType = IndexType.FULLTEXT, indexName = "id")
String id;
@Indexed(indexType = IndexType.FULLTEXT, indexName = "search", numeric = false)
String title;
String description;
@RelatedTo(type = "DIRECTED", direction = INCOMING)
Person director;
@RelatedTo(type = "ACTS_IN", direction = INCOMING)
Set<Person> actors;
@RelatedToVia(elementClass = Role.class, type = "ACTS_IN", direction = INCOMING)
// Iterable<Role> roles;
Set<Role> roles = new HashSet<>();
@RelatedToVia(elementClass = Rating.class, type = "RATED", direction = INCOMING)
@Fetch
Iterable<Rating> ratings;
// Set<Rating> ratings = new HashSet<>();
private String language;
private String imdbId;
private String tagline;
private Date releaseDate;
private Integer runtime;
private String homepage;
private String trailer;
private String genre;
private String studio;
private Integer version;
private Date lastModified;
private String imageUrl;
public Movie() {
}
public Long getNodeId() {
return nodeId;
}
public void setNodeId(Long nodeId) {
this.nodeId = nodeId;
}
public Movie(String id, String title) {
this.id = id;
this.title = title;
}
public Collection<Person> getActors() {
return actors;
}
public Collection<Role> getRoles() {
return IteratorUtil.asCollection(roles);
}
public int getYear() {
if (releaseDate == null)
return 0;
Calendar cal = Calendar.getInstance();
cal.setTime(releaseDate);
return cal.get(Calendar.YEAR);
}
public String getId() {
return id;
}
public String getTitle() {
return title;
}
@Override
public String toString() {
return String.format("%s (%s) [%s]", title, releaseDate, id);
}
public String getDescription() {
return description;
}
public int getStars() {
Iterable<Rating> allRatings = ratings;
if (allRatings == null)
return 0;
int stars = 0, count = 0;
for (Rating rating : allRatings) {
stars += rating.getStars();
count++;
}
return count == 0 ? 0 : stars / count;
}
public Collection<Rating> getRatings() {
Iterable<Rating> allRatings = ratings;
return allRatings == null ? Collections.<Rating> emptyList()
: IteratorUtil.asCollection(allRatings);
}
/*
* public Set<Rating> getRatings() { return ratings; }
*/
public void setRatings(Set<Rating> ratings) {
this.ratings = ratings;
}
/*
* public void addRating(Rating rating) { ratings.add(rating); }
*/
public void setTitle(String title) {
this.title = title;
}
public void setLanguage(String language) {
this.language = language;
}
public void setImdbId(String imdbId) {
this.imdbId = imdbId;
}
public void setTagline(String tagline) {
this.tagline = tagline;
}
public void setDescription(String description) {
this.description = description;
}
public void setReleaseDate(Date releaseDate) {
this.releaseDate = releaseDate;
}
public void setRuntime(Integer runtime) {
this.runtime = runtime;
}
public void setHomepage(String homepage) {
this.homepage = homepage;
}
public void setTrailer(String trailer) {
this.trailer = trailer;
}
public void setGenre(String genre) {
this.genre = genre;
}
public void setStudio(String studio) {
this.studio = studio;
}
public void setVersion(Integer version) {
this.version = version;
}
public void setLastModified(Date lastModified) {
this.lastModified = lastModified;
}
public void setImageUrl(String imageUrl) {
this.imageUrl = imageUrl;
}
public String getLanguage() {
return language;
}
public String getImdbId() {
return imdbId;
}
public String getTagline() {
return tagline;
}
public Date getReleaseDate() {
return releaseDate;
}
public Integer getRuntime() {
return runtime;
}
public String getHomepage() {
return homepage;
}
public String getTrailer() {
return trailer;
}
public String getGenre() {
return genre;
}
public String getStudio() {
return studio;
}
public Integer getVersion() {
return version;
}
public Date getLastModified() {
return lastModified;
}
public String getImageUrl() {
return imageUrl;
}
public String getYoutubeId() {
String trailerUrl = trailer;
if (trailerUrl == null || !trailerUrl.contains("youtu"))
return null;
String[] parts = trailerUrl.split("[=/]");
int numberOfParts = parts.length;
return numberOfParts > 0 ? parts[numberOfParts - 1] : null;
}
@Override
public boolean equals(Object o) {
if (this == o)
return true;
if (o == null || getClass() != o.getClass())
return false;
Movie movie = (Movie) o;
if (nodeId == null)
return super.equals(o);
return nodeId.equals(movie.nodeId);
}
@Override
public int hashCode() {
return nodeId != null ? nodeId.hashCode() : super.hashCode();
}
public Person getDirector() {
return director;
}
package com.oberon.fm.domain;
@节点性
公映{
@格拉希德
长节;
@索引(indexType=indexType.FULLTEXT,indexName=“id”)
字符串id;
@索引(indexType=indexType.FULLTEXT,indexName=“search”,numeric=false)
字符串标题;
字符串描述;
@与(type=“DIRECTED”,direction=传入)相关
个人董事;
@与(type=“ACTS\u IN”,方向=传入)相关
设置演员;
@RelatedToVia(elementClass=Role.class,type=“ACTS\u IN”,direction=INCOMING)
//可替代角色;
Set roles=new HashSet();
@RelatedToVia(elementClass=Rating.class,type=“RATED”,方向=传入)
@取回
可接受评级;
//Set ratings=new HashSet();
私有字符串语言;
私有字符串imdbId;
私有字符串标语;
私人日期发布日期;
私有整数运行时;
私人字符串主页;
私家拖车;
私人弦乐体裁;
私人弦乐工作室;
私有整数版本;
上次修改的私人日期;
私有字符串imageUrl;
公共电影(){
}
公共长getNodeId(){
返回节点ID;
}
公共无效setNodeId(长nodeId){
this.nodeId=nodeId;
}
公共电影(字符串id、字符串标题){
this.id=id;
this.title=标题;
}
公共集合getActors(){
返回演员;
}
公共集合getRoles(){
返回IteratorUtil.asCollection(角色);
}
公共int getYear(){
if(releaseDate==null)
返回0;
Calendar cal=Calendar.getInstance();
校准设置时间(发布日期);
返回cal.get(日历年);
}
公共字符串getId(){
返回id;
}
公共字符串getTitle(){
返回标题;
}
@凌驾
公共字符串toString(){
返回字符串。格式(“%s(%s)[%s]”,标题,发布日期,id);
}
公共字符串getDescription(){
返回说明;
}
公共int getStars(){
Iterable allRatings=评级;
如果(allRatings==null)
返回0;
整数星=0,计数=0;
对于(评级:所有评级){
stars+=评级。getStars();
计数++;
}
返回计数==0?0:星/计数;
}
公共收藏{
Iterable allRatings=评级;
return allRatings==null?Collections.emptyList()
:IteratorUtil.asCollection(所有评级);
}
/*
*public Set getRatings(){return ratings;}
*/
公共无效设置评级(设置评级){
这个。评级=评级;
}
/*
*public void addRating(评级){ratings.add(评级);}
*/
公共无效集合标题(字符串标题){
this.title=标题;
}
公共语言(字符串语言){
这种语言=语言;
}
public void setImdbId(字符串imdbId){
this.imdbId=imdbId;
}
公共无效设置标记行(字符串标记行){
this.tagline=tagline;
}
公共void集合描述(字符串描述){
this.description=描述;
}
公共作废setReleaseDate(日期releaseDate){
this.releaseDate=releaseDate;
}
公共void setRuntime(整数运行时){
this.runtime=运行时;
}
公共主页(字符串主页){
this.homepage=主页;
}
公共拖车(字符串拖车){
this.trail=拖车;
}
公共类型(字符串类型){
这个。流派=流派;
}
公共void setStudio(字符串工作室){
this.studio=studio;
}
公共void setVersion(整数版本){
this.version=版本;
}
公共作废setLastModified(日期lastModified){
this.lastModified=lastModified;
}
public void setImageUrl(字符串imageUrl){
this.imageUrl=imageUrl;
}
公共字符串getLanguage(){
返回语言;
}
公共字符串getImdbId(){
返回imdbId;
}
公共字符串getTagline(){
回归口号;
}
公共日期getReleaseDate(){
返回发布日期;
}
公共整数getRuntime(){
返回运行时;
}
公共字符串getHomepage(){
返回主页;
}
公共字符串getTrailer(){
返回拖车;
}
公共字符串getgreen(){
回归体裁;
}
公共字符串getStudio(){
回归演播室;
}
公共整数getVersion(){
返回版本;
}
公共日期getLastModified(){
返回最后修改;
}
公共字符串getImageUrl(){
返回imageUrl;
}
公共字符串getYoutubeId(){
字符串trailerUrl=拖车;
如果(trailerUrl==null | |!trailerUrl.contains(“youtu”))
返回null;
字符串[]部分=trailerUrl.split([=/]);
int numberOfParts=parts.length;
返回numberOfParts>0?零件[numberOfParts-1]:空;
}
@凌驾
公共布尔等于(对象o){
if(this==o)
返回true;
如果(o==null | | getClass()!=o.getClass())
返回false;
电影=(电影)o;
if(nodeId==null)
返回super.equals(o);
返回nodeId.equals(movie.nodeId);
}
@凌驾
公共int hashCode(){
return nodeId!=null?nodeId.hashCode():super.hashCode();
}
公众人士(董事){
返回董事;
}
}
用户类
package com.oberon.fm.domain;
@NodeEntity
public class User {
@GraphId
Long nodeId;
public static final String SALT = "cewuiqwzie";
public static final String FRIEND = "FRIEND";
public static final String RATED = "RATED";
@Indexed(indexType = IndexType.FULLTEXT, indexName = "login")
String login;
@Indexed
String name;
String password;
public void setPassword(String password) {
this.password = password;
}
String info;
private Roles[] roles;
public User() {
}
public User(String login, String name, String password, Roles... roles) {
this.login = login;
this.name = name;
this.password = encode(password);
this.roles = roles;
}
private String encode(String password) {
return new Md5PasswordEncoder().encodePassword(password, SALT);
}
@RelatedToVia(type = RATED)
@Fetch
Iterable<Rating> ratings;
// Set<Rating> ratings = new HashSet<>();
@RelatedTo(type = RATED)
Set<Movie> favorites;
public Set<Movie> getFavorites() {
return favorites;
}
public void setFavorites(Set<Movie> favorites) {
this.favorites = favorites;
}
@RelatedTo(type = FRIEND, direction = Direction.BOTH)
@Fetch
Set<User> friends;
public void addFriend(User friend) {
this.friends.add(friend);
}
public Rating rate(Neo4jOperations template, Movie movie, int stars,
String comment) {
final Rating rating = template.createRelationshipBetween(this, movie,
Rating.class, RATED, false).rate(stars, comment);
return template.save(rating);
}
/*
* public Rating rate(Movie movie, int stars, String comment) { if (ratings
* == null) { ratings = new HashSet<>(); }
*
* Rating rating = new Rating(this, movie, stars, comment);
* ratings.add(rating); movie.addRating(rating); return rating; }
*/
public Collection<Rating> getRatings() {
return IteratorUtil.asCollection(ratings);
}
/*
* public Set<Rating> getRatings() { return ratings; }
*/
@Override
public String toString() {
return String.format("%s (%s)", name, login);
}
public String getName() {
return name;
}
public Set<User> getFriends() {
return friends;
}
public Roles[] getRole() {
return roles;
}
public String getLogin() {
return login;
}
public String getPassword() {
return password;
}
public String getInfo() {
return info;
}
public void setInfo(String info) {
this.info = info;
}
public void updatePassword(String old, String newPass1, String newPass2) {
if (!password.equals(encode(old)))
throw new IllegalArgumentException("Existing Password invalid");
if (!newPass1.equals(newPass2))
throw new IllegalArgumentException("New Passwords don't match");
this.password = encode(newPass1);
}
public void setName(String name) {
this.name = name;
}
public boolean isFriend(User other) {
return other != null && getFriends().contains(other);
}
public enum Roles implements GrantedAuthority {
ROLE_USER, ROLE_ADMIN;
@Override
public String getAuthority() {
return name();
}
}
@Override
public boolean equals(Object o) {
if (this == o)
return true;
if (o == null || getClass() != o.getClass())
return false;
User user = (User) o;
if (nodeId == null)
return super.equals(o);
return nodeId.equals(user.nodeId);
}
public Long getId() {
return nodeId;
}
@Override
public int hashCode() {
return nodeId != null ? nodeId.hashCode() : super.hashCode();
}
package com.oberon.fm.domain;
@节点性
公共类用户{
@格拉希德
长节;
公共静态最终字符串SALT=“cewuiqwzie”;
公共静态最终字符串FRIEND=“FRIEND”;
公共静态最终字符串RATED=“RATED”;
@索引(indexType=indexType.FULLTEXT,indexName=“login”)
字符串登录;
@索引
字符串名;
字符串密码;
public void setPassword(字符串密码){
this.password=密码;
}
字符串信息;
私人角色[]角色;
公共用户(){
}
公共用户(字符串登录、字符串名称、字符串密码、角色…角色){
this.login=登录;
this.name=名称;
this.password=编码(密码);
this.roles=角色;
}
私有字符串编码(字符串密码){
返回新的Md5PasswordEncoder().encodePassword(密码,SALT);
}
@相关通孔(类型=额定值)
@取回
可接受评级;
//Set ratings=new HashSet();
@与(类型=额定值)相关
设置收藏夹;
公众的
package com.oberon.fm.domain;
@RelationshipEntity
public class Rating {
private static final int MAX_STARS = 5;
private static final int MIN_STARS = 0;
@GraphId
Long id;
@StartNode
User user;
@EndNode
Movie movie;
int stars;
String comment;
public User getUser() {
return user;
}
public Movie getMovie() {
return movie;
}
public int getStars() {
return stars;
}
public void setStars(int stars) {
this.stars = stars;
}
public String getComment() {
return comment;
}
public void setComment(String comment) {
this.comment = comment;
}
public Rating rate(int stars, String comment) {
if (stars >= MIN_STARS && stars <= MAX_STARS)
this.stars = stars;
if (comment != null && !comment.isEmpty())
this.comment = comment;
return this;
}
public Rating() {
}
public void setUser(User user) {
this.user = user;
}
public void setMovie(Movie movie) {
this.movie = movie;
}
@Override
public boolean equals(Object o) {
if (this == o)
return true;
if (o == null || getClass() != o.getClass())
return false;
Rating rating = (Rating) o;
if (id == null)
return super.equals(o);
return id.equals(rating.id);
}
@Override
public int hashCode() {
return id != null ? id.hashCode() : super.hashCode();
}
}
@RequestMapping(value = "/user", method = RequestMethod.GET)
public String profile(Model model, HttpServletRequest request) {
// User user=populator.getUserFromSession();
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";
}![enter image description here][4]