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
6b196884
Commit
6b196884
authored
Sep 30, 2022
by
Patryk Czarnik
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
JPQL i listy
parent
ea24d192
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
112 additions
and
6 deletions
+112
-6
persistence.xml
PC27-Hibernate/src/main/java/META-INF/persistence.xml
+1
-0
Product.java
PC27-Hibernate/src/main/java/sklep/model/Product.java
+16
-6
OdczytajWszystkieProdukty1_JPQL.java
...java/sklep/przyklady/OdczytajWszystkieProdukty1_JPQL.java
+36
-0
OdczytajWszystkieProdukty2_NamedQuery.java
...klep/przyklady/OdczytajWszystkieProdukty2_NamedQuery.java
+32
-0
OdczytajWszystkieProdukty3_Stream.java
...va/sklep/przyklady/OdczytajWszystkieProdukty3_Stream.java
+27
-0
No files found.
PC27-Hibernate/src/main/java/META-INF/persistence.xml
View file @
6b196884
...
@@ -12,6 +12,7 @@
...
@@ -12,6 +12,7 @@
<property
name=
"javax.persistence.jdbc.password"
value=
"abc123"
/>
<property
name=
"javax.persistence.jdbc.password"
value=
"abc123"
/>
<property
name=
"javax.persistence.jdbc.driver"
value=
"org.postgresql.Driver"
/>
<property
name=
"javax.persistence.jdbc.driver"
value=
"org.postgresql.Driver"
/>
<property
name=
"hibernate.show_sql"
value=
"true"
/>
<property
name=
"hibernate.show_sql"
value=
"true"
/>
<!-- To będzie pokazywać kody SQL -->
</properties>
</properties>
</persistence-unit>
</persistence-unit>
</persistence>
</persistence>
PC27-Hibernate/src/main/java/sklep/model/Product.java
View file @
6b196884
...
@@ -4,27 +4,37 @@ import java.io.Serializable;
...
@@ -4,27 +4,37 @@ import java.io.Serializable;
import
javax.persistence.*
;
import
javax.persistence.*
;
import
java.math.BigDecimal
;
import
java.math.BigDecimal
;
/**
/**
* The persistent class for the products database table.
* The persistent class for the products database table.
*
*
*/
*/
/*
* Sposób na zapisanie kilku named queries w starszych wersjach Javy (tylko nie wiem jakich ;-) )
*
* @NamedQueries({
* @NamedQuery(name="Product.findAll", query="SELECT p FROM Product p"),
* @NamedQuery(name="Product.sredniaCena", query="SELECT avg(p.price) FROM Product p"),
* })
*/
@Entity
@Entity
@Table
(
name
=
"products"
)
@Table
(
name
=
"products"
)
@NamedQuery
(
name
=
"Product.findAll"
,
query
=
"SELECT p FROM Product p"
)
@NamedQuery
(
name
=
"Product.findAll"
,
query
=
"SELECT p FROM Product p"
)
@NamedQuery
(
name
=
"Product.sredniaCena"
,
query
=
"SELECT avg(p.price) FROM Product p"
)
public
class
Product
implements
Serializable
{
public
class
Product
implements
Serializable
{
private
static
final
long
serialVersionUID
=
1L
;
private
static
final
long
serialVersionUID
=
1L
;
@Id
@Id
@GeneratedValue
(
strategy
=
GenerationType
.
IDENTITY
)
@GeneratedValue
(
strategy
=
GenerationType
.
IDENTITY
)
@Column
(
name
=
"product_id"
,
insertable
=
false
,
updatable
=
false
)
@Column
(
name
=
"product_id"
,
insertable
=
false
,
updatable
=
false
)
private
Integer
productId
;
private
Integer
productId
;
private
String
description
;
private
String
description
;
private
BigDecimal
price
;
private
BigDecimal
price
;
@Column
(
name
=
"product_name"
)
@Column
(
name
=
"product_name"
)
private
String
productName
;
private
String
productName
;
private
BigDecimal
vat
;
private
BigDecimal
vat
;
...
...
PC27-Hibernate/src/main/java/sklep/przyklady/OdczytajWszystkieProdukty1_JPQL.java
0 → 100644
View file @
6b196884
package
sklep
.
przyklady
;
import
java.util.List
;
import
javax.persistence.EntityManager
;
import
javax.persistence.EntityManagerFactory
;
import
javax.persistence.Persistence
;
import
javax.persistence.TypedQuery
;
import
sklep.model.Product
;
public
class
OdczytajWszystkieProdukty1_JPQL
{
public
static
void
main
(
String
[]
args
)
{
EntityManagerFactory
emf
=
Persistence
.
createEntityManagerFactory
(
"sklep"
);
EntityManager
em
=
emf
.
createEntityManager
();
// Chcemy wypisać wszystkie produkty.
// W tej wersji zadamy odpowiednie zapytania JPQL
// JPQL = Java Persistence Query Language (czasami mówi się też HQL - Hibernate Q.L.)
// Język wzorowany na SQL, ale:
// - interpretowany przez Hibernate'a (lub inną implementację JPA) po stronie Javy, a nie przez serwer SQL
// - zamiast nazw tabel i kolumn używamy nazw klas i pól
TypedQuery
<
Product
>
query
=
em
.
createQuery
(
"SELECT p FROM Product p ORDER BY p.productId"
,
Product
.
class
);
List
<
Product
>
products
=
query
.
getResultList
();
System
.
out
.
println
(
"Odczytano "
+
products
.
size
()
+
" rekordów."
);
for
(
Product
product
:
products
)
{
System
.
out
.
println
(
" * "
+
product
.
getProductName
()
+
" za cenę "
+
product
.
getPrice
()
+
", opis: "
+
product
.
getDescription
());
}
em
.
close
();
emf
.
close
();
}
}
PC27-Hibernate/src/main/java/sklep/przyklady/OdczytajWszystkieProdukty2_NamedQuery.java
0 → 100644
View file @
6b196884
package
sklep
.
przyklady
;
import
java.util.List
;
import
javax.persistence.EntityManager
;
import
javax.persistence.EntityManagerFactory
;
import
javax.persistence.Persistence
;
import
javax.persistence.TypedQuery
;
import
sklep.model.Product
;
public
class
OdczytajWszystkieProdukty2_NamedQuery
{
public
static
void
main
(
String
[]
args
)
{
EntityManagerFactory
emf
=
Persistence
.
createEntityManagerFactory
(
"sklep"
);
EntityManager
em
=
emf
.
createEntityManager
();
// Chcemy wypisać wszystkie produkty.
// W tej wersji skorzystamy z gotowego "named query".
TypedQuery
<
Product
>
query
=
em
.
createNamedQuery
(
"Product.findAll"
,
Product
.
class
);
List
<
Product
>
products
=
query
.
getResultList
();
System
.
out
.
println
(
"Odczytano "
+
products
.
size
()
+
" rekordów."
);
for
(
Product
product
:
products
)
{
System
.
out
.
println
(
" * "
+
product
.
getProductName
()
+
" za cenę "
+
product
.
getPrice
()
+
", opis: "
+
product
.
getDescription
());
}
em
.
close
();
emf
.
close
();
}
}
PC27-Hibernate/src/main/java/sklep/przyklady/OdczytajWszystkieProdukty3_Stream.java
0 → 100644
View file @
6b196884
package
sklep
.
przyklady
;
import
javax.persistence.EntityManager
;
import
javax.persistence.EntityManagerFactory
;
import
javax.persistence.Persistence
;
import
javax.persistence.TypedQuery
;
import
sklep.model.Product
;
public
class
OdczytajWszystkieProdukty3_Stream
{
public
static
void
main
(
String
[]
args
)
{
EntityManagerFactory
emf
=
Persistence
.
createEntityManagerFactory
(
"sklep"
);
EntityManager
em
=
emf
.
createEntityManager
();
// W tej wersji wyniki zapytania odczytamy nie jako listę, tylko jako strumień.
// Dzięki temu nie czytamy wszystkich rekordów na raz i możemy przetwarzać nawet duże tabele.
TypedQuery
<
Product
>
query
=
em
.
createNamedQuery
(
"Product.findAll"
,
Product
.
class
);
query
.
getResultStream
().
forEach
(
product
->
{
System
.
out
.
println
(
" * "
+
product
.
getProductName
()
+
" za cenę "
+
product
.
getPrice
()
+
", opis: "
+
product
.
getDescription
());
});
em
.
close
();
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