Hans Schwimmer
Hans Schwimmer

Reputation: 139

How to search after embedded entity's value in spring repository?

I have an entity Book with Author embedded(@manyToOne) and I am using repository to search a book by Author's firstname:

public interface BookRepository extends CrudRepository {

//@Query("SELECT b from Book b INNER JOIN b.author a WHERE a.lastname = ?#{[0]}")
public Iterable<Book> findByAuthorLastname(String lastname);

I tried both with my custom query and without it. Why does it fail?

Entities:

@Entity
@Table(name = "BOOK")
public class Book {

@ManyToOne
@JoinColumn(name = "AUTHOR_ID")
private Author author;

// other fields, getters and setters

}

@Entity
@Table(name = "AUTHOR")
public class Author {

@Column(name = "LASTNAME")
private String lastname;

@OneToMany(cascade = CascadeType.ALL, mappedBy = "author")
List<Book> books = new ArrayList<Book>();

// other fields, getters and setters
}

Upvotes: 0

Views: 216

Answers (1)

Elishtar
Elishtar

Reputation: 1

You have failed because, you make search by field authorLastName. For complex query you should use annotation @Query.

Upvotes: 0

Related Questions