Hi,
I was testing Hibernate NativeQuery vs JDBC and found JDBC is much faster as shown in my code below:
This is my hibernate/JPA code
long timeTx = 0;
long startTx = System.currentTimeMillis();
List batch = null;
String query = "SELECT "
+ "e.id AS ereturn_id"
+ ", e.globalId"
+ ", e.rma AS rma"
+ ", e.trackingNumber"
+ ", e.pieces AS pieces"
+ ", e.weight AS weight"
// + ", e.groupName"
+ ", pi.id AS product_item_id"
+ ", pi.returnAction"
+ ", pi.price"
// + ", pi.groupName"
// + ", r.creationtime AS receptacle_creationtime"
// + ", r.type AS receptacle_type"
// + ", r.id AS receptacle_id"
+ ", pd.sku AS sku"
+ ", shipper.id AS shipper_id"
+ ", shipper.firstName AS shipper_first_name"
+ ", shipper.lastName AS shipper_last_name"
+ ", shipper.address AS shipper_address"
+ ", shipper.state AS shipper_state"
+ ", shipper.city AS shipper_city"
+ ", shipper.country AS shipper_country"
+ ", shipper.postalcode AS shipper_postalcode"
+ ", shipper.contactNumber AS shipper_contactNumber"
+ ", consignee.id AS consignee_id"
+ ", consignee.firstName AS consignee_first_name"
+ ", consignee.lastName AS consignee_last_name"
+ ", carrier.id AS carrier_id"
+ ", carrier.firstName AS carrier_first_name"
+ ", carrier.city AS carrier_address"
+ ", carrier.city AS carrier_city"
+ ", carrier.state AS carrier_state"
+ ", carrier.country AS carrier_country"
+ ", carrier.postalcode AS carrier_postalcode"
+ " FROM"
+ " product_item pi"
+ " JOIN ereturn e ON pi.ereturn = e.id"
+ " JOIN user shipper ON e.shipper = shipper.id"
+ " JOIN user consignee ON e.consignee = consignee.id"
+ " JOIN user carrier ON e.carrier = carrier.id"
+ " JOIN product_definition pd ON pi.product = pd.id"
// + " LEFT JOIN receptacle r ON pi.receptacle = r.id"
+ " WHERE"
+ " pi.receptacle IS NULL"
// + " r.masterCrossDock IS NULL"
+ " AND carrier.email = :carrierEmail"
+ " AND e.scanDateTime IS NOT NULL"
+ " AND e.status = 'RECEIVED'";
if (shipperEmail != null && !shipperEmail.isEmpty()) {
query += " AND shipper.id = :shipper";
}
if (returnAction != null && !returnAction.isEmpty()) {
query += " AND pi.returnAction = :returnAction";
}
Query q = em.createNativeQuery(query, "ProductItemBatchMapping");
q.setParameter("carrierEmail", issuer);
if (shipperEmail != null && !shipperEmail.isEmpty()) {
User shipper = UserDAO.fetch(shipperEmail, UserType.SHIPPER); // Another SHIT!!! change shipper email for id
// so we can save this query
q.setParameter("shipper", shipper.getId());
}
if (returnAction != null && !returnAction.isEmpty()) {
q.setParameter("returnAction", returnAction);
}
batch = q.getResultList();
long endTx = System.currentTimeMillis();
timeTx = endTx - startTx;
logger.info("How long does it takes to retrieve List<ProductItem> objects from JPA? " + timeTx);
This is my JDBC code:
public List<ProductItem> getEreturn(String carrierEmail, String shipperEmail, String returnAction) throws SQLException {
List<ProductItem> productItems = new ArrayList();
String sql = "SELECT "
+ "e.id AS ereturn_id"
+ ", e.globalId"
+ ", e.rma AS rma"
+ ", e.trackingNumber"
+ ", e.pieces AS pieces"
+ ", e.weight AS weight"
// + ", e.groupName"
+ ", pi.id AS product_item_id"
+ ", pi.returnAction"
+ ", SUM(pi.price)"
// + ", pi.groupName"
// + ", r.creationtime AS receptacle_creationtime"
// + ", r.type AS receptacle_type"
// + ", r.id AS receptacle_id"
+ ", pd.sku AS sku"
+ ", shipper.id AS shipper_id"
+ ", shipper.firstName AS shipper_first_name"
+ ", shipper.lastName AS shipper_last_name"
+ ", shipper.address AS shipper_address"
+ ", shipper.state AS shipper_state"
+ ", shipper.city AS shipper_city"
+ ", shipper.country AS shipper_country"
+ ", shipper.postalcode AS shipper_postalcode"
+ ", shipper.contactNumber AS shipper_contactNumber"
+ ", consignee.id AS consignee_id"
+ ", consignee.firstName AS consignee_first_name"
+ ", consignee.lastName AS consignee_last_name"
+ ", carrier.id AS carrier_id"
+ ", carrier.firstName AS carrier_first_name"
+ ", carrier.city AS carrier_address"
+ ", carrier.city AS carrier_city"
+ ", carrier.state AS carrier_state"
+ ", carrier.country AS carrier_country"
+ ", carrier.postalcode AS carrier_postalcode"
+ " FROM"
+ " product_item pi"
+ " JOIN ereturn e ON pi.ereturn = e.id"
+ " JOIN user shipper ON e.shipper = shipper.id"
+ " JOIN user consignee ON e.consignee = consignee.id"
+ " JOIN user carrier ON e.carrier = carrier.id"
+ " JOIN product_definition pd ON pi.product = pd.id"
// + " LEFT JOIN receptacle r ON pi.receptacle = r.id"
+ " WHERE"
+ " pi.receptacle IS NULL"
// + " r.masterCrossDock IS NULL"
+ " AND carrier.email = ?"
+ " AND e.scanDateTime IS NOT NULL"
+ " AND e.status = 'RECEIVED'";
if (shipperEmail != null && !shipperEmail.isEmpty()) {
sql += " AND shipper.id = ?";
}
if (returnAction != null && !returnAction.isEmpty()) {
sql += " AND pi.returnAction = ?";
}
PreparedStatement stmt = conn.prepareStatement(sql);
stmt.setString(1, carrierEmail);
if (shipperEmail != null && !shipperEmail.isEmpty()) {
User shipper = UserDAO.fetch(shipperEmail, UserType.SHIPPER); // TODO: change shipper email for id
// so we can save this query
stmt.setLong(2, shipper.getId());
}
if (returnAction != null && !returnAction.isEmpty()) {
stmt.setString(3, returnAction);
}
ResultSet rs = stmt.executeQuery();
while (rs.next()) {
productItems.add(new ProductItem(rs));
}
return productItems;
}
Time to fetch data using hibernate/JPA nativequery => ~3.6 seconds
Time to fetch data using JDBC => 46 ms
QuestionL Why is hibernate so much lower compared to JDBC? is this normal?