所以我知道我可以过滤
- 使用
findByProperty
获取 JPA 中的特定属性。 - 以及 Set 中类的属性(属性:父级中的 classset)以及
findByClassset_Property
但是如果我有一组字符串,我该如何过滤呢? 我无法在一组字符串上使用 _Property,因为字符串没有属性?
假设我有以下实体:
@Entity(name = "mine")
public class MyEntity extends AbstractBaseEntity {
@Column(name = "something")
private String something;
@ElementCollection (fetch = FetchType.EAGER)
private Set<Anotherclass> colorlist;
@ElementCollection (fetch = FetchType.EAGER)
private Set<String> mylist;
}
另一个类以 String 作为属性:
public class Anotherclass {
String color;
}
这是我的存储库:
@Repository
public interface MyEntityRepository extends JpaRepository<RisikoEntity, Long> {
List<MyEntity> findBySomethingContainsIgnoreCase(String filterstring); // works as expected
List<MyEntity> findByColorlist_ColorIgnoreCase(String filterstring); // works as expected
List<MyEntity> findByMylistContainsIgnoreCase(String filterstring); // does not work - returns no matches
}
最佳答案
所以,正如 Java 聊天室中向我解释的那样 -
in relational or tabular databases, a collection of primitives does not exist. Arrays, maps, sets, trees, etc do not exist as values, the only thing that exists are sets of entities and those entities could have a single string column. But you would have to make an entity defining that column
因此,它要么使用包含字符串的单个列构建一个实体,然后对其进行过滤(就像我已经对具有单个字符串属性 color
的类 Anotherclass
所做的那样)从上面)。
或者使用@Query,如下所示:
@Query (value = "SELECT * from myentity e " +
" left join myentity_mylist l ON e.id = l.myentity_id " +
" WHERE lower(l.mylist) LIKE CONCAT('%',:filtertext,'%') ", nativeQuery = true)
List<MyEntity> findbyCustomFiltering(@Param("filtertext") String filterstring_lowercase);
关于java - JPA findby 过滤子字符串列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61271305/