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
3dea2f66
Commit
3dea2f66
authored
Apr 25, 2023
by
Patryk Czarnik
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Przykłady odczytu pojedynczych rekordów
parent
de373991
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
78 additions
and
3 deletions
+78
-3
persistence.xml
PC27-Hibernate/src/main/java/META-INF/persistence.xml
+8
-1
Odczyt1_JedenProdukt.java
...bernate/src/main/java/przyklady/Odczyt1_JedenProdukt.java
+23
-0
Odczyt2_Petla.java
PC27-Hibernate/src/main/java/przyklady/Odczyt2_Petla.java
+45
-0
OrderProduct.java
PC27-Hibernate/src/main/java/sklep/model/OrderProduct.java
+2
-2
No files found.
PC27-Hibernate/src/main/java/META-INF/persistence.xml
View file @
3dea2f66
<?xml version="1.0" encoding="UTF-8"?>
<?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
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=
"
PC27-Hibernate
"
>
<persistence-unit
name=
"
sklep"
transaction-type=
"RESOURCE_LOCAL
"
>
<class>
sklep.model.Customer
</class>
<class>
sklep.model.Customer
</class>
<class>
sklep.model.OrderProduct
</class>
<class>
sklep.model.OrderProduct
</class>
<class>
sklep.model.OrderProductPK
</class>
<class>
sklep.model.OrderProductPK
</class>
<class>
sklep.model.Order
</class>
<class>
sklep.model.Order
</class>
<class>
sklep.model.Product
</class>
<class>
sklep.model.Product
</class>
<properties>
<property
name=
"javax.persistence.jdbc.url"
value=
"jdbc:postgresql://localhost:5432/sklep"
/>
<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-unit>
</persistence>
</persistence>
PC27-Hibernate/src/main/java/przyklady/Odczyt1_JedenProdukt.java
0 → 100644
View file @
3dea2f66
package
przyklady
;
import
javax.persistence.EntityManager
;
import
javax.persistence.EntityManagerFactory
;
import
javax.persistence.Persistence
;
import
sklep.model.Product
;
public
class
Odczyt1_JedenProdukt
{
public
static
void
main
(
String
[]
args
)
{
EntityManagerFactory
emf
=
Persistence
.
createEntityManagerFactory
(
"sklep"
);
EntityManager
em
=
emf
.
createEntityManager
();
System
.
out
.
println
(
"Udało się połączyć, em = "
+
em
);
Product
product
=
em
.
find
(
Product
.
class
,
1
);
System
.
out
.
println
(
"Odczytany produkt: "
+
product
);
System
.
out
.
println
(
product
.
getProductName
()
+
" za cenę "
+
product
.
getPrice
());
em
.
close
();
emf
.
close
();
}
}
PC27-Hibernate/src/main/java/przyklady/Odczyt2_Petla.java
0 → 100644
View file @
3dea2f66
package
przyklady
;
import
java.util.Scanner
;
import
javax.persistence.EntityManager
;
import
javax.persistence.EntityManagerFactory
;
import
javax.persistence.Persistence
;
import
sklep.model.Product
;
public
class
Odczyt2_Petla
{
public
static
void
main
(
String
[]
args
)
{
Scanner
scanner
=
null
;
EntityManagerFactory
emf
=
null
;
EntityManager
em
=
null
;
try
{
scanner
=
new
Scanner
(
System
.
in
);
emf
=
Persistence
.
createEntityManagerFactory
(
"sklep"
);
em
=
emf
.
createEntityManager
();
while
(
true
)
{
System
.
out
.
print
(
"\nPodaj id produktu: "
);
int
productId
=
scanner
.
nextInt
();
if
(
productId
==
0
)
break
;
Product
product
=
em
.
find
(
Product
.
class
,
productId
);
if
(
product
==
null
)
{
System
.
out
.
println
(
"Nie ma takiego produktu"
);
continue
;
}
System
.
out
.
println
(
"Odczytany produkt: "
+
product
);
System
.
out
.
println
(
product
.
getProductName
()
+
" za cenę "
+
product
.
getPrice
());
if
(
product
.
getDescription
()
!=
null
)
{
System
.
out
.
println
(
product
.
getDescription
());
}
}
}
finally
{
if
(
em
!=
null
)
em
.
close
();
if
(
emf
!=
null
)
emf
.
close
();
if
(
scanner
!=
null
)
scanner
.
close
();
}
}
}
PC27-Hibernate/src/main/java/sklep/model/OrderProduct.java
View file @
3dea2f66
...
@@ -28,12 +28,12 @@ public class OrderProduct extends WspolnaNadklasa implements Serializable {
...
@@ -28,12 +28,12 @@ public class OrderProduct extends WspolnaNadklasa implements Serializable {
//bi-directional many-to-one association to Order
//bi-directional many-to-one association to Order
@ManyToOne
@ManyToOne
@JoinColumn
(
name
=
"order_id"
)
@JoinColumn
(
name
=
"order_id"
,
insertable
=
false
,
updatable
=
false
)
private
Order
order
;
private
Order
order
;
//uni-directional many-to-one association to Product
//uni-directional many-to-one association to Product
@ManyToOne
@ManyToOne
@JoinColumn
(
name
=
"product_id"
)
@JoinColumn
(
name
=
"product_id"
,
insertable
=
false
,
updatable
=
false
)
private
Product
product
;
private
Product
product
;
public
OrderProduct
()
{
public
OrderProduct
()
{
...
...
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