This example shows how to use EasyORM to update a single record. The usual procedure involves retrieving a single record by using the getRecordsForParamQuery() method and then performing an update on that record. You can also update multiple records by using a concept called delegated persistence (see code in example 2).

Single record update example code:


DBSelect dbSelect = new DBSelect();
HashMap<String,Object> hMap=new HashMap<String,Object>();
hMap.put("empId", Integer.parseInt((String)session.getAttribute("recId")));
List<EmployeeDB> employeeList = dbSelect.getRecordsForParamQuery("select * from employee where id=:empId", hMap, EmployeeDB.class, 0, 0,"id");
EmployeeDB employee=employeeList.get(0);
employee.setSurame(request.getParameter("surname").trim());
employee.setYearsAtWork(Integer.parseInt(request.getParameter("years").trim()));
employee.setDepartment(request.getParameter("dept").trim());
employee.setDateUpdated(java.sql.Date.valueOf("2015-08-01"));
employee.update();


Run this example (click the Modify)

You can also update multiple records all at once by using a concept called delegated persistence. This involves getting a range of records for update and calling the updateRange() method on only one of them (any one will do).


Multiple record update example code:

String query="Select id from employee where department='Finances'";
List<EmployeeDB> employeeList = dbSelect.getRecordsFromCustomQuery(query, EmployeeDB.class, 0, 0,"id");
Integer [] ids=new Integer[employeeList.size()];
int i=0;
for(EmployeeDB emp:employeeList){
emp.setDateUpdated(new Date());
emp.setDepartment("Financial");
//emp.update();this would be inefficient
ids[i++]=emp.getId();
}
EmployeeDB emp=employeeList.get(0);
emp.updateRange(ids);