Skip to content

Commit 9200ea5

Browse files
author
bwajtr
committed
Implementation of scenario six + renaming of badly named property in Department model class + improved english grammar in comments ;)
1 parent 399fc3a commit 9200ea5

6 files changed

Lines changed: 55 additions & 15 deletions

File tree

dbscenarios.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
3. Save new single entity and return primary key
44
4. Batch insert multiple entities of same type and return generated keys
55
5. Update single existing entity - update all fields of entity at once
6-
6. Fetch entity and it's manytoone relation (Company for Employee)
7-
7. Fetch entity and it's onetomany relation (Employees for Company)
8-
8. Update entitie's onetomany relation - add two items, update two items and delete one item
6+
6. Fetch entity and it's many-to-one relation (Company for Department)
7+
7. Fetch entity and it's one-to-many relation (Departments for Company)
8+
8. Update entitie's one-to-many relation - add two items, update two items and delete one item
99
9. Complex select - construct select where conditions based on some boolean conditions + throw in some outer joins
1010
10. Transactions handling
1111
11. Call stored procedure

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
import java.util.ArrayList;
66
import java.util.List;
77

8+
import com.clevergang.dbtests.model.Company;
9+
import com.clevergang.dbtests.model.Department;
810
import com.clevergang.dbtests.model.Employee;
911
import com.clevergang.dbtests.model.Project;
1012
import com.clevergang.dbtests.repository.DataRepository;
@@ -92,8 +94,20 @@ public void scenarioFive() {
9294

9395
}
9496

97+
/**
98+
* Fetch many-to-one relation (Company for Department)
99+
*/
95100
public void scenarioSix() {
101+
Department softwareDevelopmentDepartment = repository.findDepartment(3);
102+
103+
// Getting Company for Department (many-to-one relation) in JPA is quite easy. You typically have
104+
// @ManyToOne relation defined in the Department entity class, so once you have instance of Department,
105+
// you just call department.getCompany() and JPA does the magic for you (typically one or more lazy selects
106+
// are executed). We don't have any such magical call here, but non-JPA approach is quite straightforward
107+
// too: we have company_pid, so just ask DataRepository for the record:
108+
Company company = repository.findCompany(softwareDevelopmentDepartment.getCompanyPid());
96109

110+
logger.info("Department {} is in the {} company", softwareDevelopmentDepartment.getName(), company.getName());
97111
}
98112

99113
public void scenarioSeven() {

src/main/java/com/clevergang/dbtests/model/Department.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
*/
66
public class Department {
77
private Integer pid;
8-
private Integer department_pid;
8+
private Integer company_pid;
99
private String name;
1010

1111
public Integer getPid() {
@@ -24,12 +24,12 @@ public void setName(String name) {
2424
this.name = name;
2525
}
2626

27-
public Integer getDepartmentPid() {
28-
return department_pid;
27+
public Integer getCompanyPid() {
28+
return company_pid;
2929
}
3030

31-
public void setDepartmentPid(Integer department_pid) {
32-
this.department_pid = department_pid;
31+
public void setCompanyPid(Integer company_pid) {
32+
this.company_pid = company_pid;
3333
}
3434

3535
@Override
@@ -43,7 +43,7 @@ public boolean equals(Object o) {
4343

4444
if (pid != null ? !pid.equals(that.pid) : that.pid != null)
4545
return false;
46-
if (department_pid != null ? !department_pid.equals(that.department_pid) : that.department_pid != null)
46+
if (company_pid != null ? !company_pid.equals(that.company_pid) : that.company_pid != null)
4747
return false;
4848
return name != null ? name.equals(that.name) : that.name == null;
4949

@@ -52,7 +52,7 @@ public boolean equals(Object o) {
5252
@Override
5353
public int hashCode() {
5454
int result = pid != null ? pid.hashCode() : 0;
55-
result = 31 * result + (department_pid != null ? department_pid.hashCode() : 0);
55+
result = 31 * result + (company_pid != null ? company_pid.hashCode() : 0);
5656
result = 31 * result + (name != null ? name.hashCode() : 0);
5757
return result;
5858
}
@@ -61,7 +61,7 @@ public int hashCode() {
6161
public String toString() {
6262
return "Department{" +
6363
"pid=" + pid +
64-
", department_pid=" + department_pid +
64+
", company_pid=" + company_pid +
6565
", name='" + name + '\'' +
6666
'}';
6767
}

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import java.util.List;
44

55
import com.clevergang.dbtests.model.Company;
6+
import com.clevergang.dbtests.model.Department;
67
import com.clevergang.dbtests.model.Employee;
78
import com.clevergang.dbtests.model.Project;
89

@@ -13,6 +14,8 @@ public interface DataRepository {
1314

1415
Company findCompany(Integer pid);
1516

17+
Department findDepartment(Integer pid);
18+
1619
List<Employee> employeesWithSalaryGreaterThan(Integer minSalary);
1720

1821
Integer insertProject(Project project);

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import java.util.Map;
77

88
import com.clevergang.dbtests.model.Company;
9+
import com.clevergang.dbtests.model.Department;
910
import com.clevergang.dbtests.model.Employee;
1011
import com.clevergang.dbtests.model.Project;
1112
import com.clevergang.dbtests.repository.DataRepository;
@@ -48,6 +49,18 @@ public Company findCompany(Integer pid) {
4849
return company;
4950
}
5051

52+
@Override
53+
public Department findDepartment(Integer pid) {
54+
String query = "SELECT pid, company_pid, name" +
55+
" FROM department " +
56+
" WHERE pid = :pid";
57+
58+
Map<String, Object> params = new HashMap<>();
59+
params.put("pid", pid);
60+
61+
return jdbcTemplate.queryForObject(query, params, new BeanPropertyRowMapper<>(Department.class));
62+
}
63+
5164
@Override
5265
public List<Employee> employeesWithSalaryGreaterThan(Integer minSalary) {
5366
logger.info("Looking for employeesWithSalaryGreaterThan using JDBCTemplate");

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

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import java.util.List;
66

77
import com.clevergang.dbtests.model.Company;
8+
import com.clevergang.dbtests.model.Department;
89
import com.clevergang.dbtests.model.Employee;
910
import com.clevergang.dbtests.model.Project;
1011
import com.clevergang.dbtests.repository.DataRepository;
@@ -44,6 +45,14 @@ public Company findCompany(Integer pid) {
4445
return company;
4546
}
4647

48+
@Override
49+
public Department findDepartment(Integer pid) {
50+
return create.
51+
selectFrom(DEPARTMENT)
52+
.where(DEPARTMENT.PID.eq(pid))
53+
.fetchOneInto(Department.class);
54+
}
55+
4756
@Override
4857
public List<Employee> employeesWithSalaryGreaterThan(Integer minSalary) {
4958
logger.info("Looking for employeesWithSalaryGreaterThan using JOOQ");
@@ -100,12 +109,13 @@ public List<Integer> insertAllProjects(List<Project> projects) {
100109
public void updateEmployee(Employee employeeToUpdate) {
101110
logger.info("Updating employee using JOOQ");
102111

103-
// If the DTO object passed into this method field names match field names in the EmployeeRecord,
104-
// then it's easier to use Records when updating all fields of the entity at once:
105-
EmployeeRecord record = create.newRecord(EMPLOYEE, employeeToUpdate);
112+
// If field names of the DTO object passed into this method match field names in the jooq Record class,
113+
// then it's easier to use jooq Records when updating all fields of the entity at once (Employee field match EmployeeRecord fields):
114+
EmployeeRecord record = new EmployeeRecord();
115+
record.from(employeeToUpdate); // <-- reflection based mapping, but you can use JPA @Column annotations too!
106116
create.executeUpdate(record);
107117

108-
// If the DTO object fields does not match the employee record, then you'll have to use mapping:
118+
// If the DTO object fields do not match the employee record, then you'll have to use manual mapping:
109119
create.update(EMPLOYEE)
110120
.set(EMPLOYEE.DEPARTMENT_PID, employeeToUpdate.getDepartmentPid())
111121
.set(EMPLOYEE.NAME, employeeToUpdate.getName())

0 commit comments

Comments
 (0)