Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
2
20240528-BJava
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
20240528-BJava
Commits
b278a4fa
Commit
b278a4fa
authored
Jun 25, 2024
by
Patryk Czarnik
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
ostatnie przykłady sklep hibernate
parent
76b32b1e
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
187 additions
and
0 deletions
+187
-0
P06_Parametry.java
...Hibernate/src/main/java/sklep/programy/P06_Parametry.java
+37
-0
P07_Zapis_Update.java
...ernate/src/main/java/sklep/programy/P07_Zapis_Update.java
+77
-0
P08_Zapis_Insert.java
...ernate/src/main/java/sklep/programy/P08_Zapis_Insert.java
+73
-0
No files found.
PC30-Hibernate/src/main/java/sklep/programy/P06_Parametry.java
0 → 100644
View file @
b278a4fa
package
sklep
.
programy
;
import
java.math.BigDecimal
;
import
java.util.List
;
import
java.util.Scanner
;
import
jakarta.persistence.*
;
import
sklep.model.Product
;
public
class
P06_Parametry
{
public
static
void
main
(
String
[]
args
)
{
try
(
EntityManagerFactory
emf
=
Persistence
.
createEntityManagerFactory
(
"sklep"
);
EntityManager
em
=
emf
.
createEntityManager
();
Scanner
scanner
=
new
Scanner
(
System
.
in
))
{
System
.
out
.
print
(
"Podaj cenę minimalną: "
);
BigDecimal
min
=
scanner
.
nextBigDecimal
();
System
.
out
.
print
(
"Podaj cenę maksymalną: "
);
BigDecimal
max
=
scanner
.
nextBigDecimal
();
TypedQuery
<
Product
>
query
=
em
.
createQuery
(
"SELECT p FROM Product p WHERE p.price BETWEEN :minimum AND :maximum ORDER BY p.price DESC"
,
Product
.
class
);
query
.
setParameter
(
"minimum"
,
min
);
query
.
setParameter
(
"maximum"
,
max
);
List
<
Product
>
products
=
query
.
getResultList
();
for
(
Product
product
:
products
)
{
// System.out.println(product);
System
.
out
.
println
(
product
.
getProductName
()
+
" za cenę "
+
product
.
getPrice
());
}
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
}
}
}
PC30-Hibernate/src/main/java/sklep/programy/P07_Zapis_Update.java
0 → 100644
View file @
b278a4fa
package
sklep
.
programy
;
import
java.math.BigDecimal
;
import
java.util.Scanner
;
import
jakarta.persistence.*
;
import
sklep.model.Product
;
public
class
P07_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
();
}
}
}
PC30-Hibernate/src/main/java/sklep/programy/P08_Zapis_Insert.java
0 → 100644
View file @
b278a4fa
package
sklep
.
programy
;
import
java.math.BigDecimal
;
import
java.util.Locale
;
import
java.util.Scanner
;
import
jakarta.persistence.*
;
import
sklep.model.Product
;
public
class
P08_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
.
getId
(),
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
.
getId
(),
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
();
}
}
}
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