Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
J
java_weekendowa_20221008
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
java_weekendowa_20221008
Commits
2cada9f6
Commit
2cada9f6
authored
Nov 13, 2022
by
Patryk Czarnik
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
HibernateGotowy
parent
afb73aab
Show whitespace changes
Inline
Side-by-side
Showing
44 changed files
with
2223 additions
and
0 deletions
+2223
-0
.gitignore
PC28-HibernateGotowy/.gitignore
+7
-0
pom.xml
PC28-HibernateGotowy/pom.xml
+28
-0
.gitkeep
PC28-HibernateGotowy/src/main/java/.gitkeep
+0
-0
AdressInfo.java
PC28-HibernateGotowy/src/main/java/dto/AdressInfo.java
+129
-0
MiniEmp.java
PC28-HibernateGotowy/src/main/java/dto/MiniEmp.java
+85
-0
PracownikWDepartamencie.java
...nateGotowy/src/main/java/dto/PracownikWDepartamencie.java
+144
-0
Country.java
PC28-HibernateGotowy/src/main/java/model/Country.java
+57
-0
Department.java
PC28-HibernateGotowy/src/main/java/model/Department.java
+97
-0
Employee.java
PC28-HibernateGotowy/src/main/java/model/Employee.java
+152
-0
Job.java
PC28-HibernateGotowy/src/main/java/model/Job.java
+94
-0
Location.java
PC28-HibernateGotowy/src/main/java/model/Location.java
+91
-0
Region.java
PC28-HibernateGotowy/src/main/java/model/Region.java
+71
-0
ModyfikujRekord.java
...Gotowy/src/main/java/nasze_przyklady/ModyfikujRekord.java
+47
-0
OdczytajJedenRekord.java
...wy/src/main/java/nasze_przyklady/OdczytajJedenRekord.java
+36
-0
OdczytajWgMiasta.java
...otowy/src/main/java/nasze_przyklady/OdczytajWgMiasta.java
+49
-0
OdczytajWszystkich1.java
...wy/src/main/java/nasze_przyklady/OdczytajWszystkich1.java
+29
-0
OdczytajWszystkich2.java
...wy/src/main/java/nasze_przyklady/OdczytajWszystkich2.java
+29
-0
Odczytaj_wg_Nazwisk.java
...wy/src/main/java/nasze_przyklady/Odczytaj_wg_Nazwisk.java
+35
-0
RekordyZalezne.java
...eGotowy/src/main/java/nasze_przyklady/RekordyZalezne.java
+43
-0
AktualizujRekord1.java
...nateGotowy/src/main/java/przyklady/AktualizujRekord1.java
+50
-0
AktualizujRekord2.java
...nateGotowy/src/main/java/przyklady/AktualizujRekord2.java
+61
-0
Criteria1.java
PC28-HibernateGotowy/src/main/java/przyklady/Criteria1.java
+39
-0
Criteria2.java
PC28-HibernateGotowy/src/main/java/przyklady/Criteria2.java
+53
-0
Criteria3.java
PC28-HibernateGotowy/src/main/java/przyklady/Criteria3.java
+48
-0
DodajNoweRekordy.java
...rnateGotowy/src/main/java/przyklady/DodajNoweRekordy.java
+53
-0
Odczyt01_JedenRekord.java
...eGotowy/src/main/java/przyklady/Odczyt01_JedenRekord.java
+30
-0
Odczyt02_JedenRekord_InneTabele.java
.../main/java/przyklady/Odczyt02_JedenRekord_InneTabele.java
+53
-0
Odczyt03_WszystkieRekordy_FindAll.java
...ain/java/przyklady/Odczyt03_WszystkieRekordy_FindAll.java
+36
-0
Odczyt04_WszystkieRekordy_Stream.java
...main/java/przyklady/Odczyt04_WszystkieRekordy_Stream.java
+36
-0
Odczyt05_WszystkieRekordy_JPQL.java
...c/main/java/przyklady/Odczyt05_WszystkieRekordy_JPQL.java
+36
-0
Odczyt06_Warunek.java
...rnateGotowy/src/main/java/przyklady/Odczyt06_Warunek.java
+52
-0
Odczyt07_Skalar.java
...ernateGotowy/src/main/java/przyklady/Odczyt07_Skalar.java
+32
-0
Odczyt08_Tablica.java
...rnateGotowy/src/main/java/przyklady/Odczyt08_Tablica.java
+31
-0
Odczyt09_DTO.java
...HibernateGotowy/src/main/java/przyklady/Odczyt09_DTO.java
+34
-0
Odczyt10_Group.java
...bernateGotowy/src/main/java/przyklady/Odczyt10_Group.java
+29
-0
Odczyt11_Join.java
...ibernateGotowy/src/main/java/przyklady/Odczyt11_Join.java
+32
-0
Odczyt12_WieleTabel.java
...teGotowy/src/main/java/przyklady/Odczyt12_WieleTabel.java
+39
-0
Odczyt13_Native.java
...ernateGotowy/src/main/java/przyklady/Odczyt13_Native.java
+44
-0
Odczyt14_Native.java
...ernateGotowy/src/main/java/przyklady/Odczyt14_Native.java
+54
-0
Odczyt15_Native_DTO.java
...teGotowy/src/main/java/przyklady/Odczyt15_Native_DTO.java
+57
-0
Odczyt16_Native_Entity.java
...otowy/src/main/java/przyklady/Odczyt16_Native_Entity.java
+35
-0
OdczytajManagerow.java
...nateGotowy/src/main/java/przyklady/OdczytajManagerow.java
+47
-0
.gitkeep
PC28-HibernateGotowy/src/main/resources/.gitkeep
+0
-0
persistence.xml
...bernateGotowy/src/main/resources/META-INF/persistence.xml
+19
-0
No files found.
PC28-HibernateGotowy/.gitignore
0 → 100644
View file @
2cada9f6
/target/
/.classpath
/.project
/.settings/
/*.iml
/.idea/
PC28-HibernateGotowy/pom.xml
0 → 100644
View file @
2cada9f6
<project
xmlns=
"http://maven.apache.org/POM/4.0.0"
xmlns:xsi=
"http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation=
"http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"
>
<modelVersion>
4.0.0
</modelVersion>
<groupId>
pl.alx.kjava
</groupId>
<artifactId>
PC28-HibernateGotowy
</artifactId>
<version>
1.0
</version>
<properties>
<project.build.sourceEncoding>
UTF-8
</project.build.sourceEncoding>
<maven.compiler.source>
17
</maven.compiler.source>
<maven.compiler.target>
17
</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>
org.hibernate
</groupId>
<artifactId>
hibernate-core
</artifactId>
<version>
5.6.12.Final
</version>
</dependency>
<dependency>
<groupId>
org.postgresql
</groupId>
<artifactId>
postgresql
</artifactId>
<version>
42.5.0
</version>
<scope>
runtime
</scope>
</dependency>
</dependencies>
</project>
PC28-HibernateGotowy/src/main/java/.gitkeep
0 → 100644
View file @
2cada9f6
PC28-HibernateGotowy/src/main/java/dto/AdressInfo.java
0 → 100644
View file @
2cada9f6
package
dto
;
import
java.math.BigDecimal
;
import
java.time.LocalDate
;
import
java.util.Objects
;
public
class
AdressInfo
{
private
int
employeeId
;
private
String
firstName
,
lastName
;
private
String
jobTitle
;
private
BigDecimal
salary
;
private
LocalDate
hireDate
;
private
String
departmentName
;
private
String
streetAddress
;
private
String
postalCode
;
private
String
city
;
private
String
country
;
public
AdressInfo
()
{
}
public
AdressInfo
(
int
employeeId
,
String
firstName
,
String
lastName
,
String
jobTitle
,
BigDecimal
salary
,
LocalDate
hireDate
,
String
departmentName
,
String
streetAddress
,
String
postalCode
,
String
city
,
String
country
)
{
this
.
employeeId
=
employeeId
;
this
.
firstName
=
firstName
;
this
.
lastName
=
lastName
;
this
.
jobTitle
=
jobTitle
;
this
.
salary
=
salary
;
this
.
hireDate
=
hireDate
;
this
.
departmentName
=
departmentName
;
this
.
streetAddress
=
streetAddress
;
this
.
postalCode
=
postalCode
;
this
.
city
=
city
;
this
.
country
=
country
;
}
public
int
getEmployeeId
()
{
return
employeeId
;
}
public
void
setEmployeeId
(
int
employeeId
)
{
this
.
employeeId
=
employeeId
;
}
public
String
getFirstName
()
{
return
firstName
;
}
public
void
setFirstName
(
String
firstName
)
{
this
.
firstName
=
firstName
;
}
public
String
getLastName
()
{
return
lastName
;
}
public
void
setLastName
(
String
lastName
)
{
this
.
lastName
=
lastName
;
}
public
String
getJobTitle
()
{
return
jobTitle
;
}
public
void
setJobTitle
(
String
jobTitle
)
{
this
.
jobTitle
=
jobTitle
;
}
public
BigDecimal
getSalary
()
{
return
salary
;
}
public
void
setSalary
(
BigDecimal
salary
)
{
this
.
salary
=
salary
;
}
public
LocalDate
getHireDate
()
{
return
hireDate
;
}
public
void
setHireDate
(
LocalDate
hireDate
)
{
this
.
hireDate
=
hireDate
;
}
public
String
getDepartmentName
()
{
return
departmentName
;
}
public
void
setDepartmentName
(
String
departmentName
)
{
this
.
departmentName
=
departmentName
;
}
public
String
getStreetAddress
()
{
return
streetAddress
;
}
public
void
setStreetAddress
(
String
streetAddress
)
{
this
.
streetAddress
=
streetAddress
;
}
public
String
getPostalCode
()
{
return
postalCode
;
}
public
void
setPostalCode
(
String
postalCode
)
{
this
.
postalCode
=
postalCode
;
}
public
String
getCity
()
{
return
city
;
}
public
void
setCity
(
String
city
)
{
this
.
city
=
city
;
}
public
String
getCountry
()
{
return
country
;
}
public
void
setCountry
(
String
country
)
{
this
.
country
=
country
;
}
@Override
public
String
toString
()
{
return
"AdressInfo [employeeId="
+
employeeId
+
", firstName="
+
firstName
+
", lastName="
+
lastName
+
", jobTitle="
+
jobTitle
+
", salary="
+
salary
+
", hireDate="
+
hireDate
+
", departmentName="
+
departmentName
+
", streetAddress="
+
streetAddress
+
", postalCode="
+
postalCode
+
", city="
+
city
+
", country="
+
country
+
"]"
;
}
@Override
public
int
hashCode
()
{
return
Objects
.
hash
(
city
,
country
,
departmentName
,
employeeId
,
firstName
,
hireDate
,
jobTitle
,
lastName
,
postalCode
,
salary
,
streetAddress
);
}
@Override
public
boolean
equals
(
Object
obj
)
{
if
(
this
==
obj
)
return
true
;
if
(
obj
==
null
)
return
false
;
if
(
getClass
()
!=
obj
.
getClass
())
return
false
;
AdressInfo
other
=
(
AdressInfo
)
obj
;
return
Objects
.
equals
(
city
,
other
.
city
)
&&
Objects
.
equals
(
country
,
other
.
country
)
&&
Objects
.
equals
(
departmentName
,
other
.
departmentName
)
&&
employeeId
==
other
.
employeeId
&&
Objects
.
equals
(
firstName
,
other
.
firstName
)
&&
Objects
.
equals
(
hireDate
,
other
.
hireDate
)
&&
Objects
.
equals
(
jobTitle
,
other
.
jobTitle
)
&&
Objects
.
equals
(
lastName
,
other
.
lastName
)
&&
Objects
.
equals
(
postalCode
,
other
.
postalCode
)
&&
Objects
.
equals
(
salary
,
other
.
salary
)
&&
Objects
.
equals
(
streetAddress
,
other
.
streetAddress
);
}
}
PC28-HibernateGotowy/src/main/java/dto/MiniEmp.java
0 → 100644
View file @
2cada9f6
package
dto
;
import
java.math.BigDecimal
;
import
java.util.Objects
;
public
class
MiniEmp
{
private
String
firstName
,
lastName
;
private
String
jobTitle
;
private
BigDecimal
salary
;
public
MiniEmp
()
{
}
public
MiniEmp
(
String
firstName
,
String
lastName
,
String
jobTitle
,
BigDecimal
salary
)
{
this
.
firstName
=
firstName
;
this
.
lastName
=
lastName
;
this
.
jobTitle
=
jobTitle
;
this
.
salary
=
salary
;
System
.
out
.
println
(
"salary YES"
);
}
public
MiniEmp
(
String
firstName
,
String
lastName
,
String
jobTitle
)
{
this
.
firstName
=
firstName
;
this
.
lastName
=
lastName
;
this
.
jobTitle
=
jobTitle
;
System
.
out
.
println
(
"salary NO"
);
}
public
String
getFirstName
()
{
return
firstName
;
}
public
void
setFirstName
(
String
firstName
)
{
this
.
firstName
=
firstName
;
}
public
String
getLastName
()
{
return
lastName
;
}
public
void
setLastName
(
String
lastName
)
{
this
.
lastName
=
lastName
;
}
public
String
getJobTitle
()
{
return
jobTitle
;
}
public
void
setJobTitle
(
String
jobTitle
)
{
this
.
jobTitle
=
jobTitle
;
}
public
BigDecimal
getSalary
()
{
return
salary
;
}
public
void
setSalary
(
BigDecimal
salary
)
{
this
.
salary
=
salary
;
}
@Override
public
String
toString
()
{
return
"MiniEmp [firstName="
+
firstName
+
", lastName="
+
lastName
+
", jobTitle="
+
jobTitle
+
", salary="
+
salary
+
"]"
;
}
@Override
public
int
hashCode
()
{
return
Objects
.
hash
(
firstName
,
jobTitle
,
lastName
,
salary
);
}
@Override
public
boolean
equals
(
Object
obj
)
{
if
(
this
==
obj
)
return
true
;
if
(
obj
==
null
)
return
false
;
if
(
getClass
()
!=
obj
.
getClass
())
return
false
;
MiniEmp
other
=
(
MiniEmp
)
obj
;
return
Objects
.
equals
(
firstName
,
other
.
firstName
)
&&
Objects
.
equals
(
jobTitle
,
other
.
jobTitle
)
&&
Objects
.
equals
(
lastName
,
other
.
lastName
)
&&
Objects
.
equals
(
salary
,
other
.
salary
);
}
}
PC28-HibernateGotowy/src/main/java/dto/PracownikWDepartamencie.java
0 → 100644
View file @
2cada9f6
package
dto
;
import
java.math.BigDecimal
;
import
java.util.Locale
;
import
java.util.Objects
;
import
javax.persistence.Entity
;
import
javax.persistence.Id
;
@Entity
public
class
PracownikWDepartamencie
{
@Id
private
int
id
;
private
String
departmentName
,
firstName
,
lastName
,
jobTitle
;
private
BigDecimal
salary
,
depAvg
;
private
int
depPos
,
globalPos
;
public
PracownikWDepartamencie
()
{
}
public
PracownikWDepartamencie
(
int
id
,
String
departmentName
,
String
firstName
,
String
lastName
,
String
jobTitle
,
BigDecimal
salary
,
BigDecimal
depAvg
,
int
depPos
,
int
globalPos
)
{
this
.
id
=
id
;
this
.
departmentName
=
departmentName
;
this
.
firstName
=
firstName
;
this
.
lastName
=
lastName
;
this
.
jobTitle
=
jobTitle
;
this
.
salary
=
salary
;
this
.
depAvg
=
depAvg
;
this
.
depPos
=
depPos
;
this
.
globalPos
=
globalPos
;
}
public
int
getId
()
{
return
id
;
}
public
void
setId
(
int
id
)
{
this
.
id
=
id
;
}
public
String
getDepartmentName
()
{
return
departmentName
;
}
public
void
setDepartmentName
(
String
departmentName
)
{
this
.
departmentName
=
departmentName
;
}
public
String
getFirstName
()
{
return
firstName
;
}
public
void
setFirstName
(
String
firstName
)
{
this
.
firstName
=
firstName
;
}
public
String
getLastName
()
{
return
lastName
;
}
public
void
setLastName
(
String
lastName
)
{
this
.
lastName
=
lastName
;
}
public
String
getJobTitle
()
{
return
jobTitle
;
}
public
void
setJobTitle
(
String
jobTitle
)
{
this
.
jobTitle
=
jobTitle
;
}
public
BigDecimal
getSalary
()
{
return
salary
;
}
public
void
setSalary
(
BigDecimal
salary
)
{
this
.
salary
=
salary
;
}
public
BigDecimal
getDepAvg
()
{
return
depAvg
;
}
public
void
setDepAvg
(
BigDecimal
depAvg
)
{
this
.
depAvg
=
depAvg
;
}
public
int
getDepPos
()
{
return
depPos
;
}
public
void
setDepPos
(
int
depPos
)
{
this
.
depPos
=
depPos
;
}
public
int
getGlobalPos
()
{
return
globalPos
;
}
public
void
setGlobalPos
(
int
globalPos
)
{
this
.
globalPos
=
globalPos
;
}
@Override
public
String
toString
()
{
return
"PracownikWDepartamencie [id="
+
id
+
", departmentName="
+
departmentName
+
", firstName="
+
firstName
+
", lastName="
+
lastName
+
", jobTitle="
+
jobTitle
+
", salary="
+
salary
+
", depAvg="
+
depAvg
+
", depPos="
+
depPos
+
", globalPos="
+
globalPos
+
"]"
;
}
@Override
public
int
hashCode
()
{
return
Objects
.
hash
(
depAvg
,
depPos
,
departmentName
,
firstName
,
globalPos
,
id
,
jobTitle
,
lastName
,
salary
);
}
@Override
public
boolean
equals
(
Object
obj
)
{
if
(
this
==
obj
)
return
true
;
if
(
obj
==
null
)
return
false
;
if
(
getClass
()
!=
obj
.
getClass
())
return
false
;
PracownikWDepartamencie
other
=
(
PracownikWDepartamencie
)
obj
;
return
Objects
.
equals
(
depAvg
,
other
.
depAvg
)
&&
depPos
==
other
.
depPos
&&
Objects
.
equals
(
departmentName
,
other
.
departmentName
)
&&
Objects
.
equals
(
firstName
,
other
.
firstName
)
&&
globalPos
==
other
.
globalPos
&&
id
==
other
.
id
&&
Objects
.
equals
(
jobTitle
,
other
.
jobTitle
)
&&
Objects
.
equals
(
lastName
,
other
.
lastName
)
&&
Objects
.
equals
(
salary
,
other
.
salary
);
}
public
String
dajInfo
()
{
return
String
.
format
(
Locale
.
US
,
"%3d | %-12s %-12s %-33s | %8.2f | %-20s | %8.2f | %2d | %3d |"
,
id
,
firstName
,
lastName
,
"("
+
jobTitle
+
")"
,
salary
,
departmentName
,
depAvg
,
depPos
,
globalPos
);
}
public
static
String
naglowek
()
{
return
String
.
format
(
Locale
.
US
,
" id | pracownik | pensja | departament | średnia | nr | nr g|"
);
}
}
PC28-HibernateGotowy/src/main/java/model/Country.java
0 → 100644
View file @
2cada9f6
package
model
;
import
java.io.Serializable
;
import
javax.persistence.*
;
/**
* The persistent class for the countries database table.
*
*/
@Entity
@Table
(
name
=
"countries"
)
@NamedQuery
(
name
=
"Country.findAll"
,
query
=
"SELECT c FROM Country c"
)
public
class
Country
implements
Serializable
{
private
static
final
long
serialVersionUID
=
1L
;
@Id
@Column
(
name
=
"country_id"
)
private
String
countryId
;
@Column
(
name
=
"country_name"
)
private
String
countryName
;
//bi-directional many-to-one association to Region
@ManyToOne
@JoinColumn
(
name
=
"region_id"
)
private
Region
region
;
public
Country
()
{
}
public
String
getCountryId
()
{
return
this
.
countryId
;
}
public
void
setCountryId
(
String
countryId
)
{
this
.
countryId
=
countryId
;
}
public
String
getCountryName
()
{
return
this
.
countryName
;
}
public
void
setCountryName
(
String
countryName
)
{
this
.
countryName
=
countryName
;
}
public
Region
getRegion
()
{
return
this
.
region
;
}
public
void
setRegion
(
Region
region
)
{
this
.
region
=
region
;
}
}
\ No newline at end of file
PC28-HibernateGotowy/src/main/java/model/Department.java
0 → 100644
View file @
2cada9f6
package
model
;
import
java.io.Serializable
;
import
javax.persistence.*
;
import
java.util.List
;
/**
* The persistent class for the departments database table.
*
*/
@Entity
@Table
(
name
=
"departments"
)
@NamedQuery
(
name
=
"Department.findAll"
,
query
=
"SELECT d FROM Department d"
)
public
class
Department
implements
Serializable
{
private
static
final
long
serialVersionUID
=
1L
;
@Id
@SequenceGenerator
(
name
=
"DEPARTMENTS_DEPARTMENTID_GENERATOR"
,
sequenceName
=
"DEPARTMENTS_SEQ"
,
allocationSize
=
10
)
@GeneratedValue
(
strategy
=
GenerationType
.
SEQUENCE
,
generator
=
"DEPARTMENTS_DEPARTMENTID_GENERATOR"
)
@Column
(
name
=
"department_id"
)
private
Integer
departmentId
;
@Column
(
name
=
"department_name"
)
private
String
departmentName
;
@Column
(
name
=
"manager_id"
)
private
Integer
managerId
;
//uni-directional many-to-one association to Location
@ManyToOne
@JoinColumn
(
name
=
"location_id"
)
private
Location
location
;
//bi-directional many-to-one association to Employee
@OneToMany
(
mappedBy
=
"department"
)
private
List
<
Employee
>
employees
;
public
Department
()
{
}
public
Integer
getDepartmentId
()
{
return
this
.
departmentId
;
}
public
void
setDepartmentId
(
Integer
departmentId
)
{
this
.
departmentId
=
departmentId
;
}
public
String
getDepartmentName
()
{
return
this
.
departmentName
;
}
public
void
setDepartmentName
(
String
departmentName
)
{
this
.
departmentName
=
departmentName
;
}
public
Integer
getManagerId
()
{
return
this
.
managerId
;
}
public
void
setManagerId
(
Integer
managerId
)
{
this
.
managerId
=
managerId
;
}
public
Location
getLocation
()
{
return
this
.
location
;
}
public
void
setLocation
(
Location
location
)
{
this
.
location
=
location
;
}
public
List
<
Employee
>
getEmployees
()
{
return
this
.
employees
;
}
public
void
setEmployees
(
List
<
Employee
>
employees
)
{
this
.
employees
=
employees
;
}
public
Employee
addEmployee
(
Employee
employee
)
{
getEmployees
().
add
(
employee
);
employee
.
setDepartment
(
this
);
return
employee
;
}
public
Employee
removeEmployee
(
Employee
employee
)
{
getEmployees
().
remove
(
employee
);
employee
.
setDepartment
(
null
);
return
employee
;
}
}
\ No newline at end of file
PC28-HibernateGotowy/src/main/java/model/Employee.java
0 → 100644
View file @
2cada9f6
package
model
;
import
java.io.Serializable
;
import
javax.persistence.*
;
import
java.math.BigDecimal
;
import
java.util.Date
;
/**
* The persistent class for the employees database table.
*
*/
@Entity
@Table
(
name
=
"employees"
)
@NamedQuery
(
name
=
"Employee.findAll"
,
query
=
"SELECT e FROM Employee e"
)
public
class
Employee
implements
Serializable
{
private
static
final
long
serialVersionUID
=
1L
;
@Id
@SequenceGenerator
(
name
=
"EMPLOYEES_EMPLOYEEID_GENERATOR"
,
sequenceName
=
"EMPLOYEES_SEQ"
,
allocationSize
=
1
)
@GeneratedValue
(
strategy
=
GenerationType
.
SEQUENCE
,
generator
=
"EMPLOYEES_EMPLOYEEID_GENERATOR"
)
@Column
(
name
=
"employee_id"
)
private
Integer
employeeId
;
@Column
(
name
=
"commission_pct"
)
private
BigDecimal
commissionPct
;
private
String
email
;
@Column
(
name
=
"first_name"
)
private
String
firstName
;
@Temporal
(
TemporalType
.
DATE
)
@Column
(
name
=
"hire_date"
)
private
Date
hireDate
;
@Column
(
name
=
"last_name"
)
private
String
lastName
;
@Column
(
name
=
"phone_number"
)
private
String
phoneNumber
;
private
BigDecimal
salary
;
//bi-directional many-to-one association to Department
@ManyToOne
@JoinColumn
(
name
=
"department_id"
)
private
Department
department
;
//uni-directional many-to-one association to Employee
@ManyToOne
@JoinColumn
(
name
=
"manager_id"
)
private
Employee
manager
;
//bi-directional many-to-one association to Job
@ManyToOne
@JoinColumn
(
name
=
"job_id"
)
private
Job
job
;
public
Employee
()
{
}
public
Integer
getEmployeeId
()
{
return
this
.
employeeId
;
}
public
void
setEmployeeId
(
Integer
employeeId
)
{
this
.
employeeId
=
employeeId
;
}
public
BigDecimal
getCommissionPct
()
{
return
this
.
commissionPct
;
}
public
void
setCommissionPct
(
BigDecimal
commissionPct
)
{
this
.
commissionPct
=
commissionPct
;
}
public
String
getEmail
()
{
return
this
.
email
;
}
public
void
setEmail
(
String
email
)
{
this
.
email
=
email
;
}
public
String
getFirstName
()
{
return
this
.
firstName
;
}
public
void
setFirstName
(
String
firstName
)
{
this
.
firstName
=
firstName
;
}
public
Date
getHireDate
()
{
return
this
.
hireDate
;
}
public
void
setHireDate
(
Date
hireDate
)
{
this
.
hireDate
=
hireDate
;
}
public
String
getLastName
()
{
return
this
.
lastName
;
}
public
void
setLastName
(
String
lastName
)
{
this
.
lastName
=
lastName
;
}
public
String
getPhoneNumber
()
{
return
this
.
phoneNumber
;
}
public
void
setPhoneNumber
(
String
phoneNumber
)
{
this
.
phoneNumber
=
phoneNumber
;
}
public
BigDecimal
getSalary
()
{
return
this
.
salary
;
}
public
void
setSalary
(
BigDecimal
salary
)
{
this
.
salary
=
salary
;
}
public
Department
getDepartment
()
{
return
this
.
department
;
}
public
void
setDepartment
(
Department
department
)
{
this
.
department
=
department
;
}
public
Employee
getManager
()
{
return
this
.
manager
;
}
public
void
setManager
(
Employee
manager
)
{
this
.
manager
=
manager
;
}
public
Job
getJob
()
{
return
this
.
job
;
}
public
void
setJob
(
Job
job
)
{
this
.
job
=
job
;
}
}
\ No newline at end of file
PC28-HibernateGotowy/src/main/java/model/Job.java
0 → 100644
View file @
2cada9f6
package
model
;
import
java.io.Serializable
;
import
javax.persistence.*
;
import
java.math.BigDecimal
;
import
java.util.List
;
/**
* The persistent class for the jobs database table.
*
*/
@Entity
@Table
(
name
=
"jobs"
)
@NamedQuery
(
name
=
"Job.findAll"
,
query
=
"SELECT j FROM Job j"
)
public
class
Job
implements
Serializable
{
private
static
final
long
serialVersionUID
=
1L
;
@Id
@Column
(
name
=
"job_id"
)
private
String
jobId
;
@Column
(
name
=
"job_title"
)
private
String
jobTitle
;
@Column
(
name
=
"max_salary"
)
private
BigDecimal
maxSalary
;
@Column
(
name
=
"min_salary"
)
private
BigDecimal
minSalary
;
//bi-directional many-to-one association to Employee
@OneToMany
(
mappedBy
=
"job"
)
private
List
<
Employee
>
employees
;
public
Job
()
{
}
public
String
getJobId
()
{
return
this
.
jobId
;
}
public
void
setJobId
(
String
jobId
)
{
this
.
jobId
=
jobId
;
}
public
String
getJobTitle
()
{
return
this
.
jobTitle
;
}
public
void
setJobTitle
(
String
jobTitle
)
{
this
.
jobTitle
=
jobTitle
;
}
public
BigDecimal
getMaxSalary
()
{
return
this
.
maxSalary
;
}
public
void
setMaxSalary
(
BigDecimal
maxSalary
)
{
this
.
maxSalary
=
maxSalary
;
}
public
BigDecimal
getMinSalary
()
{
return
this
.
minSalary
;
}
public
void
setMinSalary
(
BigDecimal
minSalary
)
{
this
.
minSalary
=
minSalary
;
}
public
List
<
Employee
>
getEmployees
()
{
return
this
.
employees
;
}
public
void
setEmployees
(
List
<
Employee
>
employees
)
{
this
.
employees
=
employees
;
}
public
Employee
addEmployee
(
Employee
employee
)
{
getEmployees
().
add
(
employee
);
employee
.
setJob
(
this
);
return
employee
;
}
public
Employee
removeEmployee
(
Employee
employee
)
{
getEmployees
().
remove
(
employee
);
employee
.
setJob
(
null
);
return
employee
;
}
}
\ No newline at end of file
PC28-HibernateGotowy/src/main/java/model/Location.java
0 → 100644
View file @
2cada9f6
package
model
;
import
java.io.Serializable
;
import
javax.persistence.*
;
/**
* The persistent class for the locations database table.
*
*/
@Entity
@Table
(
name
=
"locations"
)
@NamedQuery
(
name
=
"Location.findAll"
,
query
=
"SELECT l FROM Location l"
)
public
class
Location
implements
Serializable
{
private
static
final
long
serialVersionUID
=
1L
;
@Id
@SequenceGenerator
(
name
=
"LOCATIONS_LOCATIONID_GENERATOR"
,
sequenceName
=
"LOCATIONS_SEQ"
,
allocationSize
=
100
)
@GeneratedValue
(
strategy
=
GenerationType
.
SEQUENCE
,
generator
=
"LOCATIONS_LOCATIONID_GENERATOR"
)
@Column
(
name
=
"location_id"
)
private
Integer
locationId
;
private
String
city
;
@Column
(
name
=
"postal_code"
)
private
String
postalCode
;
@Column
(
name
=
"state_province"
)
private
String
stateProvince
;
@Column
(
name
=
"street_address"
)
private
String
streetAddress
;
//uni-directional many-to-one association to Country
@ManyToOne
@JoinColumn
(
name
=
"country_id"
)
private
Country
country
;
public
Location
()
{
}
public
Integer
getLocationId
()
{
return
this
.
locationId
;
}
public
void
setLocationId
(
Integer
locationId
)
{
this
.
locationId
=
locationId
;
}
public
String
getCity
()
{
return
this
.
city
;
}
public
void
setCity
(
String
city
)
{
this
.
city
=
city
;
}
public
String
getPostalCode
()
{
return
this
.
postalCode
;
}
public
void
setPostalCode
(
String
postalCode
)
{
this
.
postalCode
=
postalCode
;
}
public
String
getStateProvince
()
{
return
this
.
stateProvince
;
}
public
void
setStateProvince
(
String
stateProvince
)
{
this
.
stateProvince
=
stateProvince
;
}
public
String
getStreetAddress
()
{
return
this
.
streetAddress
;
}
public
void
setStreetAddress
(
String
streetAddress
)
{
this
.
streetAddress
=
streetAddress
;
}
public
Country
getCountry
()
{
return
this
.
country
;
}
public
void
setCountry
(
Country
country
)
{
this
.
country
=
country
;
}
}
\ No newline at end of file
PC28-HibernateGotowy/src/main/java/model/Region.java
0 → 100644
View file @
2cada9f6
package
model
;
import
java.io.Serializable
;
import
javax.persistence.*
;
import
java.util.List
;
/**
* The persistent class for the regions database table.
*
*/
@Entity
@Table
(
name
=
"regions"
)
@NamedQuery
(
name
=
"Region.findAll"
,
query
=
"SELECT r FROM Region r"
)
public
class
Region
implements
Serializable
{
private
static
final
long
serialVersionUID
=
1L
;
@Id
@Column
(
name
=
"region_id"
)
private
Integer
regionId
;
@Column
(
name
=
"region_name"
)
private
String
regionName
;
//bi-directional many-to-one association to Country
@OneToMany
(
mappedBy
=
"region"
)
private
List
<
Country
>
countries
;
public
Region
()
{
}
public
Integer
getRegionId
()
{
return
this
.
regionId
;
}
public
void
setRegionId
(
Integer
regionId
)
{
this
.
regionId
=
regionId
;
}
public
String
getRegionName
()
{
return
this
.
regionName
;
}
public
void
setRegionName
(
String
regionName
)
{
this
.
regionName
=
regionName
;
}
public
List
<
Country
>
getCountries
()
{
return
this
.
countries
;
}
public
void
setCountries
(
List
<
Country
>
countries
)
{
this
.
countries
=
countries
;
}
public
Country
addCountry
(
Country
country
)
{
getCountries
().
add
(
country
);
country
.
setRegion
(
this
);
return
country
;
}
public
Country
removeCountry
(
Country
country
)
{
getCountries
().
remove
(
country
);
country
.
setRegion
(
null
);
return
country
;
}
}
\ No newline at end of file
PC28-HibernateGotowy/src/main/java/nasze_przyklady/ModyfikujRekord.java
0 → 100644
View file @
2cada9f6
package
nasze_przyklady
;
import
java.math.BigDecimal
;
import
javax.persistence.EntityManager
;
import
javax.persistence.EntityManagerFactory
;
import
javax.persistence.Persistence
;
import
model.Employee
;
public
class
ModyfikujRekord
{
private
static
final
BigDecimal
ZMIANA
=
new
BigDecimal
(
"100.00"
);
public
static
void
main
(
String
[]
args
)
{
int
id
=
100
;
System
.
out
.
println
(
"Startujemy..."
);
EntityManagerFactory
emf
=
Persistence
.
createEntityManagerFactory
(
"hr"
);
EntityManager
em
=
emf
.
createEntityManager
();
em
.
getTransaction
().
begin
();
Employee
emp
=
em
.
find
(
Employee
.
class
,
id
);
if
(
emp
==
null
)
{
System
.
out
.
println
(
"Nie ma takiego rekordu "
+
id
);
return
;
}
System
.
out
.
println
(
"Znaleziono: "
+
emp
);
System
.
out
.
println
(
emp
.
getFirstName
()
+
" "
+
emp
.
getLastName
()
+
" "
+
emp
.
getSalary
());
emp
.
setLastName
(
"KING"
);
emp
.
setSalary
(
emp
.
getSalary
().
add
(
ZMIANA
));
emp
.
getDepartment
().
getLocation
().
setCity
(
"Żyrardów"
);
System
.
out
.
println
(
emp
.
getFirstName
()
+
" "
+
emp
.
getLastName
()
+
" "
+
emp
.
getSalary
());
em
.
getTransaction
().
commit
();
em
.
close
();
emf
.
close
();
System
.
out
.
println
(
"Kończymy..."
);
}
}
PC28-HibernateGotowy/src/main/java/nasze_przyklady/OdczytajJedenRekord.java
0 → 100644
View file @
2cada9f6
package
nasze_przyklady
;
import
javax.persistence.EntityManager
;
import
javax.persistence.EntityManagerFactory
;
import
javax.persistence.Persistence
;
import
model.Employee
;
public
class
OdczytajJedenRekord
{
public
static
void
main
(
String
[]
args
)
{
int
id
=
100
;
System
.
out
.
println
(
"Startujemy..."
);
EntityManagerFactory
emf
=
Persistence
.
createEntityManagerFactory
(
"hr"
);
System
.
out
.
println
(
"EMF: "
+
emf
);
EntityManager
em
=
emf
.
createEntityManager
();
System
.
out
.
println
(
"EM : "
+
em
);
Employee
emp
=
em
.
find
(
Employee
.
class
,
id
);
if
(
emp
==
null
)
{
System
.
out
.
println
(
"Nie ma takiego rekordu "
+
id
);
}
else
{
System
.
out
.
println
(
"Znaleziono: "
+
emp
);
System
.
out
.
println
(
emp
.
getFirstName
()
+
" "
+
emp
.
getLastName
()
+
" "
+
emp
.
getSalary
());
}
em
.
close
();
emf
.
close
();
}
}
PC28-HibernateGotowy/src/main/java/nasze_przyklady/OdczytajWgMiasta.java
0 → 100644
View file @
2cada9f6
package
nasze_przyklady
;
import
java.util.List
;
import
java.util.Scanner
;
import
javax.persistence.EntityManager
;
import
javax.persistence.EntityManagerFactory
;
import
javax.persistence.Persistence
;
import
javax.persistence.TypedQuery
;
import
model.Employee
;
public
class
OdczytajWgMiasta
{
public
static
void
main
(
String
[]
args
)
{
Scanner
sc
=
new
Scanner
(
System
.
in
);
System
.
out
.
println
(
"Startujemy..."
);
EntityManagerFactory
emf
=
null
;
EntityManager
em
=
null
;
try
{
emf
=
Persistence
.
createEntityManagerFactory
(
"hr"
);
em
=
emf
.
createEntityManager
();
while
(
true
)
{
System
.
out
.
println
(
"Podaj nazwę miasta (enter aby zakończyć)"
);
String
miasto
=
sc
.
nextLine
().
trim
();
if
(
miasto
.
isEmpty
())
break
;
TypedQuery
<
Employee
>
query
=
em
.
createQuery
(
"SELECT emp FROM Employee emp WHERE emp.department.location.city = :parametrMiasto"
,
Employee
.
class
);
query
.
setParameter
(
"parametrMiasto"
,
miasto
);
List
<
Employee
>
lista
=
query
.
getResultList
();
System
.
out
.
println
(
"Odczytano "
+
lista
.
size
()
+
" rekordów."
);
for
(
Employee
emp
:
lista
)
{
System
.
out
.
printf
(
"%-15s %-15s %8s %s\n"
,
emp
.
getFirstName
(),
emp
.
getLastName
(),
emp
.
getSalary
(),
emp
.
getJob
().
getJobTitle
());
}
}
}
finally
{
em
.
close
();
emf
.
close
();
}
}
}
PC28-HibernateGotowy/src/main/java/nasze_przyklady/OdczytajWszystkich1.java
0 → 100644
View file @
2cada9f6
package
nasze_przyklady
;
import
java.util.List
;
import
javax.persistence.EntityManager
;
import
javax.persistence.EntityManagerFactory
;
import
javax.persistence.Persistence
;
import
javax.persistence.TypedQuery
;
import
model.Employee
;
public
class
OdczytajWszystkich1
{
public
static
void
main
(
String
[]
args
)
{
EntityManagerFactory
emf
=
Persistence
.
createEntityManagerFactory
(
"hr"
);
EntityManager
em
=
emf
.
createEntityManager
();
TypedQuery
<
Employee
>
query
=
em
.
createQuery
(
"SELECT e FROM Employee e"
,
Employee
.
class
);
List
<
Employee
>
lista
=
query
.
getResultList
();
for
(
Employee
emp
:
lista
)
{
System
.
out
.
println
(
emp
.
getFirstName
()
+
" "
+
emp
.
getLastName
()
+
" "
+
emp
.
getSalary
());
}
em
.
close
();
emf
.
close
();
}
}
PC28-HibernateGotowy/src/main/java/nasze_przyklady/OdczytajWszystkich2.java
0 → 100644
View file @
2cada9f6
package
nasze_przyklady
;
import
java.util.List
;
import
javax.persistence.EntityManager
;
import
javax.persistence.EntityManagerFactory
;
import
javax.persistence.Persistence
;
import
javax.persistence.TypedQuery
;
import
model.Employee
;
public
class
OdczytajWszystkich2
{
public
static
void
main
(
String
[]
args
)
{
EntityManagerFactory
emf
=
Persistence
.
createEntityManagerFactory
(
"hr"
);
EntityManager
em
=
emf
.
createEntityManager
();
TypedQuery
<
Employee
>
query
=
em
.
createNamedQuery
(
"Employee.findAll"
,
Employee
.
class
);
List
<
Employee
>
lista
=
query
.
getResultList
();
for
(
Employee
emp
:
lista
)
{
System
.
out
.
println
(
emp
.
getFirstName
()
+
" "
+
emp
.
getLastName
()
+
" "
+
emp
.
getSalary
());
}
em
.
close
();
emf
.
close
();
}
}
PC28-HibernateGotowy/src/main/java/nasze_przyklady/Odczytaj_wg_Nazwisk.java
0 → 100644
View file @
2cada9f6
package
nasze_przyklady
;
import
java.util.List
;
import
javax.persistence.EntityManager
;
import
javax.persistence.EntityManagerFactory
;
import
javax.persistence.Persistence
;
import
javax.persistence.TypedQuery
;
import
model.Employee
;
public
class
Odczytaj_wg_Nazwisk
{
public
static
void
main
(
String
[]
args
)
{
String
kogoSzukam
=
"King"
;
EntityManagerFactory
emf
=
Persistence
.
createEntityManagerFactory
(
"hr"
);
EntityManager
em
=
emf
.
createEntityManager
();
// TypedQuery<Employee> query = em.createQuery("SELECT e FROM Employee e WHERE lastName = ?1", Employee.class);
// query.setParameter(1, kogoSzukam);
TypedQuery
<
Employee
>
query
=
em
.
createQuery
(
"SELECT e FROM Employee e WHERE lastName = :nazwisko"
,
Employee
.
class
);
query
.
setParameter
(
"nazwisko"
,
kogoSzukam
);
List
<
Employee
>
lista
=
query
.
getResultList
();
for
(
Employee
emp
:
lista
)
{
System
.
out
.
println
(
emp
.
getFirstName
()
+
" "
+
emp
.
getLastName
()
+
" "
+
emp
.
getSalary
());
}
em
.
close
();
emf
.
close
();
}
}
PC28-HibernateGotowy/src/main/java/nasze_przyklady/RekordyZalezne.java
0 → 100644
View file @
2cada9f6
package
nasze_przyklady
;
import
javax.persistence.EntityManager
;
import
javax.persistence.EntityManagerFactory
;
import
javax.persistence.Persistence
;
import
model.Employee
;
import
model.Location
;
public
class
RekordyZalezne
{
public
static
void
main
(
String
[]
args
)
{
int
id
=
100
;
System
.
out
.
println
(
"Startujemy..."
);
EntityManagerFactory
emf
=
Persistence
.
createEntityManagerFactory
(
"hr"
);
System
.
out
.
println
(
"EMF: "
+
emf
);
EntityManager
em
=
emf
.
createEntityManager
();
System
.
out
.
println
(
"EM : "
+
em
);
Employee
emp
=
em
.
find
(
Employee
.
class
,
id
);
if
(
emp
==
null
)
{
System
.
out
.
println
(
"Nie ma takiego rekordu "
+
id
);
}
else
{
System
.
out
.
println
(
"Znaleziono: "
+
emp
);
System
.
out
.
println
(
emp
.
getFirstName
()
+
" "
+
emp
.
getLastName
()
+
" "
+
emp
.
getSalary
());
// TODO odczytaj nazwę departamentu, miasto, kraj, nazwę stanowiska (jobTitle)...
// przy takim zapisie uwaga na nulle!
System
.
out
.
println
(
"Stanowisko: "
+
emp
.
getJob
().
getJobTitle
());
System
.
out
.
println
(
"Departament: "
+
emp
.
getDepartment
().
getDepartmentName
());
Location
location
=
emp
.
getDepartment
().
getLocation
();
System
.
out
.
println
(
"Miasto i kraj: "
+
location
.
getCity
()
+
" "
+
location
.
getCountry
().
getCountryName
());
}
em
.
close
();
emf
.
close
();
}
}
PC28-HibernateGotowy/src/main/java/przyklady/AktualizujRekord1.java
0 → 100644
View file @
2cada9f6
package
przyklady
;
import
java.math.BigDecimal
;
import
javax.persistence.EntityManager
;
import
javax.persistence.EntityManagerFactory
;
import
javax.persistence.Persistence
;
import
model.Employee
;
public
class
AktualizujRekord1
{
public
static
void
main
(
String
[]
args
)
{
int
id
=
100
;
int
zmiana
=
200
;
EntityManagerFactory
emf
=
null
;
EntityManager
em
=
null
;
try
{
emf
=
Persistence
.
createEntityManagerFactory
(
"hr"
);
em
=
emf
.
createEntityManager
();
em
.
getTransaction
().
begin
();
Employee
emp
=
em
.
find
(
Employee
.
class
,
id
);
if
(
emp
==
null
)
{
System
.
out
.
println
(
"nie ma"
);
return
;
}
System
.
out
.
println
(
"Pensja przed zmianą: "
+
emp
.
getSalary
());
emp
.
setSalary
(
emp
.
getSalary
().
add
(
BigDecimal
.
valueOf
(
zmiana
)));
System
.
out
.
println
(
"Pensja po zmianie: "
+
emp
.
getSalary
());
// flush wykonuje operacje po stronie bazy danych, ale ich nie komituje
//em.flush();
// jeśli robimy commit, to nie potrzebujemy flush
em
.
getTransaction
().
commit
();
System
.
out
.
println
(
"Zapisane"
);
}
finally
{
if
(
em
!=
null
)
em
.
close
();
if
(
emf
!=
null
)
emf
.
close
();
}
}
}
PC28-HibernateGotowy/src/main/java/przyklady/AktualizujRekord2.java
0 → 100644
View file @
2cada9f6
package
przyklady
;
import
javax.persistence.EntityManager
;
import
javax.persistence.EntityManagerFactory
;
import
javax.persistence.Persistence
;
import
model.Department
;
import
model.Employee
;
public
class
AktualizujRekord2
{
public
static
void
main
(
String
[]
args
)
{
EntityManagerFactory
emf
=
null
;
EntityManager
em
=
null
;
try
{
emf
=
Persistence
.
createEntityManagerFactory
(
"hr"
);
em
=
emf
.
createEntityManager
();
em
.
getTransaction
().
begin
();
Employee
king
=
em
.
find
(
Employee
.
class
,
100
);
System
.
out
.
println
(
king
.
getEmployeeId
()
+
" "
+
king
.
getFirstName
()
+
" "
+
king
.
getLastName
());
Employee
kochhar
=
em
.
find
(
Employee
.
class
,
101
);
System
.
out
.
println
(
kochhar
.
getEmployeeId
()
+
" "
+
kochhar
.
getFirstName
()
+
" "
+
kochhar
.
getLastName
());
Department
dep
=
em
.
find
(
Department
.
class
,
90
);
System
.
out
.
println
(
"departament: "
+
dep
.
getDepartmentName
());
System
.
out
.
println
(
dep
==
king
.
getDepartment
());
System
.
out
.
println
(
dep
==
kochhar
.
getDepartment
());
System
.
out
.
println
();
System
.
out
.
println
(
"Departamenty przed zmianą: "
+
dep
.
getDepartmentName
()
+
" "
+
king
.
getDepartment
().
getDepartmentName
()
+
" "
+
kochhar
.
getDepartment
().
getDepartmentName
());
king
.
getDepartment
().
setDepartmentName
(
"Nowa nazwa"
);
// king.getDepartment().setDepartmentName("Executive");
System
.
out
.
println
(
"Departamenty po zmianie: "
+
dep
.
getDepartmentName
()
+
" "
+
king
.
getDepartment
().
getDepartmentName
()
+
" "
+
kochhar
.
getDepartment
().
getDepartmentName
());
System
.
out
.
println
(
dep
==
king
.
getDepartment
());
System
.
out
.
println
(
dep
==
kochhar
.
getDepartment
());
em
.
getTransaction
().
commit
();
System
.
out
.
println
(
"Zapisane"
);
}
finally
{
if
(
em
!=
null
)
em
.
close
();
if
(
emf
!=
null
)
emf
.
close
();
}
}
}
PC28-HibernateGotowy/src/main/java/przyklady/Criteria1.java
0 → 100644
View file @
2cada9f6
package
przyklady
;
import
java.util.List
;
import
javax.persistence.EntityManager
;
import
javax.persistence.EntityManagerFactory
;
import
javax.persistence.Persistence
;
import
javax.persistence.TypedQuery
;
import
javax.persistence.criteria.CriteriaBuilder
;
import
javax.persistence.criteria.CriteriaQuery
;
import
javax.persistence.criteria.Root
;
import
model.Employee
;
public
class
Criteria1
{
public
static
void
main
(
String
[]
args
)
{
EntityManagerFactory
emf
=
Persistence
.
createEntityManagerFactory
(
"hr"
);
EntityManager
em
=
emf
.
createEntityManager
();
CriteriaBuilder
cb
=
em
.
getCriteriaBuilder
();
CriteriaQuery
<
Employee
>
cq
=
cb
.
createQuery
(
Employee
.
class
);
Root
<
Employee
>
root
=
cq
.
from
(
Employee
.
class
);
cq
.
select
(
root
);
TypedQuery
<
Employee
>
query
=
em
.
createQuery
(
cq
);
List
<
Employee
>
lista
=
query
.
getResultList
();
System
.
out
.
println
(
"Odczytano "
+
lista
.
size
()
+
" rekordów."
);
for
(
Employee
emp
:
lista
)
{
System
.
out
.
printf
(
"%-15s %-15s %8s %s\n"
,
emp
.
getFirstName
(),
emp
.
getLastName
(),
emp
.
getSalary
(),
emp
.
getJob
().
getJobTitle
());
}
em
.
close
();
emf
.
close
();
}
}
PC28-HibernateGotowy/src/main/java/przyklady/Criteria2.java
0 → 100644
View file @
2cada9f6
package
przyklady
;
import
java.math.BigDecimal
;
import
java.util.List
;
import
javax.persistence.EntityManager
;
import
javax.persistence.EntityManagerFactory
;
import
javax.persistence.Persistence
;
import
javax.persistence.TypedQuery
;
import
javax.persistence.criteria.CriteriaBuilder
;
import
javax.persistence.criteria.CriteriaQuery
;
import
javax.persistence.criteria.Root
;
import
model.Employee
;
public
class
Criteria2
{
public
static
void
main
(
String
[]
args
)
{
BigDecimal
minSalary
=
new
BigDecimal
(
10000
);
EntityManagerFactory
emf
=
Persistence
.
createEntityManagerFactory
(
"hr"
);
EntityManager
em
=
emf
.
createEntityManager
();
CriteriaBuilder
cb
=
em
.
getCriteriaBuilder
();
// podajmey klasę dla wyników
CriteriaQuery
<
Employee
>
cq
=
cb
.
createQuery
(
Employee
.
class
);
// podajemy encję / tabelę źródłową, od której zapytanie startuje
Root
<
Employee
>
root
=
cq
.
from
(
Employee
.
class
);
// tak jakby dopisywanie kolejnych fragmentów zapytania
cq
.
select
(
root
);
cq
.
where
(
cb
.
ge
(
root
.
get
(
"salary"
),
minSalary
));
cq
.
orderBy
(
cb
.
desc
(
root
.
get
(
"salary"
)));
// Tutaj zastosowałem tworzenie kryteriów "dynamicznie" na podstawie nazw kolumn
// Istnieje też możliwość wygenerowanie "metamodelu" i posługiwania się nim
TypedQuery
<
Employee
>
query
=
em
.
createQuery
(
cq
);
List
<
Employee
>
lista
=
query
.
getResultList
();
System
.
out
.
println
(
"Odczytano "
+
lista
.
size
()
+
" rekordów."
);
for
(
Employee
emp
:
lista
)
{
System
.
out
.
printf
(
"%-15s %-15s %8s %s\n"
,
emp
.
getFirstName
(),
emp
.
getLastName
(),
emp
.
getSalary
(),
emp
.
getJob
().
getJobTitle
());
}
em
.
close
();
emf
.
close
();
}
}
PC28-HibernateGotowy/src/main/java/przyklady/Criteria3.java
0 → 100644
View file @
2cada9f6
package
przyklady
;
import
java.math.BigDecimal
;
import
java.util.List
;
import
javax.persistence.EntityManager
;
import
javax.persistence.EntityManagerFactory
;
import
javax.persistence.Persistence
;
import
javax.persistence.TypedQuery
;
import
javax.persistence.criteria.CriteriaBuilder
;
import
javax.persistence.criteria.CriteriaQuery
;
import
javax.persistence.criteria.Root
;
import
model.Employee
;
public
class
Criteria3
{
public
static
void
main
(
String
[]
args
)
{
BigDecimal
minSalary
=
new
BigDecimal
(
4000
);
String
szukaneMiasto
=
"Seattle"
;
EntityManagerFactory
emf
=
Persistence
.
createEntityManagerFactory
(
"hr"
);
EntityManager
em
=
emf
.
createEntityManager
();
CriteriaBuilder
cb
=
em
.
getCriteriaBuilder
();
CriteriaQuery
<
Employee
>
cq
=
cb
.
createQuery
(
Employee
.
class
);
Root
<
Employee
>
root
=
cq
.
from
(
Employee
.
class
);
cq
.
select
(
root
);
cq
.
where
(
cb
.
and
(
cb
.
ge
(
root
.
get
(
"salary"
),
minSalary
),
cb
.
equal
(
root
.
get
(
"department"
).
get
(
"location"
).
get
(
"city"
),
szukaneMiasto
)));
cq
.
orderBy
(
cb
.
desc
(
root
.
get
(
"salary"
)));
TypedQuery
<
Employee
>
query
=
em
.
createQuery
(
cq
);
List
<
Employee
>
lista
=
query
.
getResultList
();
System
.
out
.
println
(
"Odczytano "
+
lista
.
size
()
+
" rekordów."
);
for
(
Employee
emp
:
lista
)
{
String
miasto
=
emp
.
getDepartment
()
==
null
?
"-"
:
emp
.
getDepartment
().
getLocation
().
getCity
();
System
.
out
.
printf
(
"%-15s %-15s %-10s %8s %s\n"
,
emp
.
getFirstName
(),
emp
.
getLastName
(),
miasto
,
emp
.
getSalary
(),
emp
.
getJob
().
getJobTitle
());
}
em
.
close
();
emf
.
close
();
}
}
PC28-HibernateGotowy/src/main/java/przyklady/DodajNoweRekordy.java
0 → 100644
View file @
2cada9f6
package
przyklady
;
import
javax.persistence.EntityManager
;
import
javax.persistence.EntityManagerFactory
;
import
javax.persistence.Persistence
;
import
model.Country
;
import
model.Location
;
import
model.Region
;
public
class
DodajNoweRekordy
{
public
static
void
main
(
String
[]
args
)
{
EntityManagerFactory
emf
=
null
;
EntityManager
em
=
null
;
try
{
emf
=
Persistence
.
createEntityManagerFactory
(
"hr"
);
em
=
emf
.
createEntityManager
();
em
.
getTransaction
().
begin
();
// Utworzyć nowy kraj (my podajemy id) i nową lokalizację (id zostanie wygenerowane).
Region
region
=
em
.
find
(
Region
.
class
,
1
);
Country
polska
=
new
Country
();
polska
.
setCountryId
(
"PL"
);
polska
.
setCountryName
(
"Polska"
);
polska
.
setRegion
(
region
);
em
.
persist
(
polska
);
// Dla lokalizacji nie podajemy ID - ono zostanie wygenerowane z sekwencji.
Location
loc
=
new
Location
();
loc
.
setCountry
(
polska
);
loc
.
setStreetAddress
(
"Jasna"
);
loc
.
setCity
(
"Warszawa"
);
loc
.
setPostalCode
(
"00-123"
);
System
.
out
.
println
(
"id przed dodaniem "
+
loc
.
getLocationId
());
em
.
persist
(
loc
);
System
.
out
.
println
(
"id po dodaniu "
+
loc
.
getLocationId
());
em
.
getTransaction
().
commit
();
System
.
out
.
println
(
"Zapisano."
);
}
finally
{
if
(
em
!=
null
)
em
.
close
();
if
(
emf
!=
null
)
emf
.
close
();
}
}
}
PC28-HibernateGotowy/src/main/java/przyklady/Odczyt01_JedenRekord.java
0 → 100644
View file @
2cada9f6
package
przyklady
;
import
javax.persistence.EntityManager
;
import
javax.persistence.EntityManagerFactory
;
import
javax.persistence.Persistence
;
import
model.Employee
;
public
class
Odczyt01_JedenRekord
{
public
static
void
main
(
String
[]
args
)
{
System
.
out
.
println
(
"Początek"
);
EntityManagerFactory
emf
=
Persistence
.
createEntityManagerFactory
(
"hr"
);
EntityManager
em
=
emf
.
createEntityManager
();
System
.
out
.
println
(
"Udało się połączyć, em = "
+
em
);
Employee
emp
=
em
.
find
(
Employee
.
class
,
100
);
if
(
emp
==
null
)
{
System
.
out
.
println
(
"nie znaleziono"
);
}
else
{
System
.
out
.
println
(
emp
.
getFirstName
()
+
" "
+
emp
.
getLastName
());
}
em
.
close
();
emf
.
close
();
}
}
PC28-HibernateGotowy/src/main/java/przyklady/Odczyt02_JedenRekord_InneTabele.java
0 → 100644
View file @
2cada9f6
package
przyklady
;
import
java.util.Scanner
;
import
javax.persistence.EntityManager
;
import
javax.persistence.EntityManagerFactory
;
import
javax.persistence.Persistence
;
import
model.Department
;
import
model.Employee
;
public
class
Odczyt02_JedenRekord_InneTabele
{
public
static
void
main
(
String
[]
args
)
{
Scanner
sc
=
new
Scanner
(
System
.
in
);
EntityManagerFactory
emf
=
null
;
EntityManager
em
=
null
;
try
{
emf
=
Persistence
.
createEntityManagerFactory
(
"hr"
);
em
=
emf
.
createEntityManager
();
System
.
out
.
println
(
"Udało się połączyć, em = "
+
em
);
while
(
true
)
{
System
.
out
.
print
(
"Podaj id pracownika: "
);
if
(!
sc
.
hasNextInt
())
break
;
int
id
=
sc
.
nextInt
();
Employee
emp
=
em
.
find
(
Employee
.
class
,
id
);
if
(
emp
==
null
)
{
System
.
out
.
println
(
"nie znaleziono"
);
}
else
{
System
.
out
.
println
(
emp
.
getFirstName
()
+
" "
+
emp
.
getLastName
()
+
" $"
+
emp
.
getSalary
());
System
.
out
.
println
(
"job: "
+
emp
.
getJob
().
getJobTitle
());
Department
department
=
emp
.
getDepartment
();
if
(
department
!=
null
)
{
System
.
out
.
println
(
"departament: "
+
department
.
getDepartmentName
());
System
.
out
.
println
(
"adres: "
+
department
.
getLocation
().
getStreetAddress
()
+
" "
+
department
.
getLocation
().
getCity
()
+
" "
+
department
.
getLocation
().
getCountry
().
getCountryName
());
}
System
.
out
.
println
();
}
}
}
finally
{
if
(
em
!=
null
)
em
.
close
();
if
(
emf
!=
null
)
emf
.
close
();
}
}
}
PC28-HibernateGotowy/src/main/java/przyklady/Odczyt03_WszystkieRekordy_FindAll.java
0 → 100644
View file @
2cada9f6
package
przyklady
;
import
java.util.List
;
import
javax.persistence.EntityManager
;
import
javax.persistence.EntityManagerFactory
;
import
javax.persistence.Persistence
;
import
javax.persistence.TypedQuery
;
import
model.Employee
;
public
class
Odczyt03_WszystkieRekordy_FindAll
{
public
static
void
main
(
String
[]
args
)
{
EntityManagerFactory
emf
=
null
;
EntityManager
em
=
null
;
try
{
emf
=
Persistence
.
createEntityManagerFactory
(
"hr"
);
em
=
emf
.
createEntityManager
();
TypedQuery
<
Employee
>
query
=
em
.
createNamedQuery
(
"Employee.findAll"
,
Employee
.
class
);
// Wczytuje wszystkie dane od razu do pamięci
List
<
Employee
>
lista
=
query
.
getResultList
();
System
.
out
.
println
(
"Ilość rekordów "
+
lista
.
size
());
for
(
Employee
employee
:
lista
)
{
System
.
out
.
println
(
employee
.
getFirstName
()
+
" "
+
employee
.
getLastName
());
}
}
finally
{
if
(
em
!=
null
)
em
.
close
();
if
(
emf
!=
null
)
emf
.
close
();
}
}
}
PC28-HibernateGotowy/src/main/java/przyklady/Odczyt04_WszystkieRekordy_Stream.java
0 → 100644
View file @
2cada9f6
package
przyklady
;
import
java.util.stream.Stream
;
import
javax.persistence.EntityManager
;
import
javax.persistence.EntityManagerFactory
;
import
javax.persistence.Persistence
;
import
javax.persistence.TypedQuery
;
import
model.Employee
;
public
class
Odczyt04_WszystkieRekordy_Stream
{
public
static
void
main
(
String
[]
args
)
{
EntityManagerFactory
emf
=
null
;
EntityManager
em
=
null
;
try
{
emf
=
Persistence
.
createEntityManagerFactory
(
"hr"
);
em
=
emf
.
createEntityManager
();
TypedQuery
<
Employee
>
query
=
em
.
createNamedQuery
(
"Employee.findAll"
,
Employee
.
class
);
// Używając Stream zamiast List pobieramy dane "strumieniowo", czyli pojedynczo i przetwarzamy na bieżąco.
Stream
<
Employee
>
stream
=
query
.
getResultStream
();
stream
.
forEach
(
emp
->
{
System
.
out
.
println
(
emp
.
getFirstName
()
+
" "
+
emp
.
getLastName
());
});
}
finally
{
if
(
em
!=
null
)
em
.
close
();
if
(
emf
!=
null
)
emf
.
close
();
}
}
}
PC28-HibernateGotowy/src/main/java/przyklady/Odczyt05_WszystkieRekordy_JPQL.java
0 → 100644
View file @
2cada9f6
package
przyklady
;
import
java.util.List
;
import
javax.persistence.EntityManager
;
import
javax.persistence.EntityManagerFactory
;
import
javax.persistence.Persistence
;
import
javax.persistence.TypedQuery
;
import
model.Employee
;
public
class
Odczyt05_WszystkieRekordy_JPQL
{
public
static
void
main
(
String
[]
args
)
{
EntityManagerFactory
emf
=
null
;
EntityManager
em
=
null
;
try
{
emf
=
Persistence
.
createEntityManagerFactory
(
"hr"
);
em
=
emf
.
createEntityManager
();
TypedQuery
<
Employee
>
query
=
em
.
createQuery
(
"SELECT emp FROM Employee emp"
,
Employee
.
class
);
List
<
Employee
>
lista
=
query
.
getResultList
();
System
.
out
.
println
(
"Ilość rekordów "
+
lista
.
size
());
for
(
Employee
employee
:
lista
)
{
System
.
out
.
println
(
employee
.
getFirstName
()
+
" "
+
employee
.
getLastName
());
}
}
finally
{
if
(
em
!=
null
)
em
.
close
();
if
(
emf
!=
null
)
emf
.
close
();
}
}
}
PC28-HibernateGotowy/src/main/java/przyklady/Odczyt06_Warunek.java
0 → 100644
View file @
2cada9f6
package
przyklady
;
import
java.util.List
;
import
javax.persistence.EntityManager
;
import
javax.persistence.EntityManagerFactory
;
import
javax.persistence.Persistence
;
import
javax.persistence.TypedQuery
;
import
javax.swing.JOptionPane
;
import
model.Employee
;
public
class
Odczyt06_Warunek
{
public
static
void
main
(
String
[]
args
)
{
EntityManagerFactory
emf
=
null
;
EntityManager
em
=
null
;
try
{
emf
=
Persistence
.
createEntityManagerFactory
(
"hr"
);
em
=
emf
.
createEntityManager
();
String
kogoSzukam
=
JOptionPane
.
showInputDialog
(
"Podaj kod stanowiska"
);
// to działało:
// TypedQuery<Employee> query = em.createQuery("SELECT emp FROM Employee emp WHERE job.jobId = ?1", Employee.class);
// query.setParameter(1, kogoSzukam);
// ale to już nie ("legacy parameter style"):
// TypedQuery<Employee> query = em.createQuery("SELECT emp FROM Employee emp WHERE job.jobId = ?", Employee.class);
// query.setParameter(1, kogoSzukam);
// wszystkie trzy zapisy zadziałały:
// TypedQuery<Employee> query = em.createQuery("SELECT emp FROM Employee emp WHERE job_id = :kto", Employee.class);
// TypedQuery<Employee> query = em.createQuery("SELECT emp FROM Employee emp WHERE job.jobId = :kto", Employee.class);
TypedQuery
<
Employee
>
query
=
em
.
createQuery
(
"SELECT emp FROM Employee emp WHERE emp.job.jobId = :kto"
,
Employee
.
class
);
query
.
setParameter
(
"kto"
,
kogoSzukam
);
List
<
Employee
>
lista
=
query
.
getResultList
();
System
.
out
.
println
(
"Ilość rekordów "
+
lista
.
size
());
for
(
Employee
employee
:
lista
)
{
System
.
out
.
println
(
employee
.
getFirstName
()
+
" "
+
employee
.
getLastName
());
}
}
finally
{
if
(
em
!=
null
)
em
.
close
();
if
(
emf
!=
null
)
emf
.
close
();
}
}
}
PC28-HibernateGotowy/src/main/java/przyklady/Odczyt07_Skalar.java
0 → 100644
View file @
2cada9f6
package
przyklady
;
import
java.util.List
;
import
javax.persistence.EntityManager
;
import
javax.persistence.EntityManagerFactory
;
import
javax.persistence.Persistence
;
import
javax.persistence.TypedQuery
;
public
class
Odczyt07_Skalar
{
public
static
void
main
(
String
[]
args
)
{
EntityManagerFactory
emf
=
null
;
EntityManager
em
=
null
;
try
{
emf
=
Persistence
.
createEntityManagerFactory
(
"hr"
);
em
=
emf
.
createEntityManager
();
TypedQuery
<
String
>
query
=
em
.
createQuery
(
"SELECT e.lastName FROM Employee e ORDER BY 1"
,
String
.
class
);
// też działa:
// TypedQuery<String> query = em.createQuery("SELECT lastName FROM Employee ORDER BY lastName", String.class);
List
<
String
>
names
=
query
.
getResultList
();
for
(
String
name
:
names
)
{
System
.
out
.
println
(
name
);
}
}
finally
{
if
(
em
!=
null
)
em
.
close
();
if
(
emf
!=
null
)
emf
.
close
();
}
}
}
PC28-HibernateGotowy/src/main/java/przyklady/Odczyt08_Tablica.java
0 → 100644
View file @
2cada9f6
package
przyklady
;
import
java.util.Arrays
;
import
java.util.List
;
import
javax.persistence.EntityManager
;
import
javax.persistence.EntityManagerFactory
;
import
javax.persistence.Persistence
;
import
javax.persistence.TypedQuery
;
public
class
Odczyt08_Tablica
{
public
static
void
main
(
String
[]
args
)
{
EntityManagerFactory
emf
=
null
;
EntityManager
em
=
null
;
try
{
emf
=
Persistence
.
createEntityManagerFactory
(
"hr"
);
em
=
emf
.
createEntityManager
();
TypedQuery
<
Object
[]>
query
=
em
.
createQuery
(
"SELECT e.firstName, e.lastName, e.job.jobTitle, e.salary, e.department.departmentName, e.department.location.city FROM Employee e"
,
Object
[].
class
);
List
<
Object
[]>
emps
=
query
.
getResultList
();
for
(
Object
[]
emp
:
emps
)
{
System
.
out
.
println
(
Arrays
.
toString
(
emp
));
}
}
finally
{
if
(
em
!=
null
)
em
.
close
();
if
(
emf
!=
null
)
emf
.
close
();
}
}
}
PC28-HibernateGotowy/src/main/java/przyklady/Odczyt09_DTO.java
0 → 100644
View file @
2cada9f6
package
przyklady
;
import
java.util.List
;
import
javax.persistence.EntityManager
;
import
javax.persistence.EntityManagerFactory
;
import
javax.persistence.Persistence
;
import
javax.persistence.TypedQuery
;
import
dto.MiniEmp
;
public
class
Odczyt09_DTO
{
// Przykład zastosowania techniki (wzorca projektowego?...) DTO (data transfer object).
// Klasa DTO pełni rolę pomocniczą, nie stanowi głównego modelu aplikacji.
// Obiekty tej klasy służą przekazywaniu parametrów, zwracaniu wyników z metod,
// np. zbieraniu danych z formularza webowego lub - tak jak tutaj - zbieraniu wyników zapytania bazodanowego.
// Technicznie rzecz biorąc: w JPQL można użyć NEW aby stworzyć obiekt za pomocą konstruktora.
public
static
void
main
(
String
[]
args
)
{
EntityManagerFactory
emf
=
null
;
EntityManager
em
=
null
;
try
{
emf
=
Persistence
.
createEntityManagerFactory
(
"hr"
);
em
=
emf
.
createEntityManager
();
TypedQuery
<
MiniEmp
>
query
=
em
.
createQuery
(
"SELECT new dto.MiniEmp(e.firstName, e.lastName, e.job.jobTitle, e.salary) FROM Employee e"
,
MiniEmp
.
class
);
List
<
MiniEmp
>
emps
=
query
.
getResultList
();
for
(
MiniEmp
emp
:
emps
)
{
System
.
out
.
println
(
emp
);
}
}
finally
{
if
(
em
!=
null
)
em
.
close
();
if
(
emf
!=
null
)
emf
.
close
();
}
}
}
PC28-HibernateGotowy/src/main/java/przyklady/Odczyt10_Group.java
0 → 100644
View file @
2cada9f6
package
przyklady
;
import
java.util.Arrays
;
import
java.util.List
;
import
javax.persistence.EntityManager
;
import
javax.persistence.EntityManagerFactory
;
import
javax.persistence.Persistence
;
import
javax.persistence.TypedQuery
;
public
class
Odczyt10_Group
{
public
static
void
main
(
String
[]
args
)
{
EntityManagerFactory
emf
=
null
;
EntityManager
em
=
null
;
try
{
emf
=
Persistence
.
createEntityManagerFactory
(
"hr"
);
em
=
emf
.
createEntityManager
();
TypedQuery
<
Object
[]>
query
=
em
.
createQuery
(
"SELECT e.job.jobTitle, count(*), avg(e.salary) FROM Employee e GROUP BY e.job.jobTitle"
,
Object
[].
class
);
List
<
Object
[]>
emps
=
query
.
getResultList
();
for
(
Object
[]
emp
:
emps
)
{
System
.
out
.
println
(
Arrays
.
toString
(
emp
));
}
}
finally
{
if
(
em
!=
null
)
em
.
close
();
if
(
emf
!=
null
)
emf
.
close
();
}
}
}
PC28-HibernateGotowy/src/main/java/przyklady/Odczyt11_Join.java
0 → 100644
View file @
2cada9f6
package
przyklady
;
import
java.util.Arrays
;
import
java.util.List
;
import
javax.persistence.EntityManager
;
import
javax.persistence.EntityManagerFactory
;
import
javax.persistence.Persistence
;
import
javax.persistence.TypedQuery
;
public
class
Odczyt11_Join
{
// Przykład JOIN zapisanego w składni JPQL - "przechodzenie po dowiązaniach".
public
static
void
main
(
String
[]
args
)
{
EntityManagerFactory
emf
=
null
;
EntityManager
em
=
null
;
try
{
emf
=
Persistence
.
createEntityManagerFactory
(
"hr"
);
em
=
emf
.
createEntityManager
();
System
.
out
.
println
(
"Odczyt..."
);
TypedQuery
<
Object
[]>
query
=
em
.
createQuery
(
"SELECT e.firstName, e.lastName, d.departmentName, l.city"
+
" FROM Employee e LEFT JOIN e.department d LEFT JOIN d.location l"
,
Object
[].
class
);
List
<
Object
[]>
emps
=
query
.
getResultList
();
for
(
Object
[]
emp
:
emps
)
{
System
.
out
.
println
(
Arrays
.
toString
(
emp
));
}
}
finally
{
if
(
em
!=
null
)
em
.
close
();
if
(
emf
!=
null
)
emf
.
close
();
}
}
}
PC28-HibernateGotowy/src/main/java/przyklady/Odczyt12_WieleTabel.java
0 → 100644
View file @
2cada9f6
package
przyklady
;
import
java.util.List
;
import
javax.persistence.EntityManager
;
import
javax.persistence.EntityManagerFactory
;
import
javax.persistence.Persistence
;
import
javax.persistence.TypedQuery
;
import
model.Employee
;
public
class
Odczyt12_WieleTabel
{
public
static
void
main
(
String
[]
args
)
{
EntityManagerFactory
emf
=
null
;
EntityManager
em
=
null
;
try
{
emf
=
Persistence
.
createEntityManagerFactory
(
"hr"
);
em
=
emf
.
createEntityManager
();
TypedQuery
<
Object
[]>
query
=
em
.
createQuery
(
"SELECT DISTINCT emp, loc.city"
+
" FROM Employee emp, Location loc"
+
" WHERE substring(emp.lastName, 1, 1) = substring(loc.city, 1, 1)"
+
" ORDER BY emp.lastName, emp.firstName"
,
Object
[].
class
);
List
<
Object
[]>
results
=
query
.
getResultList
();
System
.
out
.
println
(
results
.
size
());
for
(
Object
[]
tab
:
results
)
{
Employee
emp
=
(
Employee
)
tab
[
0
];
String
city
=
(
String
)
tab
[
1
];
System
.
out
.
printf
(
"%-20s %-20s %-20s\n"
,
emp
.
getFirstName
(),
emp
.
getLastName
(),
city
);
}
}
finally
{
if
(
em
!=
null
)
em
.
close
();
if
(
emf
!=
null
)
emf
.
close
();
}
}
}
PC28-HibernateGotowy/src/main/java/przyklady/Odczyt13_Native.java
0 → 100644
View file @
2cada9f6
package
przyklady
;
import
java.util.List
;
import
javax.persistence.EntityManager
;
import
javax.persistence.EntityManagerFactory
;
import
javax.persistence.Persistence
;
import
javax.persistence.Query
;
// Prosty przykład native query.
// Wynik odczytywany jako lista wierszy, każdy wiersz jako Object[].
public
class
Odczyt13_Native
{
public
static
void
main
(
String
[]
args
)
{
EntityManagerFactory
emf
=
null
;
EntityManager
em
=
null
;
try
{
emf
=
Persistence
.
createEntityManagerFactory
(
"hr"
);
em
=
emf
.
createEntityManager
();
Query
query
=
em
.
createNativeQuery
(
"SELECT first_name || ' ' || last_name, job_title, salary FROM employees JOIN jobs USING(job_id) ORDER BY employee_id"
);
List
<?>
rows
=
query
.
getResultList
();
for
(
Object
row
:
rows
)
{
// na wszelki wypadek sprawdzam, ale widzę, że Object[] działa
if
(
row
instanceof
Object
[])
{
Object
[]
cols
=
(
Object
[])
row
;
System
.
out
.
println
(
cols
[
0
]
+
" pracuje jako "
+
cols
[
1
]
+
" i zarabia "
+
cols
[
2
]);
}
}
System
.
out
.
println
();
// Biorę pierwszy wiersz i (informacyjnie, dla nas podczas nauki) sprawdzam typy kolumn. Zauważmy, że kolumna salary jest mapowana na BigDecimal.
System
.
out
.
println
(
"Sprawdzę typy kolumn:"
);
Object
[]
row
=
(
Object
[])
rows
.
get
(
0
);
System
.
out
.
println
(
row
[
0
].
getClass
()
+
" "
+
row
[
1
].
getClass
()
+
" "
+
row
[
2
].
getClass
());
}
finally
{
if
(
em
!=
null
)
em
.
close
();
if
(
emf
!=
null
)
emf
.
close
();
}
}
}
PC28-HibernateGotowy/src/main/java/przyklady/Odczyt14_Native.java
0 → 100644
View file @
2cada9f6
package
przyklady
;
import
java.util.Arrays
;
import
java.util.List
;
import
javax.persistence.EntityManager
;
import
javax.persistence.EntityManagerFactory
;
import
javax.persistence.Persistence
;
import
javax.persistence.Query
;
// Sensowny przykład native query.
// Pokazuję, że dzięki niemu można skorzystać z funkcji konkretnej bazy danych.
// Tutaj (z dokumentacją przed nosem ;) ) wykombinowałem zapytanie z "funkcjami okienkowymi"
// - funkcjonalność znana głównie w Oracle, dostępna także w PostgreSQL.
public
class
Odczyt14_Native
{
public
static
void
main
(
String
[]
args
)
{
final
String
sql
=
"SELECT employee_id AS \"id\", "
+
" department_name AS \"departmentName\", "
+
" first_name AS \"firstName\", "
+
" last_name AS \"lastName\", "
+
" job_title AS \"jobTitle\", "
+
" salary AS \"salary\", "
+
" round(avg(salary) OVER (PARTITION BY department_id), 2) AS \"depAvg\", "
+
" rank() OVER (PARTITION BY department_id ORDER BY salary DESC) AS \"depPos\", "
+
" rank() OVER (ORDER BY salary DESC) AS \"globalPos\" "
+
"FROM departments d "
+
" INNER JOIN employees e USING(department_id) "
+
" INNER JOIN jobs j USING(job_id) "
+
"ORDER BY 7 DESC, 2, 6 DESC, 4 ASC, 3 ASC"
;
System
.
out
.
println
(
sql
);
EntityManagerFactory
emf
=
null
;
EntityManager
em
=
null
;
try
{
emf
=
Persistence
.
createEntityManagerFactory
(
"hr"
);
em
=
emf
.
createEntityManager
();
Query
query
=
em
.
createNativeQuery
(
sql
);
List
<?>
rows
=
query
.
getResultList
();
for
(
Object
row
:
rows
)
{
if
(
row
instanceof
Object
[])
{
Object
[]
cols
=
(
Object
[])
row
;
System
.
out
.
println
(
Arrays
.
toString
(
cols
));
}
}
}
finally
{
if
(
em
!=
null
)
em
.
close
();
if
(
emf
!=
null
)
emf
.
close
();
}
}
}
PC28-HibernateGotowy/src/main/java/przyklady/Odczyt15_Native_DTO.java
0 → 100644
View file @
2cada9f6
package
przyklady
;
import
java.util.List
;
import
javax.persistence.EntityManager
;
import
javax.persistence.EntityManagerFactory
;
import
javax.persistence.Persistence
;
import
javax.persistence.Query
;
import
dto.PracownikWDepartamencie
;
// Sensowny przykład native query.
// Pokazuję, że dzięki niemu można skorzystać z funkcji konkretnej bazy danych.
// Tutaj (z dokumentacją przed nosem ;) ) wykombinowałem zapytanie z "funkcjami okienkowymi"
// - funkcjonalność znana głównie w Oracle, dostępna także w PostgreSQL.
// W tej wersji dodatkowo wyniki pakuję do własnej klasy PracownikWDepartamencie
public
class
Odczyt15_Native_DTO
{
public
static
void
main
(
String
[]
args
)
{
final
String
sql
=
"SELECT employee_id AS \"id\", "
+
" department_name AS \"departmentName\", "
+
" first_name AS \"firstName\", "
+
" last_name AS \"lastName\", "
+
" job_title AS \"jobTitle\", "
+
" salary AS \"salary\", "
+
" round(avg(salary) OVER (PARTITION BY department_id), 2) AS \"depAvg\", "
+
" rank() OVER (PARTITION BY department_id ORDER BY salary DESC) AS \"depPos\", "
+
" rank() OVER (ORDER BY salary DESC) AS \"globalPos\" "
+
"FROM departments d "
+
" INNER JOIN employees e USING(department_id) "
+
" INNER JOIN jobs j USING(job_id) "
+
"ORDER BY 7 DESC, 2, 6 DESC, 4 ASC, 3 ASC"
;
System
.
out
.
println
(
sql
);
EntityManagerFactory
emf
=
null
;
EntityManager
em
=
null
;
try
{
emf
=
Persistence
.
createEntityManagerFactory
(
"hr"
);
em
=
emf
.
createEntityManager
();
Query
query
=
em
.
createNativeQuery
(
sql
,
PracownikWDepartamencie
.
class
);
List
<?>
rows
=
query
.
getResultList
();
System
.
out
.
println
(
PracownikWDepartamencie
.
naglowek
());
for
(
Object
row
:
rows
)
{
//System.out.println(row);
PracownikWDepartamencie
p
=
(
PracownikWDepartamencie
)
row
;
System
.
out
.
println
(
p
.
dajInfo
());
}
}
finally
{
if
(
em
!=
null
)
em
.
close
();
if
(
emf
!=
null
)
emf
.
close
();
}
}
}
PC28-HibernateGotowy/src/main/java/przyklady/Odczyt16_Native_Entity.java
0 → 100644
View file @
2cada9f6
package
przyklady
;
import
java.util.List
;
import
javax.persistence.EntityManager
;
import
javax.persistence.EntityManagerFactory
;
import
javax.persistence.Persistence
;
import
javax.persistence.Query
;
import
model.Country
;
// Wracając do prostszych rzeczy: Wynik native query można bez problemu odczytać jako normalną encję.
public
class
Odczyt16_Native_Entity
{
public
static
void
main
(
String
[]
args
)
{
final
String
sql
=
"SELECT * FROM countries"
;
EntityManagerFactory
emf
=
null
;
EntityManager
em
=
null
;
try
{
emf
=
Persistence
.
createEntityManagerFactory
(
"hr"
);
em
=
emf
.
createEntityManager
();
Query
query
=
em
.
createNativeQuery
(
sql
,
Country
.
class
);
List
<
Country
>
rows
=
(
List
<
Country
>)
query
.
getResultList
();
for
(
Country
country
:
rows
)
{
System
.
out
.
println
(
country
.
getCountryId
()
+
" "
+
country
.
getCountryName
());
}
}
finally
{
if
(
em
!=
null
)
em
.
close
();
if
(
emf
!=
null
)
emf
.
close
();
}
}
}
PC28-HibernateGotowy/src/main/java/przyklady/OdczytajManagerow.java
0 → 100644
View file @
2cada9f6
package
przyklady
;
import
java.util.Scanner
;
import
javax.persistence.EntityManager
;
import
javax.persistence.EntityManagerFactory
;
import
javax.persistence.Persistence
;
import
model.Employee
;
public
class
OdczytajManagerow
{
public
static
void
main
(
String
[]
args
)
{
Scanner
sc
=
new
Scanner
(
System
.
in
);
EntityManagerFactory
emf
=
null
;
EntityManager
em
=
null
;
try
{
emf
=
Persistence
.
createEntityManagerFactory
(
"hr"
);
em
=
emf
.
createEntityManager
();
while
(
true
)
{
System
.
out
.
print
(
"\nPodaj id pracownika: "
);
if
(!
sc
.
hasNextInt
())
break
;
int
id
=
sc
.
nextInt
();
Employee
emp
=
em
.
find
(
Employee
.
class
,
id
);
if
(
emp
==
null
)
{
System
.
out
.
println
(
"nie znaleziono takiego pracownika"
);
continue
;
}
while
(
emp
!=
null
)
{
System
.
out
.
println
(
" "
+
emp
.
getEmployeeId
()
+
" "
+
emp
.
getFirstName
()
+
" "
+
emp
.
getLastName
()
+
"("
+
emp
.
getJob
().
getJobTitle
()
+
")"
+
" $"
+
emp
.
getSalary
());
emp
=
emp
.
getManager
();
System
.
out
.
println
(
"Managerem jest:"
);
}
}
}
finally
{
if
(
em
!=
null
)
em
.
close
();
if
(
emf
!=
null
)
emf
.
close
();
}
}
}
PC28-HibernateGotowy/src/main/resources/.gitkeep
0 → 100644
View file @
2cada9f6
PC28-HibernateGotowy/src/main/resources/META-INF/persistence.xml
0 → 100644
View file @
2cada9f6
<?xml version="1.0" encoding="UTF-8"?>
<persistence
version=
"2.2"
xmlns=
"http://xmlns.jcp.org/xml/ns/persistence"
xmlns:xsi=
"http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation=
"http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_2.xsd"
>
<persistence-unit
name=
"hr"
transaction-type=
"RESOURCE_LOCAL"
>
<class>
model.Country
</class>
<class>
model.Department
</class>
<class>
model.Employee
</class>
<class>
model.Job
</class>
<class>
model.Location
</class>
<class>
model.Region
</class>
<class>
dto.PracownikWDepartamencie
</class>
<properties>
<property
name=
"javax.persistence.jdbc.url"
value=
"jdbc:postgresql://localhost:5432/hr"
/>
<property
name=
"javax.persistence.jdbc.user"
value=
"kurs"
/>
<property
name=
"javax.persistence.jdbc.password"
value=
"abc123"
/>
<property
name=
"javax.persistence.jdbc.driver"
value=
"org.postgresql.Driver"
/>
<property
name=
"hibernate.show_sql"
value=
"true"
/>
</properties>
</persistence-unit>
</persistence>
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