javaspring-mvcjpa

Spring Data JPA Many to Many Service Repository Problem


I am trying to add ManyToMany entity to my application. I created entity but cannot implement it.

Actor class

@Entity
@Table(name = "actor")
public class Actor {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer id;

    @Column(nullable = false, name = "actor_name")
    private String actorName;

    @ManyToMany(mappedBy = "actor", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
    private Set<Movie> movie = new HashSet<Movie>();

    public Integer getId() {
        return id;
    }

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

    public String getActorName() { return actorName; }

    public void setActorName(String actorName) {
        this.actorName = actorName;
    }

    public Set<Movie> getMovie() {
        return movie;
    }

    public void setMovie(Set<Movie> movie) {
       this.movie = movie;
    }
}

In movie class I have

@ManyToMany(cascade = CascadeType.ALL)
    @JoinTable(
            name = "movie_actor",
            joinColumns = {@JoinColumn(name = "movie_id")},
            inverseJoinColumns = {@JoinColumn(name = "actor_id")}
    )
    Set<Actor> actor = new HashSet<Actor>();

........................

 public Set<Actor> getActor () {
        return actor;
    }

    public void setActor(Set<Actor> actor){
        this.actor = actor;
    }

I created my entity just like this but in MovieService;

Actor actor = ActorRepository.findByActorName(movie.getActor().getActorName());
        movie.setActor(actor);

This part gives me error. movie.getActor().getActorName() method cannot find. Where do I need to look? In IDE it also says method getActorName and setActorName is never used. I am also adding my ActorRepository and ActorService to closer look to the problem.

ActorRepository

public interface ActorRepository extends JpaRepository<Actor, Integer> {

    Set<Actor> findByActorName(String actorName);
}

ActorService

@Service
public class ActorService {

    private ActorRepository actorRepository;

    @Autowired
    public ActorService(ActorRepository actorRepository) {
        this.actorRepository = actorRepository;
    }

    public List<Actor> getAllActor() {
        return actorRepository.findAll();
    }
}

After adding ManyToMany I was using is as OneToMany entity. Services is works for OneToMany. How can I use them for ManyToMany? I need to add multiple actors to my movies. I couldn't find MVC projects for ManyToMany implementation.


Solution

  • You're invoking movie.getActor().getActorName() which basically does a getActorName() on a Set<Actor> object.

    You're basically treating the relation as a ManyToOne instead of a OneToMany

    You could use the following to fetch the first Actor of the Set

    ActorRepository.findByActorName(
        movie.getActors()
             .iterator()
             .next()
             .getActorName()
    );
    

    But then of course, you don't have all your Actor's names

    What you could do is the following

    public interface ActorRepository extends JpaRepository<Actor, Integer> {
        Set<Actor> findByActorNameIn(List<String> actorName);
    }
    

    And invoke it that way

    ActorRepository.findByActorNameIn(
        movie.getActors()
             .stream()
             .map(Actor::getName)
             .collect(Collectors.toList())
    );