spring - 我可以在可分页的 findAll 存储库方法上引用 SpEL 中的 JPA 实体吗
问题描述
我有一个实体
@Data
@Entity
@Builder
@AllArgsConstructor
@NoArgsConstructor
@Table(name="component")
@Audited
public class Component implements Serializable {
private static final long serialVersionUID = 0L;
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Long Id;
@Version
private Long version;
@Builder.Default
private Boolean activated = true;
private String name;
@ManyToOne
@JoinColumn(referencedColumnName="Id", nullable = true)
@Audited(targetAuditMode = RelationTargetAuditMode.NOT_AUDITED)
private Organisation organisation;
}
...和一个存储库接口:
@Repository
@RepositoryRestResource(exported = false)
public interface ComponentRepository extends JpaRepository<Component, Long>, JpaSpecificationExecutor<Component>, RevisionRepository<Component, Long, Integer> {
@Query("select c from Component c where 1 = ?#{@authorisation.isInternalUser() or @authorisation.isUserInOrg(#c) ? 1:0}")
Page<Component> findAll(@Nullable Specification<Component> spec, Pageable pageable);
}
我的最终目标是以一种优雅的方式过滤掉当前登录用户不允许看到的实体。我认为 SpEL 会是一个很好的方法,但是我找不到在表达式中传递实体引用“c”的方法。另一种替代方法可能是将 JPQL 更改为这样的东西?
@Query("select c from Component c where c.organisation = ?#{ @authorisation.getCurrentUserOrganisation()} or 1 = ?#{@authorisation.isInternalUser() ? 1:0}")
或者也许有一种方法可以用一些 SpEL 直接注释实体?
我试图调试 Spring 源代码并了解它是如何工作的,但我所看到的只是在 SpEL 上下文评估中......只能访问 Pageable 对象和 JPA 规范。
先感谢您!
解决方案
创建Specification<Component> spec
这样做:
authorisation.isInternalUser() or @authorisation.isUserInOrg(#c)
并与它一起使用
Page<Component> findAll(@Nullable Specification<Component> spec, Pageable pageable);
推荐阅读
- vb.net - SQLite 在开发期间运行,而不是在编译后运行
- java - 生成令牌 JWT 后 Spring Security 302 重定向
- google-apps-script - 将自动生成的 Google Doc URL 写入电子表格的脚本
- javascript - fabric.js 和 socket.io 白板构建问题
- r - 如果值不同,则将列值折叠为一个
- python - 数骰子掷出 5 100 次(棋盘为 0-5)
- c++ - 我可以使用 std::less 来比较两个 char 数组吗?
- powershell - 使用powershell为计算机中的每个用户重命名漫游文件夹中的文件
- ios - 实施核心数据的问题
- javascript - 我的答题器游戏正在保存,但是当我尝试加载时,它会将所有内容显示为起始编号