How to set values for field after executing SQLQuery in Java Spring MVC

I have this code

query = session.createSQLQuery("select e.emp_id, emp_name, 
    emp_salary,address_line1, city, 
    zipcode from Employee e, Address a where a.emp_id=e.emp_id");
rows = query.list();

for(Object[] row : rows){
Employee emp = new Employee();
emp.setId(Long.parseLong(row[0].toString()));
emp.setName(row[1].toString());
emp.setSalary(Double.parseDouble(row[2].toString()));
Address address = new Address();
address.setAddressLine1(row[3].toString());
address.setCity(row[4].toString());
address.setZipcode(row[5].toString());
emp.setAddress(address);
System.out.println(emp);
}

I’m fetching datas from the query and setting it in the for each loop.But I want to set values now like this

emp.setName(row[1].toString());

I want to fetch data with column names.So that if there are 100 of columns in select statement I have to give 0-99 numbering in the row array. So if say, 46th column I don’t want in select query then i have to change the numbering from 46-98. So is there any way to get it done with column names.

Note: Mapping is done through query not entity/pojo class.

#java #sql #spring-boot #hibernate

2.35 GEEK