Query for multiple tables using JPA Criteria API - java

I have to make a query in criteria api of two tables that is to say I want to select in the query all the fields of the two tables.
Here I leave the models that I want to select (I have the metamodels of each of them) and I give you an example of how to select the data
of each of them in different consultations
public List<Empleado> empleadoB() {
CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<Empleado> criteria = cb.createQuery(Empleado.class);
Root<Empleado> member = criteria.from(Empleado.class);
criteria.select(member).orderBy(cb.asc(member.get(Empleado_.p_ap)));
return em.createQuery(criteria).getResultList();
}
public List<Empleado2> empleadosNom() {
CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<Empleado2> criteria = cb.createQuery(Empleado2.class);
Root<Empleado> member = criteria.from(Empleado.class);
Root<Tipo_docs> tDoc = criteria.from(Tipo_docs.class);
Root<Estad_civil> eCivil = criteria.from(Estad_civil.class);
Root<Profesiones> prof = criteria.from(Profesiones.class);
Root<Eps> eps = criteria.from(Eps.class);
Root<Pensiones> pens = criteria.from(Pensiones.class);
Root<Usuario> useri = criteria.from(Usuario.class);
Root<Cargos> cargo = criteria.from(Cargos.class);
criteria.select(cb.construct(Empleado2.class, member.get(Empleado_.id_emp),
member.get(Empleado_.p_ap), member.get(Empleado_.s_ap),
member.get(Empleado_.nombre), member.get(Empleado_.tipo_doc),
member.get(Empleado_.numero_doc), member.get(Empleado_.lugar_exp),
member.get(Empleado_.fecha_nac), member.get(Empleado_.lugar_nac),
member.get(Empleado_.estadocivil), member.get(Empleado_.direc),
member.get(Empleado_.tel), member.get(Empleado_.profesion),
cb.selectCase() .when(cb.isNull(member.get(Empleado_.matricula_prof)), "No Registrado") .otherwise(member.get(Empleado_.matricula_prof)),
member.get(Empleado_.mail),member.get(Empleado_.eps),
cb.selectCase() .when(cb.isNull(member.get(Empleado_.pensiones)), (long)0) .otherwise(member.get(Empleado_.pensiones)),
cb.selectCase() .when(cb.isNull(member.get(Empleado_.usu)), "No Existe") .otherwise(member.get(Empleado_.usu)),
tDoc.get(Tipo_docs_.desc_tdoc),
eCivil.get(Estad_civil_.desc_ecivil), prof.get(Profesiones_.nombre_prof),
eps.get(Eps_.nom_eps),
cb.selectCase() .when(cb.isNull(member.get(Empleado_.pensiones)), "No Aplica") .otherwise(pens.get(Pensiones_.nom_pension)),
cargo.get(Cargos_.id_cargo), cargo.get(Cargos_.desc_cargo) ));
criteria.where(cb.and(cb.equal(member.get(Empleado_.tipo_doc), tDoc.get(Tipo_docs_.id_entidad)),
cb.equal(member.get(Empleado_.estadocivil), eCivil.get(Estad_civil_.id_ecivil)),
cb.equal(member.get(Empleado_.profesion), prof.get(Profesiones_.id_prof)),
cb.equal(member.get(Empleado_.eps), eps.get(Eps_.id_eps)),
cb.or(cb.equal(member.get(Empleado_.usu), useri.get(Usuario_.usuario)), cb.isNull(member.get(Empleado_.usu))),
cb.or(cb.equal(member.get(Empleado_.pensiones), pens.get(Pensiones_.id_pension)), cb.isNull(member.get(Empleado_.pensiones))) ));
criteria.distinct(true);
return em.createQuery(criteria).getResultList();
}

Related

Are criteria builder faster than named query?

I have two tables that looks like:
Table_0
ID Description
5 description5
6 description6
7 description7
8 description8
Table_1
ID Table_0_ID Status
1 5 LOADED
2 6 LOADED
3 7 LOADED
4 7 LOADED
Table_2
ID Table_1_ID
1 1
2 2
3 3
4 4
my expected result when I invoke findAll query is that it eliminates duplicates so result will return (for both tables) three rows (table_1 id from 1 to 3).
I wrote both named query and criteria builder query but the last one seems to be by 4 times faster. I wonder why. Am I making a mistake?
Here is the code:
query = "SELECT OBJECT(sb) FROM Table_2 sb WHERE sb.Table_1.id IN (SELECT MAX(maxsr.id) FROM Table_1 maxsr WHERE maxsr.status = LOADED GROUP BY maxsr.Table_0.id)")
criteria builder:
final EntityManager em = getEntityManager();
final CriteriaBuilder cb = em.getCriteriaBuilder();
final CriteriaQuery<Table_2> criteriaQuery = cb.createQuery(Table_2.class);
final Root<Table_2> root = criteriaQuery.from(Table_2.class);
Predicate p = getPredicateOnList(data, cb, root, gateSession);
if (p != null) {
criteriaQuery.where(p);
}
final Query q = getEntityManager().createQuery(criteriaQuery);
return q.getResultList();
}
method getPredicateOnList
private Predicate getPredicateOnList(final PaginationData data, final CriteriaBuilder cb, final Root<Table_2> root) {
final Join<Table_2, Table_1> readingJoin = root.join("Table_1");
boolean filterUnloaded = false;
boolean selectMax = true;
for (KeyValuePair pair : data.getRequestParams()) {
if (pair.getKey().equalsIgnoreCase("filterUnloaded")) {
filterUnloaded = ParsingUtils.parseBoolean(pair.getValue(), false);
}
if (pair.getKey().equalsIgnoreCase("selectMax")) {
selectMax = ParsingUtils.parseBoolean(pair.getValue(), true);
}
}
Predicate predicate = null;
if (selectMax) {
List<Long> maxReadingIds = getMaxReadingIds(gateSession.getId(), filterUnloaded);
if (maxReadingIds == null || maxReadingIds.isEmpty()) {
//do nothing
} else {
predicate = readingJoin.get("id").in(maxReadingIds);
}
}
return predicate;
}
method getMaxReadingIds
private List<Long> getMaxReadingIds(Long sessionId, boolean filterUnloaded) {
final CriteriaBuilder cb = em.getCriteriaBuilder();
final CriteriaQuery<Long> maxReadingIdQuery = cb.createQuery(Long.class);
final Root<Table_1> Table_1Root = maxReadingIdQuery.from(Table_1.class);
final Path<Long> idGet = Table_1Root.get("id");
maxReadingIdQuery.select(cb.greatest(idGet));
final Join<Table_1, Table_0> join = Table_1Root.join("Table_0");
maxReadingIdQuery.groupBy(join.get("id"));
Predicate predicate = null;
if (filterUnloaded) {
predicate = cb.equal(Table_1Root.get("status"), LOADED);
}
//omiss sessionId parameter
if (predicate != null) {
maxReadingIdQuery.where(predicate);
}
final Query q = getEntityManager().createQuery(maxReadingIdQuery);
return q.getResultList();
}
(I made some semplification, if code is not well defined please tell me and I supply more information)
Both results are correct but criteria builder is faster.

Generating correct and or condition with JPA Criteria API

I am trying to generate query with multiple brackets in 'or' and 'and' conditions but internal brackets are not getting generated.
Brackets for outer predicates are generating properly but not for internal ones.
Code:
CriteriaBuilder criteriaBuilder = this.getEntityManager().getCriteriaBuilder();
CriteriaQuery<ConfigurationKey> query = criteriaBuilder.createQuery(ConfigurationKey.class);
Root<ConfigurationKey> configurationKeyRoot = query.from(ConfigurationKey.class);
Join<ConfigurationKey, Customer> configurationKeyCustomerJoin = configurationKeyRoot.join(ConfigurationKey_.customer);
final List<Predicate> predicates = new ArrayList<>();
predicates.add(criteriaBuilder.equal(configurationKeyCustomerJoin.get(Customer_.externalId), customerId));
predicates.add(criteriaBuilder.equal(configurationKeyRoot.get(ConfigurationKey_.configType), configType));
final List<Predicate> orPredicates = new ArrayList<>();
keys.forEach((x, y) -> {
orPredicates.add(
criteriaBuilder.and(
criteriaBuilder.equal(configurationKeyRoot.get(ConfigurationKey_.keyType), x),
criteriaBuilder.equal(configurationKeyRoot.get(ConfigurationKey_.keyValue), y)
)
);
}
);
predicates.add(criteriaBuilder.or(orPredicates.toArray(new Predicate[orPredicates.size()])));
query.where(criteriaBuilder.and(predicates.toArray(new Predicate[predicates.size()])));
query.select(configurationKeyRoot);
TypedQuery<ConfigurationKey> typedQuery = this.getEntityManager().createQuery(query);
Generated Query:
select
configurat0_.id as id1_1_,
configurat0_.createdAt as createdA2_1_,
configurat0_.updatedAt as updatedA3_1_,
configurat0_.configType as configTy4_1_,
configurat0_.customerId as customer7_1_,
configurat0_.keyType as keyType5_1_,
configurat0_.keyValue as keyValue6_1_
from
configuration_keys configurat0_
inner join
customers customer1_
on configurat0_.customerId=customer1_.id
where
customer1_.externalId=?
and configurat0_.configType=?
and (
configurat0_.keyType=?
and configurat0_.keyValue=?
or configurat0_.keyType=?
and configurat0_.keyValue=?
)
Expected Query:
select
configurat0_.id as id1_1_,
configurat0_.createdAt as createdA2_1_,
configurat0_.updatedAt as updatedA3_1_,
configurat0_.configType as configTy4_1_,
configurat0_.customerId as customer7_1_,
configurat0_.keyType as keyType5_1_,
configurat0_.keyValue as keyValue6_1_
from
configuration_keys configurat0_
inner join
customers customer1_
on configurat0_.customerId=customer1_.id
where
customer1_.externalId=?
and configurat0_.configType=?
and (
(configurat0_.keyType=?
and configurat0_.keyValue=?)
or
(configurat0_.keyType=?
and configurat0_.keyValue=?)
)
Please point any mistake.
Specifications<PcPlacement> specification = Specifications.where(null);
Specifications<PcPlacement> specificationInner = Specifications.where(null);
specificationInner = specificationInner.or(buildReferringEntitySpecificationWithContains(
criteria.getUserFullName(), PcPlacement_.pcUser, PcUser_.fullName));
specificationInner = specificationInner.or(buildReferringEntitySpecificationWithContains(
criteria.getUserEmailId(), PcPlacement_.pcUser, PcUser_.emailId));
specification = specification.and(specificationInner);

Criteria correlated subquery in Hibernate

I am trying to write the following query:
Select b.balance, a.acctOwnerId from
AcctBalHist b, Acct a
where a.acctOwnerId in ('id1','id2','id3')
and b.acctNbr = a.acctNbr
and b.created = (Select max(b2.created)
from AcctbalHist b2
where b2.acctNbr = a.acctNbr
and b2.created < date('2012-02-02'))`
using correlated subquery in Hibernate.
Here's what I have:
CriteriaBuilder builder = hiby().getCriteriaBuilder();
CriteriaQuery<IdAmountWrapper> criteria = builder.createQuery(IdAmountWrapper.class);
Root<AcctBalHist> acctBalHistRoot = criteria.from(AcctBalHist.class);
Join<AcctBalHist, Acct> acctBalHistAcctJoin = acctBalHistRoot.join(AcctBalHist_.acct);
Subquery<Date> subqueryCriteria = criteria.subquery(Date.class);
Root<AcctBalHist> subAcctBalHistRoot = subqueryCriteria.from(AcctBalHist.class);
Join<AcctBalHist, Acct> balHistRoot = subqueryCriteria.correlate(acctBalHistAcctJoin);
balHistRoot.on(builder.equal(subAcctBalHistRoot.get(AcctBalHist_.acct), balHistRoot.get(Acct_.acctNbr)));
subqueryCriteria.select(builder.greatest(subAcctBalHistRoot.get(AcctBalHist_.created)))
.where(builder.and
(builder.lessThan(subAcctBalHistRoot.get(Actv_.created), '2012-02-02')));
criteria.select(builder.construct(IdAmountWrapper.class,
builder.abs(acctBalHistRoot.get(AcctBalHist_.balance)),
acctBalHistAcctJoin.get(Acct_.acctOwnerId)))
.where(builder.and(
acctBalHistAcctJoin.get(Acct_.acctOwnerId).in(workerIds),
builder.in(acctBalHistRoot.get(AcctBalHist_.created)).value(subqueryCriteria)));
TypedQuery<IdAmountWrapper> query = hiby().createQuery(criteria);`
Which throws the following exception:
java.lang.ClassCastException: org.hibernate.hql.internal.ast.tree.SqlNode cannot be cast to org.hibernate.hql.internal.ast.tree.FromReferenceNode
Can anyone tell me what I am doing wrong ?

Adding complicated selections to CriteriaBuilder in EclipseLink?

i want add some more complicated selections as below on date coloumn
select TO_DATE ('04-JAN-2015','DD-MM-YYYY'), to_char(TO_DATE ('04-JAN-2015','DD-MM-YYYY'), 'FMDAY') day,
(case when to_char(trunc(TO_DATE ('04-JAN-2015','DD-MM-YYYY'), 'mm'), 'FMDAY') = 'SUNDAY' then to_number(to_char(TO_DATE ('04-JAN-2015','DD-MM-YYYY'), 'W'))
else ceil((to_char(TO_DATE ('04-JAN-2015','DD-MM-YYYY'), 'dd') + 1 - to_char(next_day(trunc(TO_DATE ('04-JAN-2015','DD-MM-YYYY'), 'mm'), 'SUNDAY'), 'dd'))/7)
end)+1 week_no
from dual
here breakupFields is list of string which leads to date column
final QueryBuilder distinctDateTimeFilter = new QueryBuilder() {
final List<String> breakupFields = new ArrayList<String>(fields.length);
for (final String f : fields)
if (!Strings.isEmpty(f)) {
breakupFields.add(f);
}
}
final QueryBuilder distinctDateTimeFilter = new QueryBuilder() {
#Override
public CriteriaQuery buildQuery(CriteriaBuilder cb, CriteriaQuery query, Predicate p, List orders, Root rt) {
Expression selection = null;
selection = cb.function("TO_CHAR", String.class, cb.sum(CriteriaQueryUtils.getPath(rt, breakupFields), cb.literal(miliSecToAdd)),
cb.literal("W-MM-YYYY"));
return query.select(selection).distinct(true).where(p).orderBy(orders);
}
}
final List<Object> objs = new ArrayList<Object>(ds.executeQuery(classObject, Object.class, distinctDateTimeFilter, ef, session,
SuperUserSecurityContext.class));
Can any one suggest how to add
(case when to_char(trunc(TO_DATE ('04-JAN-2015','DD-MM-YYYY'), 'mm'), 'FMDAY') = 'SUNDAY' then to_number(to_char(TO_DATE ('04-JAN-2015','DD-MM-YYYY'), 'W'))
else ceil((to_char(TO_DATE ('04-JAN-2015','DD-MM-YYYY'), 'dd') + 1 - to_char(next_day(trunc(TO_DATE ('04-JAN-2015','DD-MM-YYYY'), 'mm'), 'SUNDAY'), 'dd'))/7)
end)+1 to criteria builder
You can introduce a column in your Entity marked as
#Formula(value="the expression")
See more here and use the column in the queries.
Or you can define native SQL query with the desired entity class mapped and use fuly native SQL.

Predicates on Join not working

If I am using a Join to create predicates in a criteriaquery I won't get any results. The same predicates are returning entities when I am using the Game table as root.
Working Query:
CriteriaQuery<Game> query = cb.createQuery(Game.class);
Root<Game> root = query.from(Game.class);
List<Predicate> predicates = new LinkedList<Predicate>();
if(!selectedPlatforms.isEmpty()) {
predicates.add(root.get(Game_.type).in(TypeConverter.convert(selectedPlatforms)));
}
if(!selectedCategories.isEmpty()) {
Join<Game, String> collection = root.join(Game_.categories);
predicates.add(collection.in(cb.literal(selectedCategories)));
}
if(!selectedGames.isEmpty()) {
predicates.add(cb.isTrue(root.get(Game_.name).in(selectedGames)));
}
query.where(cb.and(predicates.toArray(new Predicate[predicates.size()])));
games = em.createQuery(query).getResultList();
Not working query:
CriteriaQuery<Hit> query = cb.createQuery(Hit.class);
List<Predicate> predicates = new LinkedList<>();
Date startDate = null;
Date endDate = null;
Root<Hit> hitRoot = query.from(Hit.class);
switch (time) {
case "Week":
startDate = new DateTime().withWeekOfWeekyear(timeValue).withDayOfWeek(DateTimeConstants.MONDAY).toDate();
endDate = new DateTime().withWeekOfWeekyear(timeValue+1).withDayOfWeek(DateTimeConstants.SUNDAY).toDate();
}
predicates.add(cb.and(cb.greaterThanOrEqualTo(hitRoot.<Date>get("hitDate"), startDate), cb.lessThanOrEqualTo(hitRoot.<Date>get("hitDate"), endDate)));
Join<Hit, Game> gameJoin = hitRoot.join("game", JoinType.LEFT);
if(!selectedPlatforms.isEmpty()) {
predicates.add(gameJoin.get(Game_.type).in(TypeConverter.convert(selectedPlatforms)));
}
if(!selectedCategories.isEmpty()) {
Join<Game, String> collection = gameJoin.join(Game_.categories);
predicates.add(collection.in(cb.literal(selectedCategories)));
}
if(!selectedGames.isEmpty()) {
predicates.add(cb.isTrue(gameJoin.get(Game_.name).in(selectedGames)));
}
query.groupBy(hitRoot.get("hitDate"), hitRoot.get("shop"));
query.orderBy(cb.asc(hitRoot.get("shop")));
query.where(predicates.toArray(new Predicate[predicates.size()]));
List<Hit> results = em.createQuery(query).getResultList();
The following part is responsible for not returning any machting entity. The same part just applied to a Root instead of Join like in the first query is returning machting entites. Without this part everything else is working.
if(!selectedGames.isEmpty()) {
predicates.add(cb.isTrue(gameJoin.get(Game_.name).in(selectedGames)));
}
The problem resolved itself by restarting the application server in 'Run' mode.

Categories

Resources