弹簧数据neo4j,我无法解决它

时间:2015-04-21 03:32:36

标签: spring neo4j spring-data-neo4j

from the picture,we can see that i can't view the title but commnetandstars is right enter image description here enter image description here我使用spring数据neo4j,我有user.class.movie.class,rating.class,我创建了电影和评级之间的关系,当我运行程序员时,我可以得到评级(明星,评论)但无法获得电影的标题(null) movie.class

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;
}

} user.class

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();
}

} rating.class

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]

1 个答案:

答案 0 :(得分:0)

如果您未在影片字段中指定@Fetch,则默认情况下仅加载影片的ID。但我宁愿建议只在您需要时使用template.fetch(rating.movie)加载它。