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
cd2af3f4
Commit
cd2af3f4
authored
Jun 25, 2024
by
Patryk Czarnik
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Poprawka konfiguracji i pierwszy program Hibernate/JPA
parent
0d81e9d4
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
60 additions
and
6 deletions
+60
-6
Customer.java
PC30-Hibernate/src/main/java/sklep/model/Customer.java
+1
-1
Order.java
PC30-Hibernate/src/main/java/sklep/model/Order.java
+6
-5
P01_JedenProdukt.java
...ernate/src/main/java/sklep/programy/P01_JedenProdukt.java
+34
-0
persistence.xml
PC30-Hibernate/src/main/resources/META-INF/persistence.xml
+19
-0
No files found.
PC30-Hibernate/src/main/java/sklep/model/Customer.java
View file @
cd2af3f4
...
@@ -27,7 +27,7 @@ public class Customer {
...
@@ -27,7 +27,7 @@ public class Customer {
@Column
(
name
=
"city"
,
length
=
100
)
@Column
(
name
=
"city"
,
length
=
100
)
private
String
city
;
private
String
city
;
@OneToMany
(
mappedBy
=
"customer
Email
"
)
@OneToMany
(
mappedBy
=
"customer"
)
private
Set
<
Order
>
orders
=
new
LinkedHashSet
<>();
private
Set
<
Order
>
orders
=
new
LinkedHashSet
<>();
public
String
getCustomerEmail
()
{
public
String
getCustomerEmail
()
{
...
...
PC30-Hibernate/src/main/java/sklep/model/Order.java
View file @
cd2af3f4
...
@@ -19,7 +19,7 @@ public class Order {
...
@@ -19,7 +19,7 @@ public class Order {
@ManyToOne
(
fetch
=
FetchType
.
LAZY
,
optional
=
false
)
@ManyToOne
(
fetch
=
FetchType
.
LAZY
,
optional
=
false
)
@JoinColumn
(
name
=
"customer_email"
,
nullable
=
false
)
@JoinColumn
(
name
=
"customer_email"
,
nullable
=
false
)
private
Customer
customer
Email
;
private
Customer
customer
;
@ColumnDefault
(
"CURRENT_TIMESTAMP"
)
@ColumnDefault
(
"CURRENT_TIMESTAMP"
)
@Column
(
name
=
"order_date"
,
nullable
=
false
)
@Column
(
name
=
"order_date"
,
nullable
=
false
)
...
@@ -27,6 +27,7 @@ public class Order {
...
@@ -27,6 +27,7 @@ public class Order {
@Column
(
name
=
"delivery_date"
)
@Column
(
name
=
"delivery_date"
)
private
LocalDate
deliveryDate
;
private
LocalDate
deliveryDate
;
@OneToMany
(
mappedBy
=
"order"
)
@OneToMany
(
mappedBy
=
"order"
)
private
Set
<
OrderProduct
>
orderProducts
=
new
LinkedHashSet
<>();
private
Set
<
OrderProduct
>
orderProducts
=
new
LinkedHashSet
<>();
...
@@ -38,12 +39,12 @@ public class Order {
...
@@ -38,12 +39,12 @@ public class Order {
this
.
id
=
id
;
this
.
id
=
id
;
}
}
public
Customer
getCustomer
Email
()
{
public
Customer
getCustomer
()
{
return
customer
Email
;
return
customer
;
}
}
public
void
setCustomer
Email
(
Customer
customerEmail
)
{
public
void
setCustomer
(
Customer
customerEmail
)
{
this
.
customer
Email
=
customerEmail
;
this
.
customer
=
customerEmail
;
}
}
public
Instant
getOrderDate
()
{
public
Instant
getOrderDate
()
{
...
...
PC30-Hibernate/src/main/java/sklep/programy/P01_JedenProdukt.java
0 → 100644
View file @
cd2af3f4
package
sklep
.
programy
;
import
jakarta.persistence.EntityManager
;
import
jakarta.persistence.EntityManagerFactory
;
import
jakarta.persistence.Persistence
;
import
sklep.model.Product
;
import
javax.swing.*
;
public
class
P01_JedenProdukt
{
public
static
void
main
(
String
[]
args
)
{
System
.
out
.
println
(
"Początek programu"
);
EntityManagerFactory
emf
=
Persistence
.
createEntityManagerFactory
(
"sklep_pu"
);
System
.
out
.
println
(
"Mam factory: "
+
emf
);
EntityManager
em
=
emf
.
createEntityManager
();
System
.
out
.
println
(
"Mam em: "
+
em
);
Integer
id
=
Integer
.
valueOf
(
JOptionPane
.
showInputDialog
(
"Podaj id produktu"
));
Product
product
=
em
.
find
(
Product
.
class
,
id
);
// w razie braku takiego rekordu, wynikiem jest null
if
(
product
==
null
)
{
System
.
out
.
println
(
"Nie ma produktu o numerze "
+
id
);
}
else
{
System
.
out
.
println
(
"Odczytany produkt: "
+
product
);
System
.
out
.
println
(
product
.
getProductName
()
+
" za cenę "
+
product
.
getPrice
());
if
(
product
.
getDescription
()
!=
null
)
{
System
.
out
.
println
(
product
.
getDescription
());
}
}
em
.
close
();
emf
.
close
();
}
}
PC30-Hibernate/src/main/resources/META-INF/persistence.xml
0 → 100644
View file @
cd2af3f4
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<persistence
xmlns=
"https://jakarta.ee/xml/ns/persistence"
xmlns:xsi=
"http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation=
"https://jakarta.ee/xml/ns/persistence https://jakarta.ee/xml/ns/persistence/persistence_3_0.xsd"
version=
"3.0"
>
<persistence-unit
name=
"sklep_pu"
transaction-type=
"RESOURCE_LOCAL"
>
<class>
sklep.model.Product
</class>
<class>
sklep.model.Customer
</class>
<class>
sklep.model.Order
</class>
<class>
sklep.model.OrderProduct
</class>
<properties>
<property
name=
"javax.persistence.jdbc.url"
value=
"jdbc:postgresql://localhost:5432/sklep"
/>
<property
name=
"javax.persistence.jdbc.user"
value=
"alx"
/>
<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>
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