JPA by Object in the Object using an atribute that is not @Id

问题: I would like to know if it is possible to create a JPA without using the @Query annotation to get the results from an atribute of the object inside the object I am querying...

问题:

I would like to know if it is possible to create a JPA without using the @Query annotation to get the results from an atribute of the object inside the object I am querying.

I am able to to `List findAllById(Long id) just fine, but what I would like to know is if it's possible to make a similar query that will look for the atribute of an object inside that class, example of my domain as follows:

public class OcorrenciaParticipante
    @Id
    @Column(name = "cod_ocorrencia_participante")
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long codigo;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "cod_ocorrencia", referencedColumnName = "cod_ocorrencia")
    private Ocorrencia ocorrencia;

public class Ocorrencia 
    @Id
    @Column(name = "cod_ocorrencia")
    private Long codigo;

What I want is to write something like List<OcorrenciaParticipante> findAllByOcorrencia to get a list of OcorrenciaParticipante using the codigo atribute from the Ocorrencia class.

When I do the above, and call my endpoint passing the value of variable codigo, I get the exception:

java.lang.IllegalArgumentException: Parameter value [1] did not match expected type [project.model.oc.Ocorrencia (n/a)]

I have tried some things using the information from here https://docs.spring.io/spring-data/jpa/docs/current/reference/html/#repository-query-keywords

Is that possible?


回答1:

I think you just need to include the nested property inside the repository method signature:

List<OcorrenciaParticipante> findAllByOcorrenciaCodigo

Spring docs on it: https://docs.spring.io/spring-data/jpa/docs/current/reference/html/#repositories.query-methods.query-property-expressions

Similar question with more discussion: Spring Data JPA find by embedded object property

  • 发表于 2019-02-16 12:43
  • 阅读 ( 293 )
  • 分类:sof

条评论

请先 登录 后评论
不写代码的码农
小编

篇文章

作家榜 »

  1. 小编 文章
返回顶部
部分文章转自于网络,若有侵权请联系我们删除