Skip to content

Commit 1839d00

Browse files
author
bwajtr
committed
Implementation of scenario 7
1 parent 9200ea5 commit 1839d00

4 files changed

Lines changed: 38 additions & 0 deletions

File tree

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,8 +110,20 @@ public void scenarioSix() {
110110
logger.info("Department {} is in the {} company", softwareDevelopmentDepartment.getName(), company.getName());
111111
}
112112

113+
/**
114+
* Fetch one-to-many relation (Departments for Company)
115+
*/
113116
public void scenarioSeven() {
117+
Company company = repository.findCompany(1);
118+
119+
// For one-to-many relations the situation is quite similar to many-to-one relations (scenario six). In JPA this
120+
// is "easy" - you define @OneToMany relation in the Company entity and then you just call getDepartments() method ->
121+
// a lazy select is issued and Departments are fetched from DB. However you can also use EAGER FetchType strategy which
122+
// causes the relation to load along with the primary entity - this behavior is (by our opinion) the source of all evil
123+
// in JPA... So, in non-JPA approach, we don't have any "eager" loads, just explicit calls for data:
124+
List<Department> departments = repository.getDepartmentsForCompany(company.getPid());
114125

126+
logger.info("There are {} departments in {} company", departments.size(), company.getName());
115127
}
116128

117129
public void scenarioEight() {

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@
88
import com.clevergang.dbtests.model.Project;
99

1010
/**
11+
* In "normal" application we would rather split the repository to multiple classes based on entity
12+
* we want to work with (CompanyRepository, DepartmentRepository etc.). We decided
13+
* to create just a single interface in this project, so different implementations are more easily compared...
14+
*
1115
* @author Bretislav Wajtr <bretislav.wajtr@clevergang.com>
1216
*/
1317
public interface DataRepository {
@@ -23,4 +27,6 @@ public interface DataRepository {
2327
List<Integer> insertAllProjects(List<Project> projects);
2428

2529
void updateEmployee(Employee employeeToUpdate);
30+
31+
List<Department> getDepartmentsForCompany(Integer pid);
2632
}

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,4 +138,16 @@ public void updateEmployee(Employee employeeToUpdate) {
138138

139139
jdbcTemplate.update(updateStatement, params);
140140
}
141+
142+
@Override
143+
public List<Department> getDepartmentsForCompany(Integer pid) {
144+
String query = "SELECT pid, company_pid, name" +
145+
" FROM department " +
146+
" WHERE company_pid = :pid";
147+
148+
Map<String, Object> params = new HashMap<>();
149+
params.put("pid", pid);
150+
151+
return jdbcTemplate.query(query, params, new BeanPropertyRowMapper<>(Department.class));
152+
}
141153
}

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,5 +126,13 @@ public void updateEmployee(Employee employeeToUpdate) {
126126
.execute();
127127
}
128128

129+
@Override
130+
public List<Department> getDepartmentsForCompany(Integer pid) {
131+
return create.
132+
selectFrom(DEPARTMENT)
133+
.where(DEPARTMENT.COMPANY_PID.eq(pid))
134+
.fetchInto(Department.class);
135+
}
136+
129137
}
130138

0 commit comments

Comments
 (0)