Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
S
std_hibernate
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Patryk Czarnik
std_hibernate
Commits
d053c221
Commit
d053c221
authored
Oct 31, 2024
by
Patryk Czarnik
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Komentarze nt LAZY/EAGER
parent
dd484e46
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
22 additions
and
4 deletions
+22
-4
Department.java
src/main/java/hr/model/Department.java
+9
-0
Employee.java
src/main/java/hr/model/Employee.java
+13
-4
No files found.
src/main/java/hr/model/Department.java
View file @
d053c221
...
...
@@ -25,6 +25,15 @@ public class Department extends AbstractEntity {
@JoinColumn
(
name
=
"location_id"
)
private
hr
.
model
.
Location
location
;
/* Kolekcje (List i Set) prowadzące do innych obiektów encji
są domyślnie wiązane w trybie LAZY.
Oznacza to, że lista nie jest od razu uzupełniane elementami,
a następuje to dopiero przy pierwszym dostępie do jej zawartości.
W trybie EAGER lista byłaby wypeniana danymi od razu,
co byłoby potencjalnie dużym problemem wydajnościowym
(zwn na dalsze powiązania tamtych obiektów itd...)
*/
@OneToMany
(
mappedBy
=
"department"
)
private
Set
<
Employee
>
employees
=
new
LinkedHashSet
<>();
...
...
src/main/java/hr/model/Employee.java
View file @
d053c221
...
...
@@ -29,10 +29,6 @@ public class Employee extends AbstractEntity {
@Column
(
name
=
"hire_date"
,
nullable
=
false
)
private
LocalDate
hireDate
;
@ManyToOne
(
optional
=
false
)
@JoinColumn
(
name
=
"job_id"
,
nullable
=
false
)
private
hr
.
model
.
Job
job
;
@Column
(
name
=
"salary"
,
precision
=
8
,
scale
=
2
)
private
BigDecimal
salary
;
...
...
@@ -42,10 +38,23 @@ public class Employee extends AbstractEntity {
@Column
(
name
=
"manager_id"
)
private
Integer
manager
;
/* Zwykłe pola domyślnie są podłączane jako "EAGER",
czyli są wczytywane wraz z obiektem głównym.
Jeśli jednak oznaczymy zależność jako LAZY, to obiekt Department zostanie załądowany dopiero
przy pierwszym dostępnei do jego pól.
Technicznie jest realizowane za pomocą "klasy proxy", którą dynamicznie tworzy Hibernate.
Aby to działało, klasa Department nie moze być final.
*/
@ManyToOne
(
fetch
=
FetchType
.
LAZY
)
@JoinColumn
(
name
=
"department_id"
)
private
Department
department
;
/* Job jest wczytywany w trybie EAGER, czyli od razu wraz z obiektem Employee.
*/
@ManyToOne
(
optional
=
false
)
@JoinColumn
(
name
=
"job_id"
,
nullable
=
false
)
private
Job
job
;
public
Integer
getId
()
{
return
id
;
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment