【问题标题】:How to filter results in a Spring repository by a value when searching from a "parent entity"?从“父实体”搜索时,如何按值过滤 Spring 存储库中的结果?
【发布时间】:2017-08-06 05:50:29
【问题描述】:

我正在使用 Spring Boot,并且我拥有这些实体:

@Entity
@Table(name = "usuario")
@NamedQuery(name = "Usuario.findAll", query="select u from Usuario u where 
u.estactiv=true order by u.user")
public class Usuario implements Serializable{
...
@Column(name="usu_estactiv",nullable=false)
private boolean estactiv=true;


@OneToMany(mappedBy = "user", cascade = CascadeType.ALL)
@JsonManagedReference
private List<Roles> roles = new ArrayList<>();
...}



@Entity
@Table(name = "roles")
@NamedQuery(name = "Roles.findAll", query="select r from Roles r where 
r.estactiv=true order by r.descripcion")
public class Roles implements Serializable{
...
@ManyToOne(fetch = FetchType.LAZY)  
@JoinColumn(columnDefinition="integer", name="usu_id", nullable=false)
@JsonBackReference
private Usuario user;

@Column(name="rol_estactiv",nullable=false)
private boolean estactiv=true;
...}

在我的存储库中:

@RepositoryRestResource(collectionResourceRel = "usuario", path = "usuario")
public interface UserRepository extends PagingAndSortingRepository<Usuario, 
Integer> {

@Override
@Query
public Page<Usuario> findAll(Pageable pageable);

Usuario findByUserAndEstactivTrue(@Param("user") String user);
}



@RepositoryRestResource(collectionResourceRel = "roles", path = "roles")
public interface RolesRepository extends 
PagingAndSortingRepository<Roles,Integer>{

@Override
@Query
public Page<Roles> findAll(Pageable pageable);

@Override
@Query
public Iterable<Roles> findAll();
}

当我转到网址时: http://localhost:8080/api/usuario/我得到了 estactiv=true 的用户:

但是当我转到http://localhost:8080/api/usuario/1/roles 时,我得到了这个:

我只想为每个用户获取具有 estactiv=true 的角色,例如:

用户 admin(id=1)在我的角色表中有两个角色:user(false) 和 admin(true),但是当我转到 /api/usuario/1/roles 时,我只想获取 admin(真的)

我希望我的问题很清楚,如果没有,请告诉我以便解决它。提前感谢您的帮助。

【问题讨论】:

    标签: java spring spring-boot


    【解决方案1】:

    您需要在角色查询中添加与表 usario 的连接,以便过滤 usario 表的 estactiv 属性。查询应如下所示:

    select r
    from Roles r
    inner join Usario u on r.usu_id=u.id
    where u.estactiv=true
    order by r.descripcion
    

    【讨论】:

    • 您好,感谢您的回答,当我在@NamedQuery 中添加内部联接时出现错误(org.hibernate.HibernateException:命名查询中的错误:Roles.findAll),我只想estactiv true 的角色,例如,当我转到 /api/usuario/1/roles 时,对于用户 admin(wit id 1),我只想要 estactiv true 的角色
    【解决方案2】:

    尝试进一步过滤列表,使用 Query string 并将参数作为 URL 中的查询字符串传递,如下所示:

    http://localhost:8080/api/usuario/1/roles?estactiv=true. 
    

    现在,您可以根据此查询字符串过滤输出结果,尝试通过在后面编写新的查询/代码来过滤您的数据。

    【讨论】:

      【解决方案3】:

      我正在阅读文档并找到了解决方案:

      首先在我的 RolesRepository 中,我声明了这个方法:

      public Collection<Roles> findByEstactivTrueAndUserId(@Param("user") Integer 
      user);
      

      然后在我的 UserController 中:

       @GetMapping("/api/usuario/{userId}/roles")
          public Collection<Roles> getRoles(@PathVariable Integer userId) {
              return rolRepo.findByEstactivTrueAndUserId(userId);
       }
      

      当我转到 http://localhost:8080/api/usuario/1/roles 时,对于 id=1 的 usuario,我只获得了 estactiv=true 的角色。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-07-12
        • 2017-02-02
        • 2021-01-03
        • 2020-04-07
        • 2021-10-14
        • 2021-12-23
        • 1970-01-01
        • 2021-07-22
        相关资源
        最近更新 更多