【问题标题】:How to use JpaRepository operations (findOne) when having a native query with union all statment使用 union all 语句进行本机查询时如何使用 JpaRepository 操作 (findOne)
【发布时间】:2014-11-10 04:16:23
【问题描述】:

我需要将 JpaRepository 与包含联合语句的查询一起使用。可能吗?这是我目前的实现和我到目前为止得到的错误:

实体:

@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);
}

错误:

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 作为 cardnumber , paymentdet0_.cardtype as cardtype2_0_, paymentdet0_.expirydate as expirydate2_0_, paymentdet0_.issuenumber as issuenu11_2_0_, paymentdet0_.nameoncard as nameoncard2_0_, paymentdet0_.securitycode as securit13_2_0_, paymentdet0_.startdate as startdate2_0_, paymentdet0_.rowtype as rowtype2_0_ ?];嵌套异常是 org.hibernate.exception.SQLGrammarException:无法加载实体:[com.bskyb.repository.payments.model.PaymentDetails#cardId] java.sql.SQLSyntaxErrorException: ORA-00942: 表或视图不存在

【问题讨论】:

    标签: spring spring-data-jpa


    【解决方案1】:

    您展示的所有内容似乎都与您看到的异常没有太大关系:

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

    【讨论】:

    • 我尝试了另一种方法并使用 TABLE_PER_CLASS 继承来处理“union”语句。有了这个解决方案,它就完美了。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-24
    • 2018-01-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多