当前位置: 首页 > 知识库问答 >
问题:

Spring数据规范-带连接的RSQL

南门鸿雪
2023-03-14

我正在尝试开发一个具有规范和RSQL的搜索API。遵循本教程-https://www.baeldung.com/rest-api-search-language-rsql-fiql

@Entity
public class User{

    @Column(nullable = false)
    private String firstName;

    @OneToOne(targetEntity = UserProfile.class, fetch = FetchType.EAGER)
    @JoinColumn(nullable = false, name = "user_profile_id")
    private UserProfile userProfile;
...

@Entity
public class UserProfile{

    @Column(nullable = false)
    private String education;

    ...
@Override
public Predicate toPredicate(Root<T> root, CriteriaQuery<?> query, CriteriaBuilder builder) {
    List<Object> args = castArguments(root);
    Object argument = args.get(0);

    switch (RsqlSearchOperation.getSimpleOperator(operator)) {

    case EQUAL: {
            if (argument instanceof String) {               
                return builder.like(root.get(property), argument.toString().replace('*', '%'));
            } else if (argument == null) {
                return builder.isNull(root.get(property));
            } else {
                return builder.equal(root.get(property), argument);
            }
        }
        case NOT_EQUAL: {
if (argument instanceof String) {
                Join<User, UserProfile> profileJoin = root.join("user_profile_id");

                return builder.like(root.get(property), profileJoin.get(property));
            } else if (argument == null) {

共有1个答案

卫念
2023-03-14

看来没有通用的具体解决方案。所以我做了它,这几乎是通用的。

switch (RsqlSearchOperation.getSimpleOperator(operator)) {

        case EQUAL: {
            if (doesClassContainProperty(UserProfile.class, property)) {
                Join<User, UserProfile> profileJoin = root.join("user_profile_id");
                return builder.equal(profileJoin.get(property), argument);
            } else {
                return builder.equal(root.get(property), argument);
            }
        }

下面是检查传递的参数是在根类中还是在联接类中的方法

public boolean doesClassContainProperty(Class<?> genericClass, String fieldName) {
        return Arrays.stream(genericClass.getDeclaredFields()).anyMatch(f -> f.getName().equals(fieldName));
    }
 类似资料:
  • 我有一个用spring boot Version1.5.x编写的项目,它连接到一个MariaDB数据库和几个表,它们之间有大量的关系。为了查询数据库,我使用org.springframework.data.jpa.repository中提供的JpaSpecificationExecutor接口。我们使用规范的原因是为了构建动态查询,而不必为存储库本身中的每个筛选可能性编写新的查询。在实体本身中,每

  • 我想使用Spring JPA规范连接函数 这是我的表格代码: > } 我如何在规范中加入表中的学生和家长?

  • 我按照本教程获得了Spring Data JPA规范:https://dzone.com/articles/using-spring-data-jpa-specification 它为我实现了这一点,但我不能调用规范方法来搜索它们。我想把它们放在我的SearchController中: 现在我想在我的SearchController(比如Controller)中调用这个方法,但我不知道如何调用。目

  • 如何使用规范编写下面查询 当我们使用Specification时,即使在multiselect中提到了count(),也没有选择count()。 为什么JPA中的multiselect方法不起作用

  • 表规范 命名统一小写下划线 非CMF核心应用,统一带应用表前缀,如portal_ 插件表,统一带插件表前缀,如:demo_ 表默认编码utf8mb4,默认排序规则utf8mb4_general_ci 引擎统一innodb 写表注释 字段规范 命名统一小写下划线 非自增主键一定要写字段注释 数据类型尽量用数字类型,数字类型的比字符类型的要快很多。 数据类型尽量小,这里的尽量小是指在满足可以预见的未来

  • 表规范 命名统一小写下划线 非CMF核心应用,统一带应用表前缀,如portal_ 插件表,统一带插件表前缀,如:plugindemo 表默认编码utf8mb4,默认排序规则utf8mb4_general_ci 引擎统一innodb 写表注释 字段规范 命名统一小写下划线 非自增主键一定要写字段注释 数据类型尽量用数字类型,数字类型的比字符类型的要快很多。 数据类型尽量小,这里的尽量小是指在满足可以