如何从CrudRepository为复合键创建方法扩展

时间:2016-08-22 17:15:12

标签: spring spring-boot spring-data-jpa

我有一个带有Composite Key ContainerLoadListPK的ContainerLoadList,就像这个

@Embeddable
public class ContainerLoadListPK implements Serializable {

    /**
     * 
     */
    private static final long serialVersionUID = 3526479259987259367L;
    @Basic(optional = false)
    @NotNull
    @Size(min = 1, max = 13)
    @Column(name = "ContainerNo")
    private String containerNo;
    @Basic(optional = false)
    @NotNull
    @Size(min = 1, max = 6)
    @Column(name = "VesselCode")
    private String vesselCode;
    @Basic(optional = false)
    @NotNull
    @Size(min = 1, max = 8)
    @Column(name = "Voyage")
    private String voyage;

    public ContainerLoadListPK() {
    }

    public ContainerLoadListPK(String containerNo, String vesselCode, String voyage) {
        this.containerNo = containerNo;
        this.vesselCode = vesselCode;
        this.voyage = voyage;
    }

    public String getContainerNo() {
        return containerNo;
    }

    public void setContainerNo(String containerNo) {
        this.containerNo = containerNo;
    }

    public String getVesselCode() {
        return vesselCode;
    }

    public void setVesselCode(String vesselCode) {
        this.vesselCode = vesselCode;
    }

    public String getVoyage() {
        return voyage;
    }

    public void setVoyage(String voyage) {
        this.voyage = voyage;
    }
}

我有另一个从CrudRepository扩展的接口,就像这个

public interface ContainerLoadListRepository extends CrudRepository<ContainerLoadList, ContainerLoadListPK> {
    List<ContainerLoadList> findByBNo(@Param("bNo") String bNo);

    @Query(name = "ContainerLoadList.findByVesselCodeAndVoyage")
    List<ContainerLoadList> findByVesselCodeAndVoyage(@Param("vesselCode") String vesselCode,@Param("voyage") String voyage);
}

您是否在不使用@Query的情况下为findByVesselCodeAndVoyage提供了另一种解决方案?

1 个答案:

答案 0 :(得分:2)

假设ContainerLoadList中的嵌入式关键字段被称为pk,您可以尝试这样做:

List<ContainerLoadList> findByPkVesselCodeAndPkVoyage(String vesselCode, String voyage);

有关详细信息,请查看Spring Data JPA Property Expressions documentation