gpt4 book ai didi

java - Spring 数据 : findByMyListIsEmpty() throws a compilation error

转载 作者:行者123 更新时间:2023-12-02 09:39:20 24 4
gpt4 key购买 nike

我正在寻找一种查询方法,该方法返回具有空 List 属性的对象列表。

我已经尝试过

List<MyEntity> findByMyListIsEmpty();
List<MyEntity> findByMyListEmpty();

我收到编译错误

Invalid derived query! No property isEmpty found for type MyList! Traversed path: MyEntity.myList.

我也尝试过

List<MyEntity> findByMyListIsNull();

它不会导致编译错误,但它给了我错误的结果,因为 myList 不为空(空列表)

这是我的实体类的代码

class MyEntity implements Serializable {
// some fields
@ManyToMany(fetch = FetchType.EAGER)
@JoinTable(name = "MY_ENTITY_LIST", joinColumns = @JoinColumn(name = "MY_ENTITY_ID"), inverseJoinColumns = @JoinColumn(name = "MY_LIST_ID"))
private List<MyList> myList;
// constuctors & getters & setters
}

MyEntityRepository:

public interface MyEntityRepository extends JpaRepository<MyEntity, Long> {
List<MyEntity> findByMyListEmpty(); //My problem is here
}

最佳答案

首先,在使用 Spring Data JPA findBy 查询进行查询时,没有可以使用的 IsEmpty/Empty 关键字。因此,您会收到编译错误,因为它不受支持。请参阅此链接了解 spring- https://docs.spring.io/spring-data/jpa/docs/current/reference/html/ 支持的关键字- 表 3. spring 提供的方法名称中支持的关键字。

IsNull 用于检查 Null 值。由于您的情况列表为空,我建议尝试使用 findByMyListIn(List list) 方法并传递空列表作为参数。我还没有实际检查过这个解决方案,但根据 spring 文档,它应该可以工作。

除此之外,还要检查实体之间的关系,因为您使用了 @JoinTable 注释,但您的帖子中没有提及关联实体/连接表。

作为替代方法,如果内置方法未给出正确的结果,您始终可以编写自己的自定义查询。

希望这有帮助。

关于java - Spring 数据 : findByMyListIsEmpty() throws a compilation error,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57223190/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com