Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
J
javab_20230928
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
javab_20230928
Commits
2d4f23a4
Commit
2d4f23a4
authored
Oct 20, 2023
by
Patryk Czarnik
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Dodatkowe przykłady HibernateSklep
parent
d2278dcc
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
256 additions
and
0 deletions
+256
-0
Parametry.java
...te/src/main/java/sklep/przyklady_hibernate/Parametry.java
+38
-0
ReferencjeMiedzyObiektami.java
.../sklep/przyklady_hibernate/ReferencjeMiedzyObiektami.java
+67
-0
Zapis_Insert.java
...src/main/java/sklep/przyklady_hibernate/Zapis_Insert.java
+74
-0
Zapis_Update.java
...src/main/java/sklep/przyklady_hibernate/Zapis_Update.java
+77
-0
No files found.
PC27-SklepHibernate/src/main/java/sklep/przyklady_hibernate/Parametry.java
0 → 100644
View file @
2d4f23a4
package
sklep
.
przyklady_hibernate
;
import
java.util.List
;
import
jakarta.persistence.EntityManager
;
import
jakarta.persistence.EntityManagerFactory
;
import
jakarta.persistence.Persistence
;
import
jakarta.persistence.TypedQuery
;
import
sklep.model.Product
;
public
class
Parametry
{
public
static
void
main
(
String
[]
args
)
{
try
(
EntityManagerFactory
emf
=
Persistence
.
createEntityManagerFactory
(
"sklep"
);
EntityManager
em
=
emf
.
createEntityManager
())
{
int
min
=
1000
;
int
max
=
3000
;
String
jpql
=
"""
SELECT p FROM Product p
WHERE p.price BETWEEN :minPrice AND :maxPrice
ORDER BY p.price DESC
"""
;
TypedQuery
<
Product
>
query
=
em
.
createQuery
(
jpql
,
Product
.
class
);
query
.
setParameter
(
"minPrice"
,
min
);
query
.
setParameter
(
"maxPrice"
,
max
);
List
<
Product
>
products
=
query
.
getResultList
();
System
.
out
.
printf
(
"Mam %d produktów:\n"
,
products
.
size
());
for
(
Product
product
:
products
)
{
System
.
out
.
printf
(
"%s za cenę %s\n"
,
product
.
getProductName
(),
product
.
getPrice
());
}
}
}
}
PC27-SklepHibernate/src/main/java/sklep/przyklady_hibernate/ReferencjeMiedzyObiektami.java
0 → 100644
View file @
2d4f23a4
package
sklep
.
przyklady_hibernate
;
import
java.util.List
;
import
java.util.Scanner
;
import
jakarta.persistence.*
;
import
sklep.model.Customer
;
import
sklep.model.Order
;
import
sklep.model.OrderProduct
;
public
class
ReferencjeMiedzyObiektami
{
// Ten przykład pokazuje, że z jednego obiektu wczytanego z bazy można zwykłymi metodami get???
// przejść do obiektów zapisanych w innych tabelach.
// Nie wymaga to pisania dodatkowych zapytań.
// Zwykłe obiekty dostępne poprzez gettery, np. op.getProduct(), są wczytywane od razu i są obecne w pamięci,
// natomiast kolekcje, np. customer.getOrders(), są uzupełniane przy pierwszym odczycie (ustawienie LAZY, które jest domyślne).
public
static
void
main
(
String
[]
args
)
{
EntityManagerFactory
emf
=
null
;
EntityManager
em
=
null
;
try
{
Scanner
scanner
=
new
Scanner
(
System
.
in
);
emf
=
Persistence
.
createEntityManagerFactory
(
"sklep"
);
em
=
emf
.
createEntityManager
();
while
(
true
)
{
System
.
out
.
print
(
"Podaj email klienta: "
);
String
email
=
scanner
.
nextLine
();
if
(
email
.
isEmpty
())
{
break
;
}
Customer
customer
=
em
.
find
(
Customer
.
class
,
email
);
if
(
customer
==
null
)
{
System
.
out
.
println
(
"Nie ma takiego klienta"
);
continue
;
}
System
.
out
.
println
(
"Znaleziono obiekt: "
+
customer
);
System
.
out
.
println
(
" * imię/nazwa: "
+
customer
.
getCustomerName
());
System
.
out
.
println
(
" * adres: "
+
customer
.
getAddress
()
+
", "
+
customer
.
getCity
());
System
.
out
.
println
(
" * nr tel: "
+
customer
.
getPhoneNumber
());
List
<
Order
>
orders
=
customer
.
getOrders
();
// tu widać leniwe wypełnianie listy danymi z bazy
System
.
out
.
println
(
"Mam już obiekt listy klasy: "
+
orders
.
getClass
().
getName
());
// System.out.println("Klient posiada " + orders.size() + " zamówień");
for
(
Order
order
:
orders
)
{
System
.
out
.
println
(
"Zamówienie nr "
+
order
.
getOrderId
()
+
" z dnia "
+
order
.
getOrderDate
());
for
(
OrderProduct
op
:
order
.
getOrderProducts
())
{
System
.
out
.
println
(
" + "
+
op
.
getProduct
().
getProductName
()
+
", "
+
op
.
getQuantity
()
+
" sztuk po "
+
op
.
getActualPrice
()
+
" zł"
);
}
}
System
.
out
.
println
();
}
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
}
finally
{
if
(
em
!=
null
)
em
.
close
();
if
(
emf
!=
null
)
emf
.
close
();
}
}
}
PC27-SklepHibernate/src/main/java/sklep/przyklady_hibernate/Zapis_Insert.java
0 → 100644
View file @
2d4f23a4
package
sklep
.
przyklady_hibernate
;
import
java.math.BigDecimal
;
import
java.util.Locale
;
import
java.util.Scanner
;
import
jakarta.persistence.*
;
import
sklep.model.Product
;
public
class
Zapis_Insert
{
public
static
void
main
(
String
[]
args
)
{
Scanner
scanner
=
new
Scanner
(
System
.
in
);
scanner
.
useLocale
(
Locale
.
US
);
try
(
EntityManagerFactory
emf
=
Persistence
.
createEntityManagerFactory
(
"sklep"
);
EntityManager
em
=
emf
.
createEntityManager
())
{
System
.
out
.
println
(
"Początek transakcji"
);
em
.
getTransaction
().
begin
();
while
(
true
)
{
System
.
out
.
print
(
"Podaj nazwę nowego produktu (pusty napis, aby zakończyć): "
);
String
name
=
scanner
.
nextLine
();
if
(
name
.
isEmpty
())
break
;
System
.
out
.
print
(
"Podaj cenę: "
);
BigDecimal
price
=
scanner
.
nextBigDecimal
();
scanner
.
nextLine
();
System
.
out
.
print
(
"Podaj stawkę VAT, np 23 : "
);
int
vat
=
scanner
.
nextInt
();
scanner
.
nextLine
();
System
.
out
.
print
(
"Podaj opis: "
);
String
description
=
scanner
.
nextLine
();
if
(
description
.
isEmpty
())
{
description
=
null
;
}
// aby wpisać rekord do bazy, tworzymy obiekt bez określonego ID
Product
product
=
new
Product
();
product
.
setProductName
(
name
);
product
.
setPrice
(
price
);
product
.
setVat
(
BigDecimal
.
valueOf
(
vat
).
movePointLeft
(
2
));
product
.
setDescription
(
description
);
System
.
out
.
printf
(
"Produkt przed persist: id: %s, name: %s, price: %s, vat: %s, desc: %s\n"
,
product
.
getProductId
(),
product
.
getProductName
(),
product
.
getPrice
(),
product
.
getVat
(),
product
.
getDescription
());
// Dodanie obiektu do puli obiektów zarządzanych i wykonanie INSERT, ale jeszcze bez COMMIT
em
.
persist
(
product
);
System
.
out
.
printf
(
"Produkt po persist: id: %s, name: %s, price: %s, vat: %s, desc: %s\n"
,
product
.
getProductId
(),
product
.
getProductName
(),
product
.
getPrice
(),
product
.
getVat
(),
product
.
getDescription
());
System
.
out
.
println
();
}
System
.
out
.
println
(
"Czy zapisać zmiany? [T/N]"
);
String
wybor
=
scanner
.
next
().
toUpperCase
();
switch
(
wybor
)
{
case
"T"
:
System
.
out
.
println
(
"Zatwierdzam transakcję"
);
em
.
getTransaction
().
commit
();
break
;
case
"N"
:
System
.
out
.
println
(
"Cofam transakcję"
);
em
.
getTransaction
().
rollback
();
break
;
default
:
System
.
out
.
println
(
"Rozłączam się bez zatwierdzenia transakcji"
);
}
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
}
}
}
PC27-SklepHibernate/src/main/java/sklep/przyklady_hibernate/Zapis_Update.java
0 → 100644
View file @
2d4f23a4
package
sklep
.
przyklady_hibernate
;
import
java.math.BigDecimal
;
import
java.util.Scanner
;
import
jakarta.persistence.*
;
import
sklep.model.Product
;
public
class
Zapis_Update
{
public
static
void
main
(
String
[]
args
)
{
try
(
EntityManagerFactory
emf
=
Persistence
.
createEntityManagerFactory
(
"sklep"
);
EntityManager
em
=
emf
.
createEntityManager
())
{
Scanner
sc
=
new
Scanner
(
System
.
in
);
System
.
out
.
println
(
"Rozpoczynam transakcję"
);
EntityTransaction
transakcja
=
em
.
getTransaction
();
transakcja
.
begin
();
System
.
out
.
println
(
"Aby zakończyć, podaj liczbę 0"
);
while
(
true
)
{
System
.
out
.
print
(
"\nPodaj id produktu: "
);
int
id
=
sc
.
nextInt
();
sc
.
nextLine
();
if
(
id
==
0
)
break
;
Product
product
=
em
.
find
(
Product
.
class
,
id
);
if
(
product
==
null
)
{
System
.
out
.
println
(
"Nie ma produktu o id "
+
id
);
continue
;
}
System
.
out
.
println
(
"Odczytany produkt: "
+
product
);
System
.
out
.
println
(
product
.
getProductName
()
+
" za cenę "
+
product
.
getPrice
()
+
", opis: "
+
product
.
getDescription
());
// Umożliwiamy wprowadzenie zmian i te zmiany są nakładane za pomocą setterów bezpośrednio na obiekcie encji.
System
.
out
.
println
(
"Podaj zmianę ceny: "
);
BigDecimal
zmiana
=
sc
.
nextBigDecimal
();
sc
.
nextLine
();
if
(
zmiana
.
compareTo
(
BigDecimal
.
ZERO
)
!=
0
)
{
product
.
setPrice
(
product
.
getPrice
().
add
(
zmiana
));
}
System
.
out
.
println
(
"Podaj nową nazwę (enter, aby nie zmieniać): "
);
String
nazwa
=
sc
.
nextLine
();
if
(!
nazwa
.
isEmpty
())
{
product
.
setProductName
(
nazwa
);
}
System
.
out
.
println
(
"Podaj nowy opis (enter, aby nie zmieniać): "
);
String
opis
=
sc
.
nextLine
();
if
(!
opis
.
isEmpty
())
{
product
.
setDescription
(
opis
);
}
}
// Do tej pory zmiany zostały dokonane w obiektach znajdujących się w pamięci.
// Aby zapisać zmiany w bazie danych, należy zatwierdzić transakcję (i tylko tyle).
// To zapisze wszystkie "obiekty zarządzane" przez EntityManagera.
System
.
out
.
println
(
"Czy zapisać zmiany? [T/N] "
);
String
taknie
=
sc
.
nextLine
().
trim
().
toUpperCase
();
switch
(
taknie
)
{
case
"T"
,
"Y"
->
{
transakcja
.
commit
();
System
.
out
.
println
(
"Zmiany zatwierdzone"
);
}
case
"N"
->
{
transakcja
.
rollback
();
System
.
out
.
println
(
"Zmiany wycofane"
);
}
// w pozostałych przypadkach nie ma ani commit, ani rollback → zmiany nie zostaną zapisane
}
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
}
}
}
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