Findby with nested object - Spring Data JPA

Asked

Viewed 471 times

0

I’m having a doubt when creating a findBy using jpa with atríbuto of a nested object, the classes are:

public class Livro {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    private String titulo;
    @ManyToOne
    private Usuario usuario;

and

public class Usuario {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    private String nome;
    private String email;

I need to return the books of a particular user by providing his email.

I tested using these methods in Restpository but it didn’t work:

List<Livro> findByLivro_Usuario_Email(String email);

List<Livro> findByLivroUsuario_Email(String email);

Do you have any suggestions of what might be?

  • I believe what you are looking for is a findByUsuarioEmail , but in the books Repository

  • You’re right, I made the correction here and it worked, it went like this: &#xA;@Repository&#xA;public interface LivroRepository extends JpaRepository<Livro, Long> {&#xA; List<Livro> findByUsuarioEmail(String email);&#xA;}&#xA; Thank you very much

1 answer

2

Corrected using:

@Repository
public interface LivroRepository extends JpaRepository<Livro, Long> {
    List<Livro> findByUsuarioEmail(String email);
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.