You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Commit 467f647
Browse filesBrowse the repository at this point in the historyBrowse files
> Repositories are classes or components that encapsulate the logic required to access data sources. They centralize common data access functionality, providing better maintainability and decoupling the infrastructure or technology used to access databases from the domain model layer.
28
+
29
+
**Programmatic Example**
30
+
31
+
Let's first look at the person class that we need to persist.
32
+
33
+
```java
34
+
@Entity
35
+
publicclassPerson {
36
+
37
+
@Id
38
+
@GeneratedValue
39
+
privateLong id;
40
+
privateString name;
41
+
privateString surname;
42
+
43
+
privateint age;
44
+
45
+
publicPerson() {
46
+
}
47
+
48
+
/**
49
+
* Constructor.
50
+
*/
51
+
publicPerson(Stringname, Stringsurname, intage) {
52
+
this.name = name;
53
+
this.surname = surname;
54
+
this.age = age;
55
+
}
56
+
57
+
publicLonggetId() {
58
+
return id;
59
+
}
60
+
61
+
publicvoidsetId(Longid) {
62
+
this.id = id;
63
+
}
64
+
65
+
publicStringgetName() {
66
+
return name;
67
+
}
68
+
69
+
publicvoidsetName(Stringname) {
70
+
this.name = name;
71
+
}
72
+
73
+
publicStringgetSurname() {
74
+
return surname;
75
+
}
76
+
77
+
publicvoidsetSurname(Stringsurname) {
78
+
this.surname = surname;
79
+
}
80
+
81
+
publicintgetAge() {
82
+
return age;
83
+
}
84
+
85
+
publicvoidsetAge(intage) {
86
+
this.age = age;
87
+
}
88
+
89
+
@Override
90
+
publicStringtoString() {
91
+
return"Person [id="+ id +", name="+ name +", surname="+ surname +", age="+ age +"]";
92
+
}
93
+
94
+
@Override
95
+
publicinthashCode() {
96
+
finalvar prime =31;
97
+
var result =1;
98
+
result = prime * result + age;
99
+
result = prime * result + (id ==null?0: id.hashCode());
100
+
result = prime * result + (name ==null?0: name.hashCode());
101
+
result = prime * result + (surname ==null?0: surname.hashCode());
102
+
return result;
103
+
}
104
+
105
+
@Override
106
+
publicbooleanequals(Objectobj) {
107
+
if (this== obj) {
108
+
returntrue;
109
+
}
110
+
if (obj ==null) {
111
+
returnfalse;
112
+
}
113
+
if (getClass() != obj.getClass()) {
114
+
returnfalse;
115
+
}
116
+
var other = (Person) obj;
117
+
if (age != other.age) {
118
+
returnfalse;
119
+
}
120
+
if (id ==null) {
121
+
if (other.id !=null) {
122
+
returnfalse;
123
+
}
124
+
} elseif (!id.equals(other.id)) {
125
+
returnfalse;
126
+
}
127
+
if (name ==null) {
128
+
if (other.name !=null) {
129
+
returnfalse;
130
+
}
131
+
} elseif (!name.equals(other.name)) {
132
+
returnfalse;
133
+
}
134
+
if (surname ==null) {
135
+
return other.surname ==null;
136
+
}
137
+
return surname.equals(other.surname);
138
+
}
139
+
}
140
+
```
141
+
142
+
We are using Spring Data to create the repository so it becomes really simple.
// Person [id=1, name=Peter, surname=Sagan, age=17]
232
+
// Person [id=2, name=Nasta, surname=Kuzminova, age=25]
233
+
// Person [id=3, name=John, surname=lawrence, age=35]
234
+
// Person [id=4, name=Terry, surname=Law, age=36]
235
+
// Find by id 2: Person [id=2, name=Barbora, surname=Spotakova, age=25]
236
+
// Count Person records: 3
237
+
// Find by John is Person [id=3, name=John, surname=lawrence, age=35]
238
+
// Find Person with age between 20,40:
239
+
// Person [id=3, name=John, surname=lawrence, age=35]
240
+
// Person [id=4, name=Terry, surname=Law, age=36]
241
+
```
17
242
18
243
## Class diagram
19
244

@@ -36,3 +261,4 @@ Use the Repository pattern when
36
261
*[Advanced Spring Data JPA - Specifications and Querydsl](https://spring.io/blog/2011/04/26/advanced-spring-data-jpa-specifications-and-querydsl/)
37
262
*[Repository Pattern Benefits and Spring Implementation](https://stackoverflow.com/questions/40068965/repository-pattern-benefits-and-spring-implementation)
38
263
*[Patterns of Enterprise Application Architecture](https://www.amazon.com/gp/product/0321127420/ref=as_li_tl?ie=UTF8&camp=1789&creative=9325&creativeASIN=0321127420&linkCode=as2&tag=javadesignpat-20&linkId=d9f7d37b032ca6e96253562d075fcc4a)
264
+
*[Design patterns that I often avoid: Repository pattern](https://www.infoworld.com/article/3117713/design-patterns-that-i-often-avoid-repository-pattern.html)
0 commit comments