简体   繁体   English

如何控制 JpaRepository 进行类似查询

[英]How to control JpaRepository for similar query

I made an entity as below.我做了一个实体如下。

@Entity
@Getter
@Setter
@Builder
@AllArgsConstructor
@NoArgsConstructor
@ToString
public class Lecture {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer id;

    @NotNull
    @Size(min = 1, max = 4096)
    private String title;

    @NotNull
    @Size(min = 1, max = 4096)
    private String link;


    @JsonIgnore
    @NotNull
    private boolean visible;
}

And I used jpa repository.我使用了 jpa 存储库。

public interface LectureRepository extends JpaRepository<Lecture, Integer> {
    Page<LectureList> findByVisible(boolean visible, Pageable pageable);
}

And this is filter for list.这是列表的过滤器。

public interface LectureList {
    String getTitle();

    LocalDateTime getLink();

    Integer getId();

}

So when I get return data by findByVisible query, it gives "id, title, link" without a problem.因此,当我通过 findByVisible 查询获取返回数据时,它会毫无问题地给出“id、title、link”。
But I need other filter for another client.但我需要另一个客户的其他过滤器。
For example, another repository should give me "id, title" field.例如,另一个存储库应该给我“id,title”字段。
So I think I have to make another Repository File for this case.所以我认为我必须为这种情况制作另一个存储库文件。
Could I have a more simple solution for this?我可以为此提供更简单的解决方案吗? Could I make this query in the same repository file?我可以在同一个存储库文件中进行此查询吗?

I'm not sure if I understand your question.我不确定我是否理解你的问题。 You can have different functions in the same JPA Repository interface.您可以在同一个 JPA Repository 接口中拥有不同的功能 And you can only have one Repository per Entity.每个实体只能有一个存储库。

There are different solutions to present different information through the controller itself.通过 controller 本身有不同的解决方案来呈现不同的信息。 If you still want to show all objects in a Page you can create another object with the parameters you'd like to return, for example.例如,如果您仍想显示页面中的所有对象,您可以创建另一个 object 并使用您想要返回的参数。

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

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