Spring 当使用union all语句进行本机查询时,如何使用JpaRepository操作(findOne)
我需要将JpaRepository与一个包含union语句的查询一起使用。可能吗?这是我当前的实现和到目前为止遇到的错误: 实体:Spring 当使用union all语句进行本机查询时,如何使用JpaRepository操作(findOne),spring,spring-data-jpa,Spring,Spring Data Jpa,我需要将JpaRepository与一个包含union语句的查询一起使用。可能吗?这是我当前的实现和到目前为止遇到的错误: 实体: @Entity public class PaymentDetails { @Id @Column private String id; @Enumerated(EnumType.STRING) @Column(name = "rowtype") private PaymentType paymentType; @Embedded p
@Entity
public class PaymentDetails {
@Id
@Column
private String id;
@Enumerated(EnumType.STRING)
@Column(name = "rowtype")
private PaymentType paymentType;
@Embedded
private CardDetails cardDetails;
@Embedded
private BankAccountDetails bankAccountDetails;
界面和查询:
public interface PaymentsRepositoryManagerPayment1 extends JpaRepository<PaymentDetails,String> {
@Query(value = "select id,'CARD' as rowtype, cardnumber, cardtype, nameoncard, expirydate, startdate, securitycode, semafonecr, issuenumber, "
+ "null accountnumber, null accountholdername, null sortcode, null bic, null iban, null currency from pcs.BsbTempCardDetails "
+ "where id = :id union all select id, 'BANK' as rowtype, null cardnumber, null cardtype, null nameoncard, null expirydate, null startdate, "
+ "null securitycode, null semafonecr, null issuenumber, accountnumber, accountholdername, sortcode, bic, iban, currency "
+ "from pcs.BsbTempBankAccountDetails where id = :id", nativeQuery = true) <br>
List< PaymentDetails > findPaymentDetails(@Param("id") String id);
错误:
org.springframework.dao.InvalidDataAccessResourceUsageException:无法加载实体:[com.bskyb.repository.payments.model.PaymentDetailscardId];SQL[选择paymentdet0\u0.id作为id2\u0\u0,paymentdet0\u0.accountnumber作为accounth2\u2\u0,paymentdet0\u0.accountnumber作为accountn3\u2\u0\u0,paymentdet0\u0.bic作为bic2\u0\u0,paymentdet0\u0.currency2\u0\u0,paymentdet0\u0.iban作为iban2\u0,PaymentDetDet0\u0.sortcode作为Sorde2\u2\u0,PaymentDetDet0.Card0\u0.Card0\u2\u0.Card0类型_,paymentdet0_u0.expirydate作为expirydate 2_0,paymentdet0.issuenumber作为issuenu11_2_0,paymentdet0.nameoncard作为nameoncard2_0,paymentdet0.securitycode作为Security13_2_0,paymentdet0.startdate作为startdate 2_0,paymentdet0;嵌套异常为org.hibernate.exception.sqlgrammareexception:无法加载实体:[com.bskyb.repository.payments.model.PaymentDetails]
java.sql.SQLSyntaxErrorException:ORA-00942:表或视图不存在
您显示的所有内容似乎与您看到的异常不太相关: 异常投诉表不可用。请确保在发出查询时PaymentDetails存在这可能是您看到异常的原因。 您调用findOne…因此findPaymentDetails上的查询声明…根本不适用于用例。
我尝试了另一种方法,并使用表\每\类继承来处理union语句。有了这个解决方案,它工作得非常完美。
@Autowired private PaymentsRepositoryManagerPayment1 paymentsRepositoryManagerPayment1;
@Transactional(value = "paymentsRepositoryTransactionManager")
public PaymentDetails retrievePaymentDetailsById1(String id) {
return paymentsRepositoryManagerPayment1.findOne(id);
}