Skip to content

Commit 7102854

Browse files
author
bwajtr
committed
General simplification - new data model; only single Repository class for all scenarios (so we can better compare two different implementations); refaktored JUnit classes -> scenarios extracted to Scenarios class
1 parent 21832fe commit 7102854

47 files changed

Lines changed: 1035 additions & 1877 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
1-
/jooqgenerator/*.jar
21
/target
32
/.idea

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ We are not comparing performance (that'll be maybe added later), but rather how
55

66
We prepared some common scenarios, which you typically need to implement data-centric application, and then we implemented these scenarios using various non-JPA frameworks.
77

8+
## Model used
9+
10+
11+
812
## Why only non-JPA?
913
Well, we were trying to "stick with standard" in our projects in the past, but after many years of JPA usage
1014
(Hibernate mostly), we realized it's counterproductive. In most of our projects it caused more problems then

SimpleCompanyModel.png

28 KB
Loading

sql-updates/SimpleCompanyModel.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<mxfile type="device" userAgent="Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.75 Safari/537.36" version="5.3.3.6" editor="www.draw.io"><diagram>7Vtfk9o2EP80PDaD8Zm7PAZC2ofrzE14aPvU0WFhK5ElVxbHkU+flbXyH8zF5PC4tCdgBuvn1b/9rbS7FkzCZfb8qyJ5+ruMKZ/MpvHzJPw4mc2Cm/AGvgxysMhtBCUDJIrFKFQDa/aNIjhFdMdiWrQEtZRcs7wNbqQQdKNbGFFK7ttiW8nbveYkcT3WwHpDeBf9g8U6tejdbF7jv1GWpK7nYP7e3nkkm6+JkjuB/U1m4bZ82dsZcW2VEw1XoEQlJTRjrrLnJeVGkU5HVhufXrhbDVJRgQP5cQXk4InwHc5zKbOciIPFC31w8y/2LONEQGmR6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9nObfat3XLBXIXbdJbTOpSwi9bTOQydo/pSXOD/tbTwKLnrzRNdtNfME+3GNEJeGGHnwyrZwMdKhoFV+jeF6yHAZWyDElBWhWkRU8sJ4NKuW34wQOO3U/ir8mr3PP6VeZ/8+5Y8XNgR1KZQTeWUdUCx/oW8Fa//cxCuvgM=</diagram></mxfile>

sql-updates/create-script.sql

Lines changed: 67 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,77 +1,88 @@
11
-- Meant for PostgreSQL --
22

3-
DROP TABLE product;
4-
DROP TABLE product_category;
5-
DROP TABLE employee_department;
6-
DROP TABLE department;
7-
DROP TABLE employee;
8-
DROP TABLE company;
3+
-- (re)create tables first
94

5+
DROP TABLE ProjectEmployee;
6+
DROP TABLE Project;
7+
DROP TABLE Employee;
8+
DROP TABLE Department;
9+
DROP TABLE Company;
1010

11-
CREATE TABLE company (
11+
CREATE TABLE Company (
1212
pid SERIAL PRIMARY KEY,
1313
name VARCHAR(40) NOT NULL,
1414
address VARCHAR(128)
1515
);
1616

17-
CREATE TABLE employee (
18-
pid SERIAL PRIMARY KEY,
19-
company_pid INTEGER NOT NULL REFERENCES company (pid) ON DELETE CASCADE,
20-
name varchar(50) NOT NULL,
21-
surname varchar(50) NOT NULL,
22-
mail varchar(50),
23-
salary numeric(10,2) CHECK (salary > 0)
24-
);
25-
26-
CREATE TABLE department (
17+
CREATE TABLE Department (
2718
pid SERIAL PRIMARY KEY,
19+
company_pid INTEGER NOT NULL REFERENCES Company (pid) ON DELETE CASCADE,
2820
name varchar(50) NOT NULL
2921
);
3022

31-
CREATE TABLE employee_department (
32-
employee_pid INTEGER NOT NULL REFERENCES employee (pid) ON DELETE CASCADE,
33-
department_pid INTEGER NOT NULL REFERENCES department (pid) ON DELETE CASCADE,
34-
UNIQUE (employee_pid, department_pid)
35-
);
36-
37-
CREATE TABLE product_category (
23+
CREATE TABLE Employee (
3824
pid SERIAL PRIMARY KEY,
39-
category varchar(50) NOT NULL
25+
department_pid INTEGER NOT NULL REFERENCES Department (pid) ON DELETE CASCADE,
26+
name varchar(50) NOT NULL,
27+
surname varchar(50) NOT NULL,
28+
email varchar(50),
29+
salary numeric(10,2) CHECK (salary > 0)
4030
);
4131

42-
CREATE TABLE product (
32+
CREATE TABLE Project (
4333
pid SERIAL PRIMARY KEY,
44-
product_category_pid INTEGER NOT NULL REFERENCES product_category (pid) ON DELETE CASCADE,
4534
name varchar(50) NOT NULL,
46-
description varchar(50) NOT NULL,
47-
row_status char(1) DEFAULT 'A',
48-
price numeric(10,2) CHECK (price > 0)
35+
dateStarted date
4936
);
5037

51-
INSERT INTO product_category (pid, category) values (1, 'Hardware');
52-
INSERT INTO product_category (pid, category) values (2, 'Software');
53-
INSERT INTO product_category (pid, category) values (3, 'Middleware');
54-
55-
INSERT INTO public.company (name, address) VALUES('CleverGang', 'Chotikov 14, Chotikov 330 17');
56-
INSERT INTO public.company (name, address) VALUES('Querity', 'Manesova 44, Plzen');
57-
58-
59-
INSERT INTO public.employee (company_pid, name, surname, mail, salary)
60-
VALUES(1, 'Břetislav', 'Wajtr', 'bretislav.wajtr@clevergang.com', 35000);
61-
INSERT INTO public.employee (company_pid, name, surname, mail, salary)
62-
VALUES(1, 'Viktor', 'Janukovic', 'viktor.janukovic@clevergang.com', 10000);
63-
INSERT INTO public.employee (company_pid, name, surname, mail, salary)
64-
VALUES(1, 'Olga', 'Pravcová', 'olga.wajtrova@clevergang.com', 35000);
65-
INSERT INTO public.employee (company_pid, name, surname, mail, salary)
66-
VALUES(2, 'Jiri', 'Kiml', 'jiri.kiml@querity.cz', 49000);
67-
68-
69-
INSERT INTO public.department (name) VALUES('Development');
70-
INSERT INTO public.department (name) VALUES('Sales');
71-
72-
73-
INSERT INTO public.employee_department (employee_pid, department_pid) VALUES(1, 1);
74-
INSERT INTO public.employee_department (employee_pid, department_pid) VALUES(2, 1);
75-
INSERT INTO public.employee_department (employee_pid, department_pid) VALUES(3, 1);
76-
INSERT INTO public.employee_department (employee_pid, department_pid) VALUES(3, 2);
38+
CREATE TABLE ProjectEmployee (
39+
project_pid INTEGER REFERENCES Project,
40+
employee_pid INTEGER REFERENCES Employee,
41+
PRIMARY KEY (project_pid, employee_pid)
42+
);
7743

44+
-- now insertProject some test data
45+
46+
INSERT INTO Company (name, address) VALUES ('CleverGang', 'Prague, Czech Republic');
47+
INSERT INTO Company (name, address) VALUES ('Supersoft', 'Berlin, Germany');
48+
INSERT INTO Company (name, address) VALUES ('Pear', 'Cupertino, USA');
49+
50+
INSERT INTO Department (company_pid, name) VALUES (1, 'Back office');
51+
INSERT INTO Department (company_pid, name) VALUES (1, 'IT Department');
52+
INSERT INTO Department (company_pid, name) VALUES (1, 'Software Development');
53+
INSERT INTO Department (company_pid, name) VALUES (2, 'Help desk');
54+
INSERT INTO Department (company_pid, name) VALUES (2, 'Sales');
55+
INSERT INTO Department (company_pid, name) VALUES (3, 'Hardware Development');
56+
57+
-- names generated using listofrandomnames.com ;)
58+
INSERT INTO Employee (department_pid, name, surname, email, salary)
59+
VALUES (1, 'Curt', 'Odegaard', 'curt.odegaard@clevergang.com', 10000);
60+
INSERT INTO Employee (department_pid, name, surname, email, salary)
61+
VALUES (2, 'Rupert', 'Spradling', 'rupert.spradling@clevergang.com', 11000);
62+
INSERT INTO Employee (department_pid, name, surname, email, salary)
63+
VALUES (3, 'Carita', 'Ladouceur', 'carita.ladouceur@clevergang.com', 12000);
64+
INSERT INTO Employee (department_pid, name, surname, email, salary)
65+
VALUES (4, 'Abbie', 'Waring', 'abbie.waring@supersoft.com', 13000);
66+
INSERT INTO Employee (department_pid, name, surname, email, salary)
67+
VALUES (5, 'Cecily', 'Devaughn', 'cecily.devaughn@supersoft.com', 15000);
68+
INSERT INTO Employee (department_pid, name, surname, email, salary)
69+
VALUES (6, 'Yulanda', 'Grado', 'yulanda.grado@pear.com', 17000);
70+
INSERT INTO Employee (department_pid, name, surname, email, salary)
71+
VALUES (1, 'Chia', 'Kuder', 'chia.kuder@clevergang.com', 20000);
72+
INSERT INTO Employee (department_pid, name, surname, email, salary)
73+
VALUES (2, 'Alica', 'Iannotti', 'alica.iannotti@clevergang.com', 30000);
74+
INSERT INTO Employee (department_pid, name, surname, email, salary)
75+
VALUES (3, 'Estrella', 'Heroux', 'estrella.heroux@clevergang.com', 31000);
76+
INSERT INTO Employee (department_pid, name, surname, email, salary)
77+
VALUES (4, 'Myrta', 'Lirette', 'myrta.lirette@supersoft.com', 50000);
78+
79+
80+
INSERT INTO Project (name, dateStarted) VALUES ('Awesome app', CURRENT_DATE);
81+
INSERT INTO Project (name, dateStarted) VALUES ('Desktop app', CURRENT_DATE);
82+
83+
84+
INSERT INTO ProjectEmployee (project_pid, employee_pid) VALUES (1, 1);
85+
INSERT INTO ProjectEmployee (project_pid, employee_pid) VALUES (1, 2);
86+
INSERT INTO ProjectEmployee (project_pid, employee_pid) VALUES (1, 9);
87+
INSERT INTO ProjectEmployee (project_pid, employee_pid) VALUES (2, 10);
88+
INSERT INTO ProjectEmployee (project_pid, employee_pid) VALUES (2, 5);
Lines changed: 79 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,94 @@
11
package com.clevergang.dbtests;
22

3-
import org.springframework.stereotype.Component;
3+
import java.time.LocalDate;
4+
import java.util.ArrayList;
5+
import java.util.List;
6+
7+
import com.clevergang.dbtests.model.Project;
8+
import com.clevergang.dbtests.service.repository.DataRepository;
9+
import org.apache.commons.lang3.RandomStringUtils;
10+
import org.slf4j.Logger;
11+
import org.slf4j.LoggerFactory;
12+
import org.springframework.util.StopWatch;
413

514
/**
615
* Common scenarios
716
*
817
* @author Bretislav Wajtr <bretislav.wajtr@clevergang.com>
918
*/
10-
@Component
1119
public class Scenarios {
20+
private static final Logger logger = LoggerFactory.getLogger(Scenarios.class);
21+
22+
private DataRepository repository;
23+
24+
public Scenarios(DataRepository repository) {
25+
this.repository = repository;
26+
}
27+
28+
/**
29+
* Load single entity based on primary key
30+
*/
31+
public void scenarioOne() {
32+
Integer pid = 1;
33+
repository.findCompany(pid);
34+
}
35+
36+
/**
37+
* Load list of entities based on condition
38+
*/
39+
public void scenarioTwo() {
40+
Integer minSalary = 2000;
41+
repository.employeesWithSalaryGreaterThan(minSalary);
42+
}
43+
44+
/**
45+
* Save new single entity and return primary key
46+
*/
47+
public void scenarioThree() {
48+
Project project = new Project();
49+
project.setName("TestProject");
50+
project.setDate(LocalDate.now());
51+
52+
repository.insertProject(project);
53+
}
1254

55+
/**
56+
* Batch insertProject multiple entities of same type and return generated keys
57+
*/
58+
public void scenarioFour() {
59+
// create a list of thousand products
60+
List<Project> projects = new ArrayList<>();
61+
StopWatch watch = new StopWatch();
62+
watch.start();
63+
for (int i = 0; i < 1000; i++) {
64+
Project project = new Project();
65+
project.setName(RandomStringUtils.randomAlphabetic(10));
66+
project.setDate(LocalDate.now());
67+
projects.add(project);
68+
}
69+
repository.insertAllProjects(projects);
70+
watch.stop();
71+
logger.info("Total time {} ms", watch.getTotalTimeMillis());
72+
}
1373

74+
/**
75+
* Update single existing entity - update one field of the entity
76+
*/
77+
public void scenarioFive() {
78+
}
1479

80+
public void scenarioSix() {
81+
82+
}
83+
84+
public void scenarioSeven() {
85+
86+
}
87+
88+
public void scenarioEight() {
89+
90+
}
1591
}
1692

1793

94+

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

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

1011
public Integer getPid() {
@@ -23,6 +24,14 @@ public void setName(String name) {
2324
this.name = name;
2425
}
2526

27+
public Integer getDepartmentPid() {
28+
return department_pid;
29+
}
30+
31+
public void setDepartmentPid(Integer department_pid) {
32+
this.department_pid = department_pid;
33+
}
34+
2635
@Override
2736
public boolean equals(Object o) {
2837
if (this == o)
@@ -34,13 +43,16 @@ public boolean equals(Object o) {
3443

3544
if (pid != null ? !pid.equals(that.pid) : that.pid != null)
3645
return false;
46+
if (department_pid != null ? !department_pid.equals(that.department_pid) : that.department_pid != null)
47+
return false;
3748
return name != null ? name.equals(that.name) : that.name == null;
3849

3950
}
4051

4152
@Override
4253
public int hashCode() {
4354
int result = pid != null ? pid.hashCode() : 0;
55+
result = 31 * result + (department_pid != null ? department_pid.hashCode() : 0);
4456
result = 31 * result + (name != null ? name.hashCode() : 0);
4557
return result;
4658
}
@@ -49,7 +61,9 @@ public int hashCode() {
4961
public String toString() {
5062
return "Department{" +
5163
"pid=" + pid +
64+
", department_pid=" + department_pid +
5265
", name='" + name + '\'' +
5366
'}';
5467
}
5568
}
69+

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

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@
88
public class Employee {
99

1010
private Integer pid;
11-
private Integer company_pid;
11+
private Integer department_pid;
1212
private String name;
1313
private String surname;
14-
private String mail;
14+
private String email;
1515
private BigDecimal salary;
1616

1717
public Integer getPid() {
@@ -22,12 +22,12 @@ public void setPid(Integer pid) {
2222
this.pid = pid;
2323
}
2424

25-
public Integer getCompany_pid() {
26-
return company_pid;
25+
public Integer getDepartmentPid() {
26+
return department_pid;
2727
}
2828

29-
public void setCompany_pid(Integer company_pid) {
30-
this.company_pid = company_pid;
29+
public void setDepartmentPid(Integer department_pid) {
30+
this.department_pid = department_pid;
3131
}
3232

3333
public String getName() {
@@ -46,12 +46,12 @@ public void setSurname(String surname) {
4646
this.surname = surname;
4747
}
4848

49-
public String getMail() {
50-
return mail;
49+
public String getEmail() {
50+
return email;
5151
}
5252

53-
public void setMail(String mail) {
54-
this.mail = mail;
53+
public void setEmail(String email) {
54+
this.email = email;
5555
}
5656

5757
public BigDecimal getSalary() {
@@ -73,13 +73,13 @@ public boolean equals(Object o) {
7373

7474
if (pid != null ? !pid.equals(employee.pid) : employee.pid != null)
7575
return false;
76-
if (company_pid != null ? !company_pid.equals(employee.company_pid) : employee.company_pid != null)
76+
if (department_pid != null ? !department_pid.equals(employee.department_pid) : employee.department_pid != null)
7777
return false;
7878
if (name != null ? !name.equals(employee.name) : employee.name != null)
7979
return false;
8080
if (surname != null ? !surname.equals(employee.surname) : employee.surname != null)
8181
return false;
82-
if (mail != null ? !mail.equals(employee.mail) : employee.mail != null)
82+
if (email != null ? !email.equals(employee.email) : employee.email != null)
8383
return false;
8484
return salary != null ? salary.equals(employee.salary) : employee.salary == null;
8585

@@ -88,10 +88,10 @@ public boolean equals(Object o) {
8888
@Override
8989
public int hashCode() {
9090
int result = pid != null ? pid.hashCode() : 0;
91-
result = 31 * result + (company_pid != null ? company_pid.hashCode() : 0);
91+
result = 31 * result + (department_pid != null ? department_pid.hashCode() : 0);
9292
result = 31 * result + (name != null ? name.hashCode() : 0);
9393
result = 31 * result + (surname != null ? surname.hashCode() : 0);
94-
result = 31 * result + (mail != null ? mail.hashCode() : 0);
94+
result = 31 * result + (email != null ? email.hashCode() : 0);
9595
result = 31 * result + (salary != null ? salary.hashCode() : 0);
9696
return result;
9797
}
@@ -100,10 +100,10 @@ public int hashCode() {
100100
public String toString() {
101101
return "Employee{" +
102102
"pid=" + pid +
103-
", company_pid=" + company_pid +
103+
", department_pid=" + department_pid +
104104
", name='" + name + '\'' +
105105
", surname='" + surname + '\'' +
106-
", mail='" + mail + '\'' +
106+
", email='" + email + '\'' +
107107
", salary=" + salary +
108108
'}';
109109
}

0 commit comments

Comments
 (0)