Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
J
java_dzienna_15_09
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_dzienna_15_09
Commits
ea24d192
Commit
ea24d192
authored
Sep 30, 2022
by
Patryk Czarnik
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Przykłady z referencjami Hibernate
parent
029f5311
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
95 additions
and
20 deletions
+95
-20
persistence.xml
PC27-Hibernate/src/main/java/META-INF/persistence.xml
+1
-0
OdczytajJedenProdukt2.java
.../src/main/java/sklep/przyklady/OdczytajJedenProdukt2.java
+27
-20
ReferencjeMiedzyObiektami.java
.../main/java/sklep/przyklady/ReferencjeMiedzyObiektami.java
+67
-0
No files found.
PC27-Hibernate/src/main/java/META-INF/persistence.xml
View file @
ea24d192
...
...
@@ -11,6 +11,7 @@
<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>
PC27-Hibernate/src/main/java/sklep/przyklady/OdczytajJedenProdukt2.java
View file @
ea24d192
...
...
@@ -11,27 +11,34 @@ import sklep.model.Product;
public
class
OdczytajJedenProdukt2
{
public
static
void
main
(
String
[]
args
)
{
Scanner
sc
=
new
Scanner
(
System
.
in
);
EntityManagerFactory
emf
=
Persistence
.
createEntityManagerFactory
(
"sklep"
);
EntityManager
em
=
emf
.
createEntityManager
();
System
.
out
.
println
(
"Aby zakończyć, podaj liczbę 0"
);
while
(
true
)
{
System
.
out
.
print
(
"\nPodaj id produktu: "
);
int
id
=
sc
.
nextInt
();
if
(
id
==
0
)
break
;
Product
product
=
em
.
find
(
Product
.
class
,
id
);
if
(
product
!=
null
)
{
System
.
out
.
println
(
"Odczytany produkt: "
+
product
);
System
.
out
.
println
(
product
.
getProductName
()
+
" za cenę "
+
product
.
getPrice
()
+
", opis: "
+
product
.
getDescription
());
}
else
{
System
.
out
.
println
(
"Nie ma produktu o id "
+
id
);
EntityManagerFactory
emf
=
null
;
EntityManager
em
=
null
;
try
{
Scanner
sc
=
new
Scanner
(
System
.
in
);
emf
=
Persistence
.
createEntityManagerFactory
(
"sklep"
);
em
=
emf
.
createEntityManager
();
System
.
out
.
println
(
"Aby zakończyć, podaj liczbę 0"
);
while
(
true
)
{
System
.
out
.
print
(
"\nPodaj id produktu: "
);
int
id
=
sc
.
nextInt
();
if
(
id
==
0
)
break
;
Product
product
=
em
.
find
(
Product
.
class
,
id
);
if
(
product
!=
null
)
{
System
.
out
.
println
(
"Odczytany produkt: "
+
product
);
System
.
out
.
println
(
product
.
getProductName
()
+
" za cenę "
+
product
.
getPrice
()
+
", opis: "
+
product
.
getDescription
());
}
else
{
System
.
out
.
println
(
"Nie ma produktu o id "
+
id
);
}
}
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
}
finally
{
if
(
em
!=
null
)
em
.
close
();
if
(
emf
!=
null
)
emf
.
close
();
}
em
.
close
();
emf
.
close
();
}
}
PC27-Hibernate/src/main/java/sklep/przyklady/ReferencjeMiedzyObiektami.java
0 → 100644
View file @
ea24d192
package
sklep
.
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
();
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
();
}
}
}
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