早上好,我对此有疑问。我有一个涉及3个表的SQL查询:贷方,付款和账单
从信用中选择c.date,c.pay_number,c.cost c在c.payment_id = p.id上加入付款p在b.id = p.bill_id上加入账单b其中b.id =:b.id;
当我将此查询放入dbeaver中时,它可以很好地工作,但是我需要一些帮助来转换为正确的JPA句子。
这是帐单实体:
@Entity
@Data
public class Bill {
@Id
@GeneratedValue
private UUID id;
private long dateCreated;
private boolean active;
private String billSeq;
private String stringSeq;
private String priceType;
private double iva;
private double ice;
private double total;
private double subTotal;
private double baseNoTaxes;
private double baseTaxes;
private String ordenDecompra;
private String srr;
private String lugar;
private String otir;
private double discount;
private int discountPercentage;
private String claveDeAcceso;
private String idSri;
private int typeDocument;
private String quotationSeq;
private String stringQuotationSeq;
@ManyToOne
@JoinColumn(name = "client_id")
private Client client;
@ManyToOne
@JoinColumn(name = "user_id")
private UserIntegridad userIntegridad;
@ManyToOne
@JoinColumn(name = "subsidiary_id")
private Subsidiary subsidiary;
@OneToMany(mappedBy = "bill", cascade = CascadeType.ALL)
private List<Detail> details;
@OneToMany(mappedBy = "bill", cascade = CascadeType.ALL)
private List<Payment> payment;
public void setListsNull(){
details = null;
payment = null;
};
public void setFatherListToNull(){
client.setListsNull();
client.setFatherListToNull();
userIntegridad.setListsNull();
userIntegridad.setFatherListToNull();
subsidiary.setListsNull();
subsidiary.setFatherListToNull();
};
@Transient
public static Bill newBillTest(){
Bill bill = new Bill();
bill.setDetails(new ArrayList<>());
bill.setPagos(new ArrayList<>());
bill.setClient(Client.newClientTest());
bill.setUserIntegridad(UserIntegridad.newUserIntegridadTest());
bill.setSubsidiary(Subsidiary.newSubsidiaryTest());
return bill;
};
}
这是付款实体:
@Entity
@Data
public class Payment implements Child {
@Id
@GeneratedValue
private UUID id;
private String medio;
private double total;
private String payForm;
private String cardBrand;
private String chequeAccount;
private String chequeBank;
private String chequeNumber;
private String chequeDiasPlazo;
private long fechaCobro;
private int creditoIntervalos;
private int creditoNumeroPagos;
@OneToMany(mappedBy = "payment", cascade = CascadeType.ALL)
private List<Credits> credits;
@ManyToOne
@JoinColumn(name = "bill_id")
private Bill bill;
public void setListsNull(){
credits = null;
}
public void setFatherListToNull(){
bill.setListsNull();
bill.setFatherListToNull();
}
public static Payment newPaymentTest(){
Payment payment = new Payment();
payment.setBill(Bill.newBillTest());
return payment;
}
}
这是信用实体:
@Entity
@Data
public class Credits implements Child {
@Id
@GeneratedValue
private UUID id;
private int diasPlazo;
private long fecha;
private int payNumber;
private double valor;
@ManyToOne
@JoinColumn(name = "payment_id")
private Payment payment;
public void setFatherListToNull(){
payment.setListsNull();
payment.setFatherListToNull();
}
public static Credits newCreditsTest(){
Credits credit = new Credits();
credit.setPayment(Payment.newPaymentTest());
return credit;
}
}
系统流程从生成发票时开始,付款类型记录在付款表中,如果以现金或贷方付款,则如果付款为贷方,则信息存储在表贷方中,因此我需要通过仅输入发票ID从表积分中提取积分表中存储的所有信息。
因此,在存储库(CreditsRepository)中,我这样做:
@Repository
@Qualifier(value="CreditsRepository")
public interface CreditsRepository extends CrudRepository<Credits, UUID>{
Iterable<Credits> findByPago(Pago pago);
@Query("SELECT c FROM Credits c JOIN c.payment p JOIN p.bill b WHERE b.id = :id")
Iterable<Credits> findCreditsOfBillByBillId(@Param("id") UUID id);
}
所以我需要帮助来制作一个好的@Query
答案 0 :(得分:3)
您的@Query
绝对不错。您还可以删除@Query
并仅使用spring-data-jpa
等效方法名称,如下所示:
//no annotation here
public interface CreditsRepository extends CrudRepository<Credits, UUID> {
//if you want to pass Bill object
List<Credits> findAllByPaymentBill(Bill bill);
//if you want to pass an id (UUID)
List<Credits> findAllByPaymentBillId(UUID id);
}
关于代码的一些事情。
如果扩展CrudRepository
,则不需要@Repository
注释,并且由于您正在编写接口,因此也不需要@Qualifier
。
您只需在服务中@Autowire
@Autowired
private CreditsRepository creditsRepository;