繁体   English   中英

如何在JPA存储库方法中使用部分复合键来获取数据?

[英]How to use part of composite key in JPA repository methods to fetch data?

我有一个使用EmbeddedId的类Application和embbbedID中的第二列是forgein键,并且与offer有多对一的关系。

@Entity
   public class Application implements Serializable{

private Integer id;

@EmbeddedId
private MyKey mykey;

private String resume;

@Enumerated(EnumType.STRING)
@NotNull
private ApplicationStatus applicationStatus;

    @Embeddable
public class MyKey implements Serializable{

    private static final long serialVersionUID = 1L;
    @NotNull
    private String emailId;
    @ManyToOne(fetch = FetchType.LAZY)
    @NotNull
    private Offer offer;

在Offer类映射是在jobTitle上完成的。

 @Repository
interface ApplicationRepository extends JpaRepository <Application,MyKey>

{

List<Application> findAllByMyKey_Offer(String jobTitle);
}

尝试这个但没有成功...我想获取有关特定jobTitle的所有应用程序。 什么是我在ApplicationRepository类中的方法名称。

您的方法名称错误,右侧是findAllByMykey_OfferfindAllByMykeyOffer ,因为您的字段名为mykey。

正如在文档https://docs.spring.io/spring-data/jpa/docs/2.1.0.RELEASE/reference/html/中使用List<Application> findAllByMykey_Offer(String jobTitle);所提到的那样List<Application> findAllByMykey_Offer(String jobTitle); 优于List<Application> findAllByMykeyOffer(String jobTitle);

使用复合键,您的字段名称应包含嵌入式ID字段的名称。 在你的情况下它会是这样的(我还没有测试过)

List<Application> findAllByMykeyOffer(String jobTitle);

请注意, k在这里是小写的,因为类Application的字段名为mykey ,而不是myKey

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM