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

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

我想知道是否可以在不使用 @Query 批注的情况下创建 JPA,以从我正在查询的对象内部的对象属性中获取结果。

我能够 `List findAllById(Long id) 就好了,但我想知道的是,是否可以进行类似的查询,以查找 [=29 中的对象的属性=],我的域名示例如下:

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;

我想要的是编写类似 List<OcorrenciaParticipante> findAllByOcorrencia 的内容,以使用来自 Ocorrencia class 的 codigo 属性获取 OcorrenciaParticipante 的列表。

当我执行上述操作并调用我的端点传递变量 codigo 的值时,出现异常:

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

我已经使用这里的信息尝试了一些事情 https://docs.spring.io/spring-data/jpa/docs/current/reference/html/#repository-query-keywords

这可能吗?

我认为您只需要在存储库方法签名中包含嵌套的 属性:

List<OcorrenciaParticipante> findAllByOcorrenciaCodigo

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

有更多讨论的类似问题:Spring Data JPA find by embedded object property