Skip to content

Commit 22bd1a6

Browse files
author
bwajtr
committed
Implementation of scenario "Update single existing entity - update all fields of entity at once"
1 parent 871c52f commit 22bd1a6

5 files changed

Lines changed: 63 additions & 3 deletions

File tree

dbscenarios.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
2. Fetch list of entities based on condition
33
3. Save new single entity and return primary key
44
4. Batch insert multiple entities of same type and return generated keys
5-
5. Update single existing entity - update one field of the entity
5+
5. Update single existing entity - update all fields of entity at once
66
6. Fetch entity and it's manytoone relation (Company for Employee)
77
7. Fetch entity and it's onetomany relation (Employees for Company)
88
8. Update entitie's onetomany relation - add two items, update two items and delete one item

src/main/java/com/clevergang/dbtests/Scenarios.java

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
package com.clevergang.dbtests;
22

3+
import java.math.BigDecimal;
34
import java.time.LocalDate;
45
import java.util.ArrayList;
56
import java.util.List;
67

8+
import com.clevergang.dbtests.model.Employee;
79
import com.clevergang.dbtests.model.Project;
810
import com.clevergang.dbtests.service.repository.DataRepository;
911
import org.apache.commons.lang3.RandomStringUtils;
@@ -72,9 +74,22 @@ public void scenarioFour() {
7274
}
7375

7476
/**
75-
* Update single existing entity - update one field of the entity
77+
* Update single existing entity - update all fields of entity at once
7678
*/
7779
public void scenarioFive() {
80+
// Complete already store entity with all fields set - imagine that this object comes from UI edit dialog,
81+
// which is typical scenario
82+
Employee employeeToUpdate = new Employee();
83+
employeeToUpdate.setPid(1);
84+
employeeToUpdate.setDepartmentPid(1);
85+
employeeToUpdate.setName("Curt");
86+
employeeToUpdate.setSurname("Odegaard");
87+
employeeToUpdate.setEmail("curt.odegaard@updated.com"); // <-- this is updated value
88+
employeeToUpdate.setSalary(new BigDecimal("15000")); // <-- this is updated value
89+
90+
// now update the employee in DB
91+
repository.updateEmployee(employeeToUpdate);
92+
7893
}
7994

8095
public void scenarioSix() {

src/main/java/com/clevergang/dbtests/service/repository/DataRepository.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,5 @@ public interface DataRepository {
1919

2020
List<Integer> insertAllProjects(List<Project> projects);
2121

22+
void updateEmployee(Employee employeeToUpdate);
2223
}

src/main/java/com/clevergang/dbtests/service/repository/impl/jdbctemplate/JDBCDataRepositoryImpl.java

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,30 @@ public List<Integer> insertAllProjects(List<Project> projects) {
103103

104104
jdbcTemplate.batchUpdate(insertStatement, paramsList);
105105

106-
// FIXME JDBCTemplate cannot return IDs for batch update!!
106+
// FIXME JDBCTemplate cannot return IDs for every row after batch update!!
107107
return null;
108108
}
109+
110+
@Override
111+
public void updateEmployee(Employee employeeToUpdate) {
112+
logger.info("Updating employee using JDBC Template");
113+
114+
String updateStatement = " UPDATE EMPLOYEE SET " +
115+
" department_pid = :department_pid, " +
116+
" name = :name," +
117+
" surname = :surname," +
118+
" email = :email," +
119+
" salary = :salary" +
120+
" WHERE pid = :pid";
121+
122+
MapSqlParameterSource params = new MapSqlParameterSource();
123+
params.addValue("department_pid", employeeToUpdate.getDepartmentPid());
124+
params.addValue("name", employeeToUpdate.getName());
125+
params.addValue("surname", employeeToUpdate.getSurname());
126+
params.addValue("email", employeeToUpdate.getEmail());
127+
params.addValue("salary", employeeToUpdate.getSalary());
128+
params.addValue("pid", employeeToUpdate.getPid());
129+
130+
jdbcTemplate.update(updateStatement, params);
131+
}
109132
}

src/main/java/com/clevergang/dbtests/service/repository/impl/jooq/JooqDataRepositoryImpl.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import com.clevergang.dbtests.model.Project;
1010
import com.clevergang.dbtests.service.repository.DataRepository;
1111
import com.clevergang.dbtests.service.repository.impl.ImplBasedOn;
12+
import com.clevergang.dbtests.service.repository.impl.jooq.generated.tables.records.EmployeeRecord;
1213
import org.jooq.BatchBindStep;
1314
import org.jooq.DSLContext;
1415
import org.slf4j.Logger;
@@ -98,5 +99,25 @@ public List<Integer> insertAllProjects(List<Project> projects) {
9899
return null;
99100
}
100101

102+
@Override
103+
public void updateEmployee(Employee employeeToUpdate) {
104+
logger.info("Updating employee using JOOQ");
105+
106+
// If the DTO object passed into this method field names match field names in the EmployeeRecord,
107+
// then it's easier to use Records when updating all fields of the entity at once:
108+
EmployeeRecord record = create.newRecord(EMPLOYEE, employeeToUpdate);
109+
create.executeUpdate(record);
110+
111+
// If the DTO object fields does not match the employee record, then you'll have to use mapping:
112+
create.update(EMPLOYEE)
113+
.set(EMPLOYEE.DEPARTMENT_PID, employeeToUpdate.getDepartmentPid())
114+
.set(EMPLOYEE.NAME, employeeToUpdate.getName())
115+
.set(EMPLOYEE.SURNAME, employeeToUpdate.getSurname())
116+
.set(EMPLOYEE.EMAIL, employeeToUpdate.getEmail())
117+
.set(EMPLOYEE.SALARY, employeeToUpdate.getSalary())
118+
.where(EMPLOYEE.PID.eq(employeeToUpdate.getPid())) // <-- Do not forget to add where condition!
119+
.execute();
120+
}
121+
101122
}
102123

0 commit comments

Comments
 (0)