Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
2
20230403
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
20230403
Commits
fea23d7c
Commit
fea23d7c
authored
Apr 25, 2023
by
Patryk Czarnik
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Przykłady na obiektach
parent
3dea2f66
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
241 additions
and
0 deletions
+241
-0
ReferencjeMiedzyObiektami.java
...te/src/main/java/przyklady/ReferencjeMiedzyObiektami.java
+69
-0
Zapis_Insert.java
PC27-Hibernate/src/main/java/przyklady/Zapis_Insert.java
+84
-0
Zapis_Update.java
PC27-Hibernate/src/main/java/przyklady/Zapis_Update.java
+88
-0
No files found.
PC27-Hibernate/src/main/java/przyklady/ReferencjeMiedzyObiektami.java
0 → 100644
View file @
fea23d7c
package
przyklady
;
import
java.util.List
;
import
java.util.Scanner
;
import
javax.persistence.EntityManager
;
import
javax.persistence.EntityManagerFactory
;
import
javax.persistence.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-Hibernate/src/main/java/przyklady/Zapis_Insert.java
0 → 100644
View file @
fea23d7c
package
przyklady
;
import
java.math.BigDecimal
;
import
java.util.Locale
;
import
java.util.Scanner
;
import
javax.persistence.EntityManager
;
import
javax.persistence.EntityManagerFactory
;
import
javax.persistence.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
);
EntityManagerFactory
emf
=
null
;
EntityManager
em
=
null
;
try
{
emf
=
Persistence
.
createEntityManagerFactory
(
"sklep"
);
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
();
}
finally
{
if
(
em
!=
null
)
em
.
close
();
if
(
emf
!=
null
)
emf
.
close
();
}
}
}
PC27-Hibernate/src/main/java/przyklady/Zapis_Update.java
0 → 100644
View file @
fea23d7c
package
przyklady
;
import
java.math.BigDecimal
;
import
java.util.Scanner
;
import
javax.persistence.EntityManager
;
import
javax.persistence.EntityManagerFactory
;
import
javax.persistence.EntityTransaction
;
import
javax.persistence.Persistence
;
import
sklep.model.Product
;
public
class
Zapis_Update
{
public
static
void
main
(
String
[]
args
)
{
EntityManagerFactory
emf
=
null
;
EntityManager
em
=
null
;
try
{
Scanner
sc
=
new
Scanner
(
System
.
in
);
emf
=
Persistence
.
createEntityManagerFactory
(
"sklep"
);
em
=
emf
.
createEntityManager
();
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
();
}
finally
{
if
(
em
!=
null
)
em
.
close
();
if
(
emf
!=
null
)
emf
.
close
();
}
}
}
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