I am a beginner with JPA.
My query is,
Select a,b,c FROM Table1 one, Table2 two where one.a=two.a;
How can i perform this with Criteria builder?
Assume there is a department table which contains some number of people .Below is a join example of CriteriaBuilder.
CriteriaBuilder cb = entityManager.getCriteriaBuilder();
CriteriaQuery<Person> query = cb.createQuery(Person.class);
Root<Department> department= query.from(Department.class);
Join<Department, Person> people= departent.join("people");
query.select(people).where(cb.equal(person.get("name"), "Michael"));
Related
I have a scenario where I need to get the row with maximum date. The SQL query for this would be
SELECT c.*
FROM course c
INNER JOIN
(SELECT moduleId ,MAX(endDate) AS max_date
FROM course
WHERE moduleId = 12345
GROUP BY moduleId
) customSelect
ON customSelect.moduleId = c.moduleId AND c.endDate = customSelect.max_date
WHERE c.moduleId = 12345
I need to convert this query to JPA using CriteriaBuilder.Since this isn't a direct entity join, rather a selection join to root entity, I'm having trouble to figure out how to join the custom select part to the root entity Course with below syntax:
CriteriaBuilder builder = session.getCriteriaBuilder();
CriteriaQuery<Course> criteriaQuery = builder.createQuery(Course.class);
Root<Course> root = criteriaQuery.from(Course.class);
And then how to root.join(JoinType.INNER) to join customSelect part?
If someone can show some pointers to the syntax of of joining the root to a custom selection, that would be great.
Try this
CriteriaBuilder builder = session.getCriteriaBuilder();
CriteriaQuery<Course> criteriaQuery = builder.createQuery(Course.class);
Root<Course> root = criteriaQuery.from(Course.class);
criteriaQuery.select(root).where(cb.equal(root.get(Course_.moduleId), 12345));
criteriaQuery.orderBy(cb.desc(r.get(Course_.endDate)));
TypedQuery<Course> query = em.createQuery(criteriaQuery);
query.setMaxResult(1);
Course result = query.getSingleResult();
Let's say, I have a query like
Select a.valA, b.valB
from tableA a join tableB b on a.joinCol = b.joinCol
where a.someCol = 1.
I want to execute it using Hibernate (and Spring Data) in one query to the database. I know, I can write just
Query query = em.createQuery(...);
List<Object[]> resultRows = (List<Object[]>)query.getResultList();
But my question would be - is it possible to do it in a typesafe way, using CriteriaQuery for example? The difficulty is, that, as you see, I need to select values from different tables. Is there some way to do this?
Simple example where an Employee has many to many relation to several jobs that he may have :
CriteriaBuilder builder = session.getCriteriaBuilder();
CriteriaQuery<Tuple> criteria = builder.createTupleQuery();
Root<TableA> root = criteria.from(TableA.class);
Path<Long> qId = root.get("id");
Path<String> qTitle = root.get("title");
Join<TableA, TableB> tableTwo = root.join("joinColmn", JoinType.INNER);
criteria.multiselect(qId, qTitle, tableTwo);
List<Tuple> tuples = session.createQuery(criteria).getResultList();
for (Tuple tuple : tuples)
{
Long id = tuple.get(qId);
String title = tuple.get(qTitle);
TableB tableB= tuple.get(tableTwo);
}
but saw that there is an alternate answer here :
JPA Criteria API - How to add JOIN clause (as general sentence as possible)
I am stuck in a situation where I am not able to create a filter on left join between two table.
My query is something like this.
select count(*)
from orders o
left
join payments p
on o.id = p.o_id
where o.uid_id = 1
and p.name = "abc"
I am trying to do left join using criteria query. In criteria query.
CriteriaBuilder cb = entityManager.getCriteriaBuilder();
CriteriaQuery<Orders> query = cb.createQuery(Orders.class);
Root<Orders> ordersRoot = query.from(Orders.class);
Join<Orders, Payments> join = ordersRoot.join(JOIN_COLUMN, JoinType.LEFT);
List<Predicate> predicates = new ArrayList<>();
predicates.addAll(getPredicates(cb, ordersRoot)); //this method gives other predicates on order table
query.where(predicates.toArray(new Predicate[predicates.size()]));
query.select(ordersRoot).distinct(true);
TypedQuery<Orders> query = entityManager.createQuery(criteriaQuery);
List<Orders> list = query.getResultList();
This is my criteria Java code.
In this I have not added for this check.
orders and payments table has one to many relationship.
SQL QUERY:
SELECT SUM(IF(table.type='type1', 1, 0)) as type1,SUM(IF(table.type='type2', 1, 0)) as type2 from table;
How to write same query in criteria builder like below:
CriteriaBuilder builder = entityManager.getCriteriaBuilder();
CriteriaQuery<Object[]> criteria = builder.createQuery( Object[].class );
Root<Table> root = criteria.from( Table.class );
criteria.multiselect(builder.sum())
I am unable to place if condition in criteria builder.
Note: there is bean class for Table.
Thanks in advance.
Apparently hibernate criteria does not support IF. I would consider just making 2 queries with criteria or using HQL or just native SQL.
I'd like to know how to make a query in JPA where content is not inside a "third table" responsible to make relationship manyToMany.
example:
SELECT id FROM A WHERE id NOT IN (SELECT id FROM A_B WHERE idB = #id)
In this case I have Table A, Table B, and the relationShip manytomany A_B
I tried with CriteriaQuery and subquery, but it didn't work. Actually, it returned none result.
Does somebody have any example for this case?
I got my solution!
I don't know if it is completely correct, so, if someone see an error, please let me know.
The end query, created by JPA become this:
select maingroup0_.GroupId as GroupId1_, maingroup0_.idGroupAttendanceType as idGroupA2_1_, maingroup0_.name as name1_ from TUnPbxGroup maingroup0_ where maingroup0_.GroupId not in (select maingroup1_.GroupId from TUnPbxGroup maingroup1_ inner join THolidayGroup listholida2_ on maingroup1_.GroupId=listholida2_.GroupId inner join THoliday holiday3_ on listholida2_.IdHoliday=holiday3_.IdHoliday where holiday3_.IdHoliday=2)
The code:
CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<MainGroup> query = cb.createQuery(MainGroup.class);
Root<MainGroup> root = query.from(MainGroup.class);
query.select(root);
Subquery<Long> subquery = query.subquery(Long.class);
Root<MainGroup> subRoot = subquery.from(MainGroup.class);
subquery.select(subRoot.<Long>get("id"));
Join<Holiday, Holiday> maingroups = subRoot.join("listHolidays");
subquery.where(cb.equal(maingroups.get("id"), holidayId));
query.where(cb.not(cb.in(root.get("id")).value(subquery)));
TypedQuery<MainGroup> typedQuery = em.createQuery(query);
List<MainGroup> result = typedQuery.getResultList();