javaspringspring-data-jpamethod-namesquery-derivation

Spring Data JPA - Method Name - @ManyToOne


I have an entity CpoPipeline with a relationship ManyToOne with CpoEnvironment:

@Entity
@Table(name = "cpo_pipeline", catalog = "cup_orchestrator")
public class CpoPipeline implements java.io.Serializable {
    private String pipelineId;
    private String pipelineName;
    private CpoEnvironment cpoEnvironment;

    @Column(name = "pipeline_id", unique = true, nullable = false)
    public String getPipelineId() {
        return this.pipelineId;
    }

    @ManyToOne(fetch = FetchType.EAGER)
    @JoinColumn(name = "environment_id", nullable = false)
    public CpoEnvironment getCpoEnvironment() {
        return this.cpoEnvironment;
    }
    //Getters and Setters
}

The entity CpoEnvironment:

@Entity
@Table(name = "cpo_environment", catalog = "cup_orchestrator")
public class CpoEnvironment implements java.io.Serializable {

    private String environmentId;
    private String environment;
    private Set<CpoPipeline> cpoPipelines = new HashSet<CpoPipeline>(0);

    @Id
    @Column(name = "environment_id", unique = true, nullable = false)
    public String getEnvironmentId() {
        return this.environmentId;
    }

    @OneToMany(fetch = FetchType.LAZY, mappedBy = "cpoEnvironment")
    public Set<CpoPipeline> getCpoPipelines() {
        return this.cpoPipelines;
    }

    //Getters and Setters
}

The repository for this entity with a method name:

@Repository
public interface PipelineRep extends JpaRepository<CpoPipeline, String> {

    Optional<CpoPipeline> findByPipelineIdAndEnvironmentId(String pipelineId, String environmentId);

}

Error: Caused by: org.springframework.data.mapping.PropertyReferenceException: No property environmentId found for type CpoPipeline

How can I create a method name using one field from the entity and one field from the relation? Is it possible?


Solution

  • Yes possible, to use environmentId of CpoEnvironment entity use this way CpoEnvironmentEnvironmentId

    Optional<CpoPipeline> findByPipelineIdAndCpoEnvironmentEnvironmentId(String pipelineId, String environmentId);