public class CustomItemTypeRepositoryImpl
{
@Autowired
private
EntityManager entityManager;
public void deleteCustomById
(ItemType itemType) {
entityManager.remove(itemType);
}
public List
getAll() {
Query query = this.entityManager.createQuery("SELECT p From ProfileEntity as p");
List profileEntities = query.getResultList();
return profileEntities;
}
}
dasturlash.uz
Custom or Composable Repositories
public List
getAll() {
Query query = entityManager.createNativeQuery("SELECT * FROM profile ", ProfileEntity.class);
List profileEntities = query.getResultList();
return profileEntities;
}
dasturlash.uz
JPA Criteria
The JPA Criteria API provides an alternative way for defining JPA queries, which is
mainly useful for building dynamic queries whose exact structure is only known at
runtime.
First JPA Criteria Query
SELECT c FROM Country c
CriteriaBuilder
cb = em.
getCriteriaBuilder
();
CriteriaQuery
q = cb.
createQuery
(Country.class);
Root
c = q.
from
(Country.class);
q.
select
(c);
TypedQuery
query = em.
createQuery
(q);
List results = query.
getResultList
();
Select * from country
dasturlash.uz
JPA Criteria
CriteriaBuilder
cb = em.
getCriteriaBuilder
();
CriteriaQuery
q = cb.
createQuery
(Country.class);
Root
c = q.
from
(Country.class);
q.
select
(c);
TypedQuery
query = em.
createQuery
(q);
List results = query.
getResultList
();
The
CriteriaBuilder
interface serves as the main factory of criteria queries
and criteria query elements.
It can be obtained either by
the
EntityManagerFactory
's
getCriteriaBuilder
method or by
the
EntityManager
's
getCriteriaBuilder
In the example above a
CriteriaQuery
instance is created for representing the
built query.
Root
instance is created to define a range variable in the FROM clause.
Finally, the range variable, c, is also used in the SELECT clause as the query
result expression.
A CriteriaQuery instance is equivalent to a JPQL string and not to
a
TypedQuery
instance.
Therefore, running the query still requires a TypedQuery instance:
dasturlash.uz
JPA Criteria – example 1
CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
CriteriaQuery
criteriaQuery = criteriaBuilder.createQuery(ProfileEntity.class);
Root
root = criteriaQuery.from(ProfileEntity.class);
criteriaQuery.select(root);
Predicate predicate = criteriaBuilder.equal(root.get("name"), "Vali");
criteriaQuery.where(predicate);
Select * from profile where name=?
dasturlash.uz
JPA Criteria – example 2
CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
CriteriaQuery
criteriaQuery = criteriaBuilder.createQuery(ProfileEntity.class);
Root
root = criteriaQuery.from(ProfileEntity.class);
criteriaQuery.select(root);
Predicate namePredicate = criteriaBuilder.equal(root.get("name"), "Vali");
Predicate idPredicate = criteriaBuilder.equal(root.get("id"), 33);
criteriaQuery.where(namePredicate, idPredicate);
List
studentList = entityManager.createQuery(criteriaQuery).getResultList();
Select * from profile where name=? and id=33
Can be joined with Predicate
Predicate nameAndIdPredicate = criteriaBuilder.and(namePredicate, idPredicate);
criteriaQuery.where(nameAndIdPredicate);
dasturlash.uz
dasturlash.uz
JPA Criteria – example 2
CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
CriteriaQuery
criteriaQuery = criteriaBuilder.createQuery(ProfileEntity.class);
Root
root = criteriaQuery.from(ProfileEntity.class);
criteriaQuery.select(root);
Predicate namePredicate = criteriaBuilder.equal(root.get("name"), "Vali");
Predicate idPredicate = criteriaBuilder.equal(root.get("id"), 33);
Predicate nameOrIdPredicate = criteriaBuilder.or(namePredicate, idPredicate);
criteriaQuery.where(nameOrIdPredicate);
List
studentList = entityManager.createQuery(criteriaQuery).getResultList();
Select * from profile where name=? or id=33
dasturlash.uz
JPA Criteria – example 3
…
Predicate namePredicate = criteriaBuilder.equal(root.get("name"), "Vali");
Predicate idPredicate = criteriaBuilder.equal(root.get("id"), 5);
Predicate surnamePredicate = criteriaBuilder.equal(root.get("surname"), "Aliyev");
Predicate nameAndIdPredicate = criteriaBuilder.and(namePredicate, idPredicate);
Predicate nameAndIdOrSurnamePredicate = criteriaBuilder.or(nameAndIdPredicate,
surnamePredicate);
criteriaQuery.where(nameAndIdOrSurnamePredicate);
List
studentList =
entityManager.createQuery(criteriaQuery).getResultList();
select * from profile where name = 'Vali' and id = 4 or surname = 'Aliyev'
dasturlash.uz
JPA Criteria
CriteriaQuery metods:
select
Where
groupBy
orderBy
distinct
CriteriaBuilder metods
And
Or, not
equals, notEqual
sum, max, min, count
isTrue, isFalse,
Juda ko’p
https://docs.oracle.com/javaee/6/api/javax/persistence/criteria/CriteriaBuilder.html
Ideyani tushunganskiy?
dasturlash.uz
JPA Criteria
Using the criteria API introduces some extra work, at least for simple static
queries, since the equivalent JPQL query could simply be executed as follows:
TypedQuery
query =
em.
createQuery
("SELECT c FROM Country
c", Country.class); List results = query.
getResultList
();
Because eventually both types of queries are represented by
a
TypedQuery
instance -
query execution
and
query setting
is similar,
regardless of the way in which the query is built.
dasturlash.uz
Specification
public interface Specification {
// static Specification
not
(@Nullable Specification spec) ...
// static Specification
where
(@Nullable Specification spec) ...
// default Specification
and
(@Nullable Specification other) ..
// default Specification or(@Nullable Specification other)
// for add pridicate
Predicate toPredicate(Root root, CriteriaQuery> query,
CriteriaBuilder builder);
}
dasturlash.uz
JpaSpecificationExecutor
public interface JpaSpecificationExecutor {
Optional findOne(@Nullable Specification var1);
List findAll(@Nullable Specification var1);
Page findAll(@Nullable Specification var1, Pageable var2);
List findAll(@Nullable Specification var1, Sort var2);
long count(@Nullable Specification var1);
}
dasturlash.uz
Specification - Example
1. Extend from JpaSpecificationExecutor
public interface ProfileRepository extends JpaRepository
Integer>,
JpaSpecificationExecutor
2. Define a Specification method
public static Specification
byName(final String name) {
return new Specification
() {
@Override
public Predicate toPredicate(Root
root,
CriteriaQuery> criteriaQuery,
CriteriaBuilder criteriaBuilder) {
return criteriaBuilder.equal(root.get("name"), name);
}
};
}
dasturlash.uz
Specification - Example
Specification
spec = Specification.
Do'stlaringiz bilan baham: |