如何使用union all statment进行本机查询时如何使用JpaRepository操作(findOne)

时间:2014-09-16 12:08:04

标签: 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
  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);

呼叫:

@Autowired private PaymentsRepositoryManagerPayment1 paymentsRepositoryManagerPayment1;
@Transactional(value = "paymentsRepositoryTransactionManager")
public PaymentDetails retrievePaymentDetailsById1(String id) {
  return paymentsRepositoryManagerPayment1.findOne(id);
}

ERROR:

  

org.springframework.dao.InvalidDataAccessResourceUsageException:无法加载实体:[com.bskyb.repository.payments.model.PaymentDetails#cardId]; SQL [选择paymentdet0_.id为id2_0_,paymentdet0_.accountholdername为accounth2_2_0_,paymentdet0_.accountnumber为accountn3_2_0_,paymentdet0_.bic为bic2_0_,paymentdet0_.currency为currency2_0_,paymentdet0_.iban为iban2_0_,paymentdet0_.sortcode为sortcode2_0_,paymentdet0_.cardnumber为cardnumber2_0_ ,paymentdet0_.cardtype为cardtype2_0_,paymentdet0_.expirydate为expirydate2_0_,paymentdet0_.issuenumber为issuenu11_2_0_,paymentdet0_.nameoncard为nameoncard2_0_,paymentdet0_.securitycode为securit13_2_0_,paymentdet0_.startdate为startdate2_0_,paymentdet0_.rowtype为rowtype2_0_,来自PaymentDetails paymentdet0_,其中paymentdet0_.id = ?]嵌套异常是org.hibernate.exception.SQLGrammarException:无法加载实体:[com.bskyb.repository.payments.model.PaymentDetails#cardId]   java.sql.SQLSyntaxErrorException:ORA-00942:表或视图不存在

1 个答案:

答案 0 :(得分:1)

您展示的所有内容似乎与您看到的例外情况无关:

  1. 关于桌子不可用的例外投诉。发出查询时确保PaymentDetails存在(这可能是 的原因,您看到异常)。
  2. 您致电findOne(…)。因此,findPaymentDetails(…)上的查询声明根本不会影响用例。
相关问题