Spring Jpa按登录用户及其ID选择 [英] Spring Jpa Choose By Logged User and his Id
本文介绍了Spring Jpa按登录用户及其ID选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图发送一个由id为的稀疏用户创建的收入列表,例如,我得到了company和customer,在表收入中,它们都具有相同的clinet ID,因为它们是由用户ID定义的,所以如何我拉正确的ID,因为如果我尝试发送get ID 1,例如,它将拉我表中的所有1.ty
i tried to send an list of income that created by the spacific user that have id , i got company for example and customer , in the table income they both have the same clinet id becouse they id defined by the user id so how i pull the right id becouse if i will try to send get id 1 for example it will pull me all the 1's in the table . ty
我现在构建的一些代码可能不正确:
some code that i build for now that probably not right :
@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);
}
推荐答案
a)
尝试:
findByClientId(Long clientId)
或:
List< T>findAll(Specification< T> spec);
b)
您正在使用
int客户ID
它在哪里:
长
这篇关于Spring Jpa按登录用户及其ID选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文