SQL 查询返回空值

Posted

技术标签:

【中文标题】SQL 查询返回空值【英文标题】:SQL Query returns Null Values 【发布时间】:2022-01-01 09:52:51 【问题描述】:

我有一个角色类型为营销人员、常务董事和总经理的用户实体。 当用户角色董事总经理登录时,我希望用户角色董事总经理仅查看分配给与董事总经理具有相同分支机构 ID 的用户类型营销人员的客户。

我在 客户存储库 中有一个自定义查询,它返回空结果。

@Query("SELECT customer from Customer customer join customer.marketer marketer "
 + "where marketer.branch = :director") 
List<Customer> findByUserBranch(User director);

这是用户实体

@Entity
@JsonIgnoreProperties("hibernateLazyInitializer","handler")
public class User 

    
    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    private Long id;
    
    private String firstName ;
    private String lastName;
    
    @Column(name="user_name", unique=true)
    private String userName;
    
    private String password;
    private String Gender; 
    private String phoneNumber;
    private String email;
    
    @JsonIgnoreProperties("hibernateLazyInitializer", "handler")
    @ManyToOne(targetEntity = Branch.class, 
     fetch = FetchType.LAZY )
    @JoinColumn(name="branch_id") 
    private Branch branch;
    
    @DateTimeFormat(pattern = "yyyy-MM-dd")
    private Date createdDate;
    
    @ManyToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
    @JoinTable(
    name = "users_roles", 
    joinColumns = @JoinColumn(name = "user_id"),
    inverseJoinColumns = @JoinColumn(name = "role_id")
    )
    private Set<UserRole> userRole = new HashSet<>();
    
    @Enumerated(EnumType.STRING)
    private UserStatus status;
    
    @JsonBackReference
    @OneToMany(mappedBy="marketer",cascade = CascadeType.ALL, targetEntity=Customer.class)
    private List <Customer> customer;

这是控制器类

@GetMapping(value="branch/customers") 
public List<Customer> getListByBranch()
 Authentication authentication =
SecurityContextHolder.getContext().getAuthentication(); 
User loggedInUser = userRepo.findByUserName(authentication.getName()); return customerRepo.findByBranch(loggedInUser); 


更新:

这是客户

@Entity
@JsonIgnoreProperties("hibernateLazyInitializer","handler")
public class Customer implements Serializable 

    /**
     * 
     */
    private static final long serialVersionUID = 8348682056500740593L;
    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    private Long id;
    private String userName;
    private String password;
    private String firstName ;
    private String lastName;
    private String gender;
    private String Address; 
    private String maritalStatus;
    private String category;
    private String motherMaidenName;
    private String idType;
    private String  idNumber;
    private String phoneNumber;
    private String email;
    
    @Column(nullable = true, length = 64)
    private String photos;
    
    @DateTimeFormat(pattern = "yyyy-MM-dd")
    private Date dateOfBirth;
    
    @DateTimeFormat(pattern = "yyyy-MM-dd")
    private Date registrationDate;
    
    @JsonIgnoreProperties("hibernateLazyInitializer", "handler")
    @ManyToOne(targetEntity = User.class, 
     fetch = FetchType.LAZY )
    @JoinColumn(name="marketer_id") 
    private User marketer ;
    
    @JsonBackReference
    @OneToMany(mappedBy="customer_id",cascade = CascadeType.ALL, targetEntity=Investment.class)
    private List<Investment> investment;

【问题讨论】:

【参考方案1】:

我无法发表评论,所以我会要求也给我们 customer.class。

【讨论】:

我刚刚用客户类更新了问题 你试过数据库中的查询吗?如果是,请与我分享。 不,我没有在数据库中尝试。 试试这个 => List findByUserBranch(@Param("director") 用户主管);我添加了 @Param("director") ,以便在查询中知道,看看它是否有效。 刚刚尝试过,仍然得到相同的空结果【参考方案2】:

解决了。我将用户对象更改为分支对象。

@Query("SELECT customer from Customer customer join "
            + "customer.marketer marketer "
          + "where marketer.branch = :branch") 
      List<Customer> findByUserBranch(Branch branch);

然后重构控制器类

@GetMapping(value="branch/customers") 
      public List<Customer> getListByBranch(Principal principal)
       
      User loggedInUser = userRepo.findByUserName(principal.getName());
      Branch branchId = loggedInUser.getBranch();
      return customerRepo.findByBranch(branchId); 
      

【讨论】:

以上是关于SQL 查询返回空值的主要内容,如果未能解决你的问题,请参考以下文章

automapper如何全局配置map条件过滤null值空值对所有映射起效

sql 查询时有空值返回0怎么写

那些年我们踩过的坑,SQL 中的空值陷阱!

SQL 查询返回空值

为啥针对 S3 的 pyspark sql 查询返回空值

如果我们传递空值,Sql 查询 ISNULL(@parameter,ColumnName) 返回啥?