Peter Boomsma
Peter Boomsma

Reputation: 9806

Create list of objects by using userId

I have an application where users can add movie titles to a list. Currently I use this code to return the list:

@Override 
public List<Movie> getAllmovies() {

    List<Movie> movies = new ArrayList<Movie>();
    Iterator<Movie> iterator = movieRepository.findAll().iterator();
    while (iterator.hasNext()) {
        movies.add(iterator.next());
    }

    return movies;
}

As you expect, this returns all the movies and not the movies from the current user. So I changed to code to use findAllByUserId() instead of findAll():

@Override 
public List<Movie> getAllmovies() {

    User current_user = userService.getUser();  
    List<Movie> movies = new ArrayList<Movie>();
    movies = movieRepository.findAllByUserId(current_user.getId());

    return movies;
}

And I added the method in my MovieRepository:

@Repository
public interface MovieRepository extends JpaRepository<Movie, Serializable> {
    List<Movie> findAllByUserId(Long userId);
}

But now when I compile the code I get the error:

Caused by: org.springframework.data.mapping.PropertyReferenceException: No property userId found for type Movie! Did you mean 'users'?

Where does this error come from? It's not the userId argument in the MovieRepository.

//edit. After messing around a bit I figured out that userId relates to the findAllByUserId() method. So the error means I can't use userId because it's not a property of type Movie. So how do I return a list of movies by user id?

//edit. Added the Movie model:

package com.movieseat.models;

import com.movieseat.model.security.User;
import java.util.HashSet;
import java.util.Set;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToMany;
import javax.persistence.Table;

@Entity(name = "Movie")
@Table(name = "movie")
public class Movie {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "id")
    private Integer id;

    private String name;

    public Movie(){}

    public Movie(String name) {
        this.name = name;
    }

    public Movie(Integer id, String name ) {
        this.id = id;
        this.name = name;
    }

    public Movie(String name, Set<User> users){
        this.name = name;
        this.users = users;
    }    

    @ManyToMany(mappedBy = "movies")
    private Set<User> users = new HashSet<>(); 

    public Set<User> getUsers() {
        return users;
    }

    public void setUsers(Set<User> users) {
        this.users = users;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString(){
        return "id: " + id + "name: " + name;
    }

}

Upvotes: 0

Views: 627

Answers (1)

timothyclifford
timothyclifford

Reputation: 6969

The following method signature will get you want to want:

List<Movie> findByUsers_Id(Long id)

This is using the property expression feature of Spring Data JPA. The signature Users_Id will be translated to the JPQL x.users.id

More info here

Upvotes: 1

Related Questions