Java Spring Data - 存储库方法名称中的 OR 条件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37115582/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 19:02:22  来源:igfitidea点击:

Spring Data - OR condition in a repository method name

javaspringjpaspring-dataspring-data-jpa

提问by alexanoid

In my Spring Data project I have a following entity:

在我的 Spring Data 项目中,我有一个以下实体:

@Entity
@NamedEntityGraph(name = "graph.CardPair", attributeNodes = {})
@Table(name = "card_pairs")
public class CardPair extends BaseEntity implements Serializable {

    private static final long serialVersionUID = 7571004010972840728L;

    @Id
    @SequenceGenerator(name = "card_pairs_id_seq", sequenceName = "card_pairs_id_seq", allocationSize = 1)
    @GeneratedValue(strategy = GenerationType.AUTO, generator = "card_pairs_id_seq")
    private Long id;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "card1_id")
    private Card card1;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "card2_id")
    private Card card2;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "selected_card_id")
    private Card selectedCard;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "board_id")
    private Board board;

....

}

I need to implement a Spring Data repository method that will look for a CardPairbased on cardPairId, boardand card. The more complicated case to me is a Cardbecause I need to construct a condition where card1 = card or card2 = card

我需要实现一个 Spring Data 存储库方法,该方法将查找CardPair基于cardPairId,board和 的card. 对我来说更复杂的情况是Card因为我需要构建一个条件,其中card1 = card or card2 = card

Is it possible to provide this condition via Spring Data repository method name ?

是否可以通过 Spring Data 存储库方法名称提供此条件?

For example

例如

cardPairRepository.findByIdAndBoardAnd[card1 = card or card2 = card]AndSelectedCardIsNull(cardPairId, board, card);

Is it possible to translate this condition card1 = card or card2 = cardin a method name ?

是否可以card1 = card or card2 = card在方法名称中翻译此条件?

采纳答案by Alec Kravets

It is impossible to achieve this using just method name since you need the last condition to be in parentheses: id = ? AND board = ? AND (card1 = ? OR card2 = ?). There is no way to infer this from method name since it is intended to cover basic cases only.

这是不可能的,因为你需要的最后一个条件是在括号内实现这个只使用方法名称:id = ? AND board = ? AND (card1 = ? OR card2 = ?)。无法从方法名称推断出这一点,因为它仅用于涵盖基本情况。

There are three ways in your case:

您的情况有以下三种方式:

回答by Roel Strolenberg

You can find all operations here: http://docs.spring.io/spring-data/jpa/docs/1.3.0.RELEASE/reference/html/jpa.repositories.htmlsection 2.2.2

您可以在此处找到所有操作:http: //docs.spring.io/spring-data/jpa/docs/1.3.0.RELEASE/reference/html/jpa.repositories.htmlsection 2.2.2

findByIdAndBoardAndCard1OrCard2(cardPairId, board, card, card);

I haven't testen the order of this, but it should be something like this

我还没有测试这个顺序,但它应该是这样的