Spring Jpa按登录用户及其ID选择

时间:2019-12-26 12:37:11

标签: spring jpa

我尝试发送由具有ID的特定用户创建的收入列表,例如我得到了company和customer,在表收入中它们都具有相同的客户编号,因为它们是由用户ID定义的,所以如何我拉正确的ID,因为如果我尝试发送get ID 1,例如,它将拉我表中的所有1。 ty

我现在构建的一些代码可能不正确:

@SuppressWarnings("static-access")
    public List<Income> viewIncomeByCompany(int clientid) throws Exception {
        LoginUser loginUser = new LoginUser();
        if (loginUser.getClientType().COMPANY != null) {
            List<Income> allIncomesByCompany = incomeRepo.findAllByClientId(clientid);
            return allIncomesByCompany;
        } else {

            throw new Exception();
        }

    }


在公司财务负责人中:


    @GetMapping("/allincomecompany/{companyid}")
    public List<Income> viewIncomeByCompanyId(HttpServletRequest req) throws Exception {
        List<Income> allcompanyincome = incomeService.viewIncomeByCompany(getLoggedUser(req).getUserId());
        return allcompanyincome;

    }

收入表:

@Entity
@Table(name = "income")
public class Income {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "incomeId")
    private int id;

    @Basic(optional = false)
    @Column(nullable = false)
    private long clientId;

    @Column(nullable = false)
    @Basic(optional = false)
    private String name;

    @Column(nullable = false)
    @Basic(optional = false)
    @JsonFormat(pattern="dd-MM-yyyy")
//  @DateTimeFormat(pattern = "dd-MM-yyyy")
    private Date date;

    @Column(nullable = false)
    @Enumerated(EnumType.STRING)
    private IncomeType description;

    @Column(nullable = false)
    @Basic(optional = false)
    private double price;

    public int getId() {
        return id;
    }

收入库:


@Repository
public interface IncomeRepo extends JpaRepository<Income, Long> {

    List<Income> findAllByClientId(int clientid);

}

1 个答案:

答案 0 :(得分:1)

a)

尝试:

findByClientId(Long clientId)

或:

List<T> findAll(Specification<T> spec);

b)

您正在使用

int clientid

它在哪里:

long