java 使用Spring JPA搜索相关实体列以匹配关键字

3phpmpom  于 2023-02-21  发布在  Java
关注(0)|答案(1)|浏览(116)

我已经使用了前面要求的question来开发一个足够好的解决方案,通过关键字搜索我的大多数列,我的问题是我有一些列是其他实体的外键,我想把它们包括在搜索中。
以下两项均不起作用:
SearchSpecification byBedroom = new SearchSpecification(new SearchCriteria("masterBedroom", ":", "searchVal"));执行但仅返回非外部实体或
SearchSpecification byBedroom = new SearchSpecification(new SearchCriteria("masterBedroom.roomName", ":", "searchVal"));抛出一个异常-org.springframework.dao.无效数据访问ApiUsageException:在此ManagedType上找不到具有给定名称[masterBedroom. roomName]的属性。
我错过了什么?

vuv7lop3

vuv7lop31#

我建议使用QueryDSL进行基于过滤器的搜索,因为它比Spring Data 规范或Criteria API更具可读性。
1.添加依赖项
<querydsl.version>5.0.0</querydsl.version>

<dependency>
            <groupId>com.querydsl</groupId>
            <artifactId>querydsl-core</artifactId>
            <version>${querydsl.version}</version>
        </dependency>
        <dependency>
            <groupId>com.querydsl</groupId>
            <artifactId>querydsl-jpa</artifactId>
            <version>${querydsl.version}</version>
            <classifier>jakarta</classifier>
        </dependency>
        <dependency>
            <groupId>com.querydsl</groupId>
            <artifactId>querydsl-sql</artifactId>
            <version>${querydsl.version}</version>
        </dependency>
        <dependency>
            <groupId>com.querydsl</groupId>
            <artifactId>querydsl-sql-spring</artifactId>
            <version>${querydsl.version}</version>
        </dependency>
        <dependency>
            <groupId>com.querydsl</groupId>
            <artifactId>querydsl-apt</artifactId>
            <version>${querydsl.version}</version>
            <classifier>jakarta</classifier>
        </dependency>

运行mvn clean install-它将生成querydsl模型类。
1.创建一个搜索参数pojo下面的示例使用record

@Builder(toBuilder = true)
public record UserProfileFilterParam(String city, String country, String state, Integer pincode) {}

1.创建搜索存储库界面

public interface UserProfileFilterRepository {

    List<UserProfile> fetchAll(UserProfileFilterParam filter);

    //Add methods if needed
}

1.在步骤3中创建repo的实现

@Repository
public class UserProfileFilterRepositoryImpl extends QuerydslRepositorySupport implements UserProfileFilterRepository {

    public UserProfileFilterRepositoryImpl() {
        super(UserProfile.class);
    }

    @Override
    public List<UserProfile> fetchAll(UserProfileFilterParam filter) {
        //Classes started with 'Q' are autogenerated by QueryDSL when executed maven build command.
        QUserProfile userProfile = QUserProfile.userProfile; //root
        QAddress address = QAddress.address; //foreign keys
        JPQLQuery<UserProfile> query = from(userProfile)
                .join(address).on(userProfile.address.id.eq(address.id));

        if(filter.city() != null) {
            query = query.where(address.city.likeIgnoreCase(filter.city()));
        }

        if(filter.country() != null) {
            query = query.where(address.country.likeIgnoreCase(filter.country()));
        }

        if(filter.state() != null) {
            query = query.where(address.state.likeIgnoreCase(filter.state()));
        }

        if(filter.pincode() != null) {
            query = query.where(address.pinCode.likeIgnoreCase(Integer.toString(filter.pincode())));
        }
        return query.orderBy(userProfile.id.asc()).fetch();
    }
}

更多详情:https://spring.io/blog/2011/04/26/advanced-spring-data-jpa-specifications-and-querydslhttps://www.baeldung.com/rest-api-search-language-spring-data-querydsl

相关问题