Serg Shapoval
Serg Shapoval

Reputation: 717

How to find all matches for every string in collection using Spring data

I have got a collection of strings. I need to find in database all file names that contains this string. Like a full matching "LIKE(%KEYWORD%)";

Here is my method

  Page<FilePdf> getByFileNameContainingInAndExtension(Collection<String> fileNames, String extension, Pageable pageable);

But it gives me an exception

Caused by: org.springframework.data.mapping.PropertyReferenceException: No property containing found for type String! Traversed path: FilePdf.fileName.

UPDATE Here is my repository

 public interface FilePdfRepository extends JpaRepository<FilePdf, Long> {
    Page<FilePdf> getByFileNameContainingInAndExtension(Collection<String> fileNames, String extension, Pageable pageable);
}

And here is my entity

@Entity
@Table(name = "dlfileentry")
public class FilePdf {
    @Id
    @Column(name = "uuid_")
    private String id;
    @Column(name = "fileentryid")
    private Integer fileEntryId;
    @Column(name = "groupid")
    private Integer groupId;
    @Column(name = "filename")
    private String fileName;
    @Column(name = "folderid")
    private Integer folderId;
    @Column(name = "extension")
    private String extension;
}

Upvotes: 1

Views: 4889

Answers (3)

eeedev
eeedev

Reputation: 149

If you want to query using querydsl, the JPAQuery can be formed in the following manner :

Predicate finalPredicate(StringPath sp, List<String> filterValues) {
        BooleanBuilder bb = new BooleanBuilder();
        String value;
        for (Iterator arg2 = filterValues.iterator(); arg2
                .hasNext(); bb = bb.or(sp.containsIgnoreCase(value.trim().toLowerCase()))) {
            value = (String) arg2.next();
        }
        return bb;
    }

Upvotes: 0

The root cause of the problem is that the ContainingIn keyword does not belong to the Spring Data JPA.

The Containing keyword does support only a single value, not a collection.

Consider updating the repository as follows:

public interface FilePdfRepository extends JpaRepository<FilePdf, Long> {
    Page<FilePdf> getByFileNameContainingAndExtension(String fileName, String extension, Pageable pageable);
}

Upvotes: 0

Barath
Barath

Reputation: 5283

use findBy instead of getBy

using in keyword

 public interface FilePdfRepository extends JpaRepository<FilePdf, Long> {
        Page<FilePdf> findByFileNameInAndExtension(Collection<String> fileNames, String extension, Pageable pageable);
    }

see Docs : https://docs.spring.io/spring-data/jpa/docs/1.11.6.RELEASE/reference/html/

Upvotes: 1

Related Questions