this.partService .query({ 'deleted.specified': false, //判断字段为null page: pageToLoad - 1, size: this.itemsPerPage, sort: this.sort(), }) 这是一个part表的条件查询数据库使用的是herbnate的orm框架其中'deleted.specified': false条件中的deleted字段是part表外键关联的另一个表中的字段现在这么编写是不正确的那我应该怎么编写才能 'deleted.specified': false的功能
时间: 2024-03-11 15:46:46 浏览: 11
如果 `deleted` 字段是关联的另一个表中的字段,你需要使用 JOIN 来连接两个表,然后在查询条件中使用关联表的字段名。具体做法如下:
```java
import org.hibernate.criterion.Restrictions;
import org.hibernate.sql.JoinType;
import org.springframework.data.jpa.domain.Specification;
Specification<Part> spec = (root, query, cb) -> {
// 使用 LEFT JOIN 连接两个表
root.join("relatedTable", JoinType.LEFT);
// 使用关联表的字段名
return cb.isFalse(root.get("relatedTable").get("deleted"));
};
Page<Part> parts = partRepository.findAll(spec, PageRequest.of(pageToLoad - 1, itemsPerPage, sort));
```
这里假设 `Part` 实体类中有一个名为 `relatedTable` 的属性,用于关联另一个表。在 `Specification` 中,我们使用 `root.join()` 方法来连接两个表,并使用 `cb.isFalse()` 方法来判断关联表中的 `deleted` 字段是否为 `false`。
请注意,具体的语法可能会因为使用的数据库和 ORM 框架而有所不同,上述示例仅供参考。