I have Book entity and book repository. Book has string array with genres. I need to implement findAllByGenre method for searching all books which will be includes passed "genre" at their array. How it will be better to implements? Book Entity Book Repository
I tried to create method which will be returns List of books, but can't understood how should I pass params and do select with string
UPDATED: I've tried to implement Repository method like that
@Repository
public interface BookRepository extends CrudRepository<BookEntity, Long> {
BookEntity findOneBySlug(String slug);
@Query("SELECT b FROM book_entity WHERE :genre in b.genre")
List<BookEntity> findAllByGenre(@Param("genre") String genre);
}
Also I'm sharing BookEntity code
package com.uabookserver.uabookserver.entity;
import jakarta.persistence.*;
import org.springframework.lang.NonNull;
@Entity
public class BookEntity {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@NonNull
private String slug;
private String title;
private String author;
private String country;
private int year;
private String[] genre;
private int likes = 0;
private int bookmarks = 0;
private int downloads = 0;
@Lob
private String image;
@Lob
private String[] file;
public BookEntity() {}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getSlug() {
return slug;
}
public void setSlug(String slug) {
this.slug = slug;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public int getYear() {
return year;
}
public void setYear(int year) {
this.year = year;
}
public String[] getGenre() {
return genre;
}
public void setGenre(String[] genre) {
this.genre = genre;
}
public int getLikes() {
return likes;
}
public void setLikes(int likes) {
this.likes = likes;
}
public int getBookmarks() {
return bookmarks;
}
public void setBookmarks(int bookmarks) {
this.bookmarks = bookmarks;
}
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
public int getDownloads() {
return downloads;
}
public void setDownloads(int downloads) {
this.downloads = downloads;
}
public String getImage() {
return image;
}
public void setImage(String image) {
this.image = image;
}
public String[] getFile() {
return file;
}
public void setFile(String[] file) {
this.file = file;
}
}
And BokkService method for getting all books by genre
public List<Book> getAllByGenre(String genre) {
List<BookEntity> books = bookRepository.findAllByGenre(genre);
List<Book> models = this.convertToModelsList(books);
return models;
}