spring-data-jpa JPA条件,例如OneToOne连接

b5lpy0ml  于 2022-11-10  发布在  Spring
关注(0)|答案(1)|浏览(211)

我需要搜索包含字符串参数的数据
我有两个实体:

@Entity
@Table(name = "referentiel_digital")
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
public class ReferentielDigital implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @NotNull
    @Size(max = 200)
    @Column(name = "libelle_commercial", length = 200, nullable = false)
    private String libelleCommercial;

    @Size(max = 1000)
    @Column(name = "description_courte", length = 1000)
    private String descriptionCourte;

    @Size(max = 1000)
    @Column(name = "description_longue", length = 1000)
    private String descriptionLongue;

    @OneToOne
    @JoinColumn(unique = true)
    private Referentiel reference;

@Entity
@Table(name = "referentiel")
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
public class Referentiel implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @NotNull
    @Column(name = "uuid", nullable = false)
    private UUID uuid;

    ....

    @OneToOne(mappedBy = "reference")
    @JsonIgnoreProperties("referentiels")
    private ReferentielDigital digital;

我开发了一个使用JPA标准的搜索API,它可以很好地处理不同的字段和join(ManyToOne),但是在这个例子中,我需要找到Referentiel,它包含referentielDigital中的字符串。
我试试这个:

Join<Referentiel, ReferentielDigital> digital = root.join("digital");
        Expression<String> exp1 = digital.get("libelleCommercial");

        Predicate p1 = exp1.in("%" + criteria.getLibelle() + "%");

        Expression<String> exp2 = digital.get("descriptionCourte");
        Predicate p2 = exp1.in("%" + criteria.getLibelle() + "%");

        Expression<String> exp3 = digital.get("descriptionLongue");
        Predicate p3 = exp1.in("%" + criteria.getLibelle() + "%");

        predicates.add(cb.or(p1, p2, p3));

它不起作用,因为请求包含“在”,我想有“喜欢”,以获得良好的结果。
对于Extension对象,我没有“like”方法。
请问我怎样才能用这个加入来做喜欢的请求呢?

bz4sfanl

bz4sfanl1#

我找到解决方案与JB Nizet帮助:

Join<Referentiel, ReferentielDigital> digital = root.join("digital");
        Expression<String> exp1 = digital.get("libelleCommercial");

        Predicate p1 = cb.like(exp1, "%" + criteria.getLibelle() + "%");

        Expression<String> exp2 = digital.get("descriptionCourte");
        Predicate p2 = cb.like(exp2, "%" + criteria.getLibelle() + "%");

        Expression<String> exp3 = digital.get("descriptionLongue");
        Predicate p3 = cb.like(exp3, "%" + criteria.getLibelle() + "%");

        predicates.add(cb.or(p1, p2, p3));

相关问题