EDIT 3:
@Query(value = "SELECT REP_BAL_QTY\r\n" + "FROM ODH_INV.SBT_INVEN\r\n" + "WHERE CUST_NBR = RIGHT(CONCAT('0000000000', CAST(:customerNumber AS varchar)), 10) " + "AND EAN_UPC = RIGHT(CONCAT('0000000000000', CAST(:eanUpc AS varchar)), 13)", nativeQuery = true) Optional<Integer> findReportedBalanceQuantityByCustomerNumberAndEanUpc(String customerNumber, String eanUpc); So this is working fine inside a repository, but in this case it's an API I'm exposing, and I only need the REP_BAL_QTY column in the result, so I have to run this query for each row of the page I'm returning. Before I had this mapped in the entity:
@MapsId @OneToOne @JoinColumns({ @JoinColumn(name = "EAN_UPC", referencedColumnName = "EAN_UPC"), @JoinColumn(name = "CUST_NBR", referencedColumnName = "CUST_NBR") }) @NotFound(action = NotFoundAction.IGNORE) private SbtInventory sbtInventory; This was called when the entity was loaded and the process is a bit faster, I think ( besides the obvious, avoiding the boilerplate code ) Using the join in the entity, the process to bring a page of 10 rows lasts:
SbtInventoryController.getData took 84113 ms
This is one of the executed queries for the join:
select sbtinvento0_.CUST_NBR as CUST_NBR1_19_0_, sbtinvento0_.EAN_UPC as EAN_UPC2_19_0_, sbtinvento0_.REP_BAL_QTY as REP_BAL_3_19_0_ from ODH_INV.SBT_INVEN sbtinvento0_ where sbtinvento0_.CUST_NBR=? and sbtinvento0_.EAN_UPC=? Using the query, the process lasts:
SbtInventoryController.getData took 14063 ms
So it's a huge performance jump. The downside is that by using the query, I have to drop sorting on the column holding REP_BAL_QTY :(