Spring Data REST-如何在投影中包含计算的数据? [英] Spring Data REST - How to include calculated data in a projection?
问题描述
我定义了以下域类.
贷款类别
@Data
@Entity
public class Loan {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
private String loanTitle;
@OneToMany(cascade = CascadeType.ALL, orphanRemoval = true)
@JoinColumn(name = "loan_id")
private List<Allowance> allowances;
}
津贴等级
@Data
@Entity
public class Allowance {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@ManyToOne
private AllowanceType allowanceType;
private Double allowanceAmount;
}
我还为贷款类定义了一个投影界面,如下所示:
@Projection(name = "studyLoanSingle", types = {Loan.class})
public interface LoanProjection {
String getLoanTitle();
List<AllowanceProjection> getAllowances();
}
现在,我要在投影中包括贷款的总金额(通过对津贴"列表进行迭代来计算),并将其发送到UI.在Spring Data REST中可以做到这一点吗?
来自 并像这样使用Projection: 然后您可以获得全部金额的贷款:
一切看起来不错,但是这里有一个小"问题-对于结果中的每条记录,我们都会向数据库额外查询以计算总金额.因此,您在这里面临' N + 1个查询问题 `. 我对带有Projection的SDR中此问题的调查,可以在此处找到.> I have the following domain classes defined. Loan Class Allowance class I also have a projection interface defined for the loan class as follows: Now I want to include the total amount of the loan(which is calculated by iterating the list of Allowances) in the projection and send it to the UI. Is it possible to do this in Spring Data REST? From here: You can annotate exposed properties in Projection with So you have to create a and use like this Projection: Then you can get your loans with total amount:
All looks fine but we have a 'small' issue here - for each record in the result we get an extra query to the DB that calculate total amount. So you faces here with 'N+1 queries issue`. My investigation of this problem in SDR with Projections you can find here. 这篇关于Spring Data REST-如何在投影中包含计算的数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!@Projection(name = "totalAmount", types = Loan.class)
public interface LoanTotalAmount {
@Value("#{target}")
Loan getLoan();
@Value("#{@loanRepo.getTotalAmountByLoan(target)}")
Double getAmount();
}
GET http://localhost:8080/api/loans?projection=totalAmount
@Data
@Entity
public class Loan {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
private String loanTitle;
@OneToMany(cascade = CascadeType.ALL, orphanRemoval = true)
@JoinColumn(name = "loan_id")
private List<Allowance> allowances;
}
@Data
@Entity
public class Allowance {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@ManyToOne
private AllowanceType allowanceType;
private Double allowanceAmount;
}
@Projection(name = "studyLoanSingle", types = {Loan.class})
public interface LoanProjection {
String getLoanTitle();
List<AllowanceProjection> getAllowances();
}
@Value
using SpEL expressions to expose synthetic properties. Even invoke methods on other Spring beans and hand over the target to it for use in advanced calculations.LoanRepo
bean method (for example) that calculate the total amount of the given loan:@Query("select sum(a.allowanceAmount) as amount from Loan l join l.allowances a where l = ?1")
Double getTotalAmountByLoan(Loan loan);
@Projection(name = "totalAmount", types = Loan.class)
public interface LoanTotalAmount {
@Value("#{target}")
Loan getLoan();
@Value("#{@loanRepo.getTotalAmountByLoan(target)}")
Double getAmount();
}
GET http://localhost:8080/api/loans?projection=totalAmount