【问题标题】:Spring Data JPA - Method Name - @ManyToOneSpring Data JPA - 方法名称 - @ManyToOne
【发布时间】:2020-08-24 14:15:02
【问题描述】:

我有一个实体 CpoPipeline,其关系为 ManyToOne 和 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
}

实体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
}

具有方法名称的该实体的存储库:

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

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

}

错误:原因:org.springframework.data.mapping.PropertyReferenceException:未找到类型 CpoPipeline 的属性 environmentId

如何使用实体中的一个字段和关系中的一个字段创建方法名称?有可能吗?

【问题讨论】:

标签: java spring spring-data-jpa method-names query-derivation


【解决方案1】:

可以,使用environmentIdCpoEnvironment 实体使用这种方式CpoEnvironmentEnvironmentId

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

【讨论】:

    猜你喜欢
    • 2018-09-04
    • 2021-03-16
    • 1970-01-01
    • 1970-01-01
    • 2022-11-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-09
    相关资源
    最近更新 更多