Spring Data和Oracle从查询返回不同的结果?为什么?

时间:2019-02-04 21:07:09

标签: java oracle spring-boot spring-data-jpa

我的JPA查询非常复杂,因为我正在使用nativequery,但是当我运行它时,它会返回与JAVA查询和Oracle查询不同的结果。但是,Oracle查询返回正确的结果。这可能是什么原因?

我更改了SELECT语句,尝试通过a。*提取所有它们,还尝试仅提取必要的列。我还删除了DATE比较语句。但是有趣的是,JPA查询返回4行,并且它们完全相同。我还尝试在DATE比较部分中获取不带TRUNC关键字的数据,但无法正常工作。另外,我在本机查询的表前面添加了架构名称,这不再起作用。

public Optional<LocalDateTime> getPaymentPeriod2(LocalDate reportDate, String loanId)
{



final String reportDateParemeter = reportDate.format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));

    final String query = "SELECT st.t_no, st.islem_tarihi, st.s_kodu, st.odeme_suresi " +
                "FROM k_tarihce st, k_tanim sta, kt_tanim tta " +
                "WHERE st.s_kodu = sta.s_kodu " +
                "AND tta.s_tipi = sta.s_tipi AND tta.t_kodu = st.t_kodu " +
                "AND st.t_no = :loanId AND st.t_kodu in (37,55) " +
                "AND st.s_kodu = 'K1' AND (TRUNC(st.odeme_suresi) >= TRUNC(TO_DATE(:reportDateParemeter,'yyyy-mm-dd')))";

    final Query loanGetPaymentPeriodQuery = entityManager.createNativeQuery(query, LoanStatusHistory.class);
        loanGetPaymentPeriodQuery.setParameter("reportDateParemeter", reportDateParemeter);
        loanGetPaymentPeriodQuery.setParameter("loanId", loanId);
        List<LoanStatusHistory> loanStatusHistoryList = loanGetPaymentPeriodQuery.getResultList();

        return loanStatusHistoryList.isEmpty() ? Optional.empty() : Optional.of(loanStatusHistoryList.get(0).getPaymentPeriod());
    }

我的实体是:

@Entity
@Table(name = "K_TARIHCE", schema = "LOS")
@Getter
@Setter
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Cacheable(false)
@IdClass(LoanStatusHistoryId.class)
public class LoanStatusHistory {
    @Id
    @Column(name = "T_NO")
    private String loanId;
    @Id
    @Column(name = "ISLEM_TARIHI")
    private LocalDate processDate;
    @Id
    @Column(name = "S_KODU")
    private String statusCode;
    @Column(name = "ODEME_SURESI")
    private LocalDateTime paymentPeriod;
    @Column(name = "T_KODU")
    private String historyCode;
}

class LoanStatusHistoryId implements Serializable {
    private String loanId;
    private transient LocalDate processDate;
    private String statusCode;
}

我希望与Oracle SQL Developer取得相同的结果。我的主要目标是获取ODEME_SURESI字段。

1 个答案:

答案 0 :(得分:0)

我再次找到了解决方法!在下面的新代码中,我没有将本机查询与实体映射,而是返回了一些LocalDate。

final Query loanGetPaymentPeriodQuery = entityManager.createNativeQuery(query);
            loanGetPaymentPeriodQuery.setParameter("reportDateParemeter", reportDateParemeter);
            loanGetPaymentPeriodQuery.setParameter("loanId", loanId);
            final List<LocalDate> loanStatusHistoryList = loanGetPaymentPeriodQuery.getResultList();

            return loanStatusHistoryList.isEmpty() ? Optional.empty() : Optional.of(loanStatusHistoryList.get(0));

这是我所做的唯一更改。我仍然不知道它为什么起作用?。