'i have a search box that works well but it currently filters results only by artist name although i also have filtering by song.. what is wrong?

I have a search box that should filter the data stored in the database by artist or song but for some reason it currently only filters by artist even though I have defined it to filter by song as well.

MusicController.java

@Controller
public class MusicController {
    
    @Autowired
    private MusicRepository mrepository;
    
     @Autowired
     private GenreRepository grepository;
    
    
 //sisäänkirjautumimen
     @RequestMapping(value="/login")
     public String login() {    
         return "login";
     }  

     //Näyttää musiikkilistan
        
     @RequestMapping(value="/musiclist")
        public String bookList(Model model) {
        model.addAttribute("musics", mrepository.findAll());
                return "musiclist";
            }
     //Search field
    @RequestMapping(value ="musiclist", method=RequestMethod.GET)
    public String musics(Model model, @Param("keyword") String keyword) {
        if (keyword != null) {
            model.addAttribute("musics", mrepository.findByArtist(keyword));
            model.addAttribute("musics", mrepository.findBySong(keyword));
            
            return "Music";
        }
            model.addAttribute("musics", mrepository.findAll());
            return "Music";
    }
    

MusicRepository

public interface MusicRepository extends CrudRepository<Music, Long> {
    
     List<Music> findByMusicId(Long musicId);
     List<Music> findByArtist(String artist);
     List<Music> findBySong(String song);
     
        
}

Here is the Music.java i cant find any problems :(

package MusicProject.Music.domain;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

@Entity
public class Music {
     @Id
     @GeneratedValue(strategy=GenerationType.AUTO)
     public Long musicId;
     public String artist;
     public String song;
     public int year;
     
     @ManyToOne
       @JsonIgnoreProperties ("musics") 
       @JoinColumn(name = "genreId")
       private Genre genre;
     
     public Music() {}
     
     public Music(String artist, String song, int year, Genre genre) {
         super();
         this.artist = artist;
         this.song = song;
         this.year = year;
         this.genre = genre;
    }
    public Long getMusicId() {
        return musicId;
    }

    public void setMusicId(Long id) {
        this.musicId = id;
    }
    
    public String getArtist() {
        return artist;
    }

    public void setArtist(String artist) {
        this.artist = artist;
    }
    
    public String getSong() {
        return song;
    }

    public void setSong(String song) {
        this.song = song;
    }
    
    public int getYear() {
        return year;
    }
    
    public void setYear(int year) {
        this.year = year;
    }
    
     
    public Genre getGenre() {
        return genre;
    }

    public void setGenre(Genre genre) {
        this.genre = genre;
    }

    @Override
    public String toString() {
        if (this.genre != null)
        return "Music [musicId=" + musicId + ", Artist=" + artist + ", Song=" + song + ", Year=" + year + "Genre=" + this.getGenre() + "]";
        else 
            return "Music [musicId=" + musicId + ", Artist=" + artist + ", Song=" + song + ", Year=" + year + "]";
                 
}   
}

I haven't made any changes to the music category since the search box was added.



Solution 1:[1]

It seems that filtering is performed only by 1 field, because you re-write musics model attribute here

if (keyword != null) {
        model.addAttribute("musics", mrepository.findByArtist(keyword));
        model.addAttribute("musics", mrepository.findBySong(keyword));
  1. First you set it to the findByArtist result
  2. Then try to update with findBySong result.

Solution can be the following:

  1. add additional method to your MusicRepository

    findByArtistOrSong(String artist, String song)
    
  2. Call this method instead the other two here

    if (keyword != null) { model.addAttribute("musics", mrepository.findByArtistOrSong(keyword, keyword));

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Sve Kamenska