Spring Data - 存储库方法名称中的 OR 条件 [英] Spring Data - OR condition in a repository method name

查看:28
本文介绍了Spring Data - 存储库方法名称中的 OR 条件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

In my Spring Data project I have a following entity:

@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;

....

}

我需要实现一个 Spring Data 存储库方法,该方法将根据 cardPairIdboardcard<查找 CardPair/代码>.对我来说更复杂的情况是 Card,因为我需要构造一个条件,其中 card1 = card 或 card2 = card

I need to implement a Spring Data repository method that will look for a CardPair based on cardPairId, board and card. The more complicated case to me is a Card because I need to construct a condition where card1 = card or card2 = card

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

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

例如

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

是否可以将这个条件card1 = card or card2 = card 翻译成方法名称?

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

推荐答案

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

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.

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

There are three ways in your case:

这篇关于Spring Data - 存储库方法名称中的 OR 条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆