1

Is there any other way to reduce the below code snippet. Before we are using Hibernate4 current we changing to Hibernate5(Version 5.3.6.Final) so there are so many changes in Hibernate5. We are new to this version so anyone shares us any code snippet for calling any stored procedure in Hibernate5. We already did it working but we need some standard optimized code.

Here we are using the following method.

 @Override
 public List<LocationCount> getLocationCounts(String userName) {

        StoredProcedureQuery q = getCurrentSession().createStoredProcedureQuery("locationcount");
        q.registerStoredProcedureParameter("userName", String.class, ParameterMode.IN);
        q.setParameter("userName", userName);
        q.execute();

        List<LocationCount> data = new ArrayList<>();
        List<Object[]> results = q.getResultList();

        results.stream().forEach((record) -> {
            LocationCount d = new LocationCount();
            d.setOrgCode((String) record[0]);
            d.setOrgName((String) record[1]);
            d.setLocationCode((String) record[2]);
            d.setLocationName((String) record[3]);
            data.add(d);
        });

        return data;
    }

Thanks

Sitansu

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Sitansu
  • 3,225
  • 8
  • 34
  • 61
  • the code looks standard optimized enough, if you just want it shorter you can: 1. user JPA entity maneger createNamedStoredProcedureQuery instead of createStoredProcedureQuery so you don't have to registerStoredProcedureParameter. 2. create a constructor for LocationCount that accepts OrgCode, OrgName, LocCode and LocName – gherkin Oct 11 '18 at 10:52
  • @gherkin Can you answer this question https://stackoverflow.com/questions/52758885/how-to-get-count-in-hibernate-5-criteriabuilder-with-criteria-condition – Sitansu Oct 11 '18 at 11:32

0 Answers0