JPA Criteria Builder order by a sum column - java

I have the following SQL query.
select colA, sum(colB) as colB from tableA group by colA order by colB desc;
I have used jpa criteria builder to build the query dynamically.
List<String> selectCols = new ArrayList<>();
selectCols.add("colA");
List<String> sumColumns = new ArrayList<>();
sumColumns.add("colB");
List<String> groupByColumns = new ArrayList<>();
groupByColumns.add("colA");
List<String> orderingColumns = new ArrayList<>();
orderingColumns.add("colB");
CriteriaBuilder builder = entityManager.getCriteriaBuilder();
CriteriaQuery<Tuple> cQuery = builder.createQuery(Tuple.class);
Root<T> root = cQuery.from(getDomainClass());
List<Selection<?>> selections = new ArrayList<>();
for (String column : selectCols) {
selections.add(root.get(column).alias(column));
}
if (sumColumns != null) {
for (String sumColumn : sumColumns) {
selections.add(builder.sum(root.get(sumColumn)).alias(sumColumn));
}
}
cQuery.multiselect(selections);
List<Expression<?>> groupByExpressions = new LinkedList<>();
for (String column : groupByColumns) {
groupByExpressions.add(root.get(column));
}
cQuery.groupBy(groupByExpressions);
List<Order> orders = new ArrayList<>();
for (String orderCol : orderingColumns) {
orders.add(builder.desc(root.get(orderCol)));
}
cQuery.orderBy(orders);
entityManager.createQuery(cQuery).getResultList();
I debugged and checked the exact sql statement that is getting executed.
select colA as col_1_0, sum(colB) as col_2_0 from tableA group by colA order by colB desc;
The resultant sql statement that is getting executed doesn't have the alias for colB as colB. I am using MySql. Surprisingly, this statement gets executed and I get proper results when sql mode is set to NO_ENGINE_SUBSTITUTION. But when the sql mode is ONLY_FULL_GROUP_BY, the database throws an error saying
Expression #1 of ORDER BY clause is not in GROUP BY clause and
contains nonaggregated column 'colB' which is not functionally
dependent on columns in GROUP BY clause; this is incompatible with
sql_mode=only_full_group_by
I understand that in this mode, sql validates the query and my query fails. How do I create an alias with jpa criteria builder so that the actual statement getting executed contains a proper alias for the sum column?
Note: I use spring-data-jpa and this is a custom implementation of repository. I can manually write the query using #Query annotation but the nature of the queries are dynamic on group by, order by, where clauses and the select columns. Hence the need for a dynamic criteria builder implementation.

Try sorting over the aggregate. In my experience, column aliases usually can't be referenced in the query itself.
orders.add(builder.desc(builder.sum(sumColumn)));

i think so you have bug in orderingColumns, you schould add colA instead colB
or instead
for (String orderCol : orderingColumns) {
orders.add(builder.desc(root.get(orderCol)));
}
you can use
orders.add(builder.desc(root.get("colA")));

Related

Hibernate: Query to select values from multiple tables using CriteriaQuery

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)

Java SQL Iterating 2 ResultSet and merging them

let's say i have 2 queries and 2 ResultSet. the first one is members table query, while the second query is for other member datas. now i want to join the first resultset with the second one. for example it looks like this
ResultSet rsMember = psMembers.executeQuery();
ResultSet rsCustomValues = psCustomValues.executeQuery();
// object for mapping query results
MembersMapper memberMapper = new MembersMapper();
while (rsMember.next()) {
memberMapper.setId(rsMember.getString("id"));
memberMapper.setName(rsMember.getString("name"));
memberMapper.setUsername(rsMember.getString("username"));
memberMapper.setGroup(rsMember.getString("group_id"));
List strCustomValues = new ArrayList<>();
while(rsCustomValues.next()){
// map the custom values
Map<String, Object> mapTemp = new HashMap<String, Object>();
mapTemp.put(FIELD_ID, rsCustomValues.getString("custom_field_id"));
mapTemp.put(INTERNAL_NAME,
rsCustomValues.getString("custom_field_internalname"));
mapTemp.put(NAME,rsCustomValues.getString("custom_field_name"));
strCustomValues.add(mapTemp);
}
memberMapper.setCustomvalues(strCustomValues);
}
the problem is the second (inner while) query. what connects data between first and second resultset is member id, which is primary key in first table (first query) and foreign key in second query. so the second query will have member id in random order.
so how can i order the second query without having to put 'order by member_id' in the second query? i will have to avoid 'order by member_id' because it will take time to process.
Edit: here's the scripts
First script
select
mbr.*, usr.username, grp.name as groupname, grp.status
from members mbr
join users usr on mbr.id = usr.id
join groups grp on mbr.group_id = grp.id
where mbr.id > #id#
order by id asc
limit #limit#
Second script
select
cfv.member_id as 'member_id', cf.id as 'custom_field_id',
cf.internal_name as 'custom_field_internalname',
cf.name as 'custom_field_name', cfv.string_value as 'cfv_stringvalue',
cfv.possible_value_id as 'cf_possiblevalueid', cfvp.value as 'cfvpvalue'
from custom_field_values cfv
join custom_fields cf on cf.id = cfv.field_id
left join custom_field_possible_values cfvp on cfv.possible_value_id = cfvp.id
where exists(
select * from (select id from members where id > #id#
limit #limit#
) result where result.id = cfv.member_id)
and cf.subclass = #subclass#
order by cfv.member_id asc
It is better to fetch the second result set to an object representation and sort it out.
See a similar question here How can I sort ResultSet in java?

Inner join query on Hibernate - SQL queries do not currently support iteration

I'm new to hibernate and I've this SQL query which works perfectly
SELECT count(*) as posti_disponibili from occupazione t inner join
(select id_posto_park, max(date_time) as MaxDate from occupazione
group by id_posto_park) tm on t.id_posto_park = tm.id_posto_park and
t.date_time = tm.Maxdate and t.isOccupied = 0
which gives me all the last items with isOccupied = 0
I was porting it into Hibernate, I've tried to use
result = ( (Integer) session.createSQLQuery(query).iterate().next() ).intValue()
to return posti_disponibili but i got this exception
java.lang.UnsupportedOperationException: SQL queries do not currently support iteration
How can i solve this? I cannot find the equivalent HQL query
Thank you
I would suggest you to use
Query#uniqueResult()
which will give you single result.
select count(*) .....
will always return you a single result.
Hibernate support it's own iterator-like scroll:
String sqlQuery = "select a, b, c from someTable";
ScrollableResults scroll = getSession().createSQLQuery(sqlQuery).scroll(ScrollMode.FORWARD_ONLY);
while (scroll.next()) {
Object[] row = scroll.get();
//process row columns
}
scroll.close();

Java Criteria API: Select single column from one-to-many relationship table

I am trying to select a single column from a related table. I have a table (Item) with many Values. I would like to select Value.valueString.
Basically, the query is supposed to pass in a bunch of values and pull any ValueFields that contain those values. The SQL might look something like this:
select ItemValues.valueString from ItemEntity
join StockItem on ItemEntity.stockItemId = StockItem.id
join ItemValues on ItemEntity.id = ItemValues.itemId
where StockItem.vendor = vendorId
AND (ItemValues.valueString like '%test%' OR ItemValues.valueString like '%test2%'...);
Here is my code:
final CriteriaBuilder builder = this.entityManager.getCriteriaBuilder();
final CriteriaQuery<String> query = builder.createQuery(String.class);
final Root<ItemEntity> root = query.from(ItemEntity.class);
query.select(root.join("ItemValues").<String>get("ValueString"));
final List<Predicate> filters = new LinkedList<Predicate>();
filters.add(builder.equal(root.join("StockItem").get("id"), vendorNumber));
final List<Predicate> filterNamesCriteria = new LinkedList<Predicate>();
if (filenames.length > 0) {
for (String fileName : filenames) {
filterNamesCriteria.add(builder.like(root.join("ItemValues").<String>get("ValueString"), fileName));
}
filters.add(builder.or(filterNamesCriteria.toArray(new Predicate[0])));
}
query.where(filters.toArray(new Predicate[0]));
final TypedQuery<String> resolvedQuery = this.entityManager.createQuery(query);
return resolvedQuery.getResultList();
I want the result to return a List of Strings (valueString column), but it's not returning anything.
Am I doing something wrong? When I say "builder.createQuery(String.class)", is that correct?
I found the problem:
filters.add(builder.equal(root.join("StockItem").get("id"), vendorNumber));
I was joining based on the StockItem id and not the StockItem.itemNumber
I used two queries to solve the issue of joining the Itemvalues map (it was returning 32,000+ results)

Named Query Possibility

I have a fields of customerName, MembershipNumber, nationality and some other field in my customer table and if i get one value from the above three. I need to write only one named query to get the value from the Customer table from the value i got. Is there any possibility to do that by named query without use of normal query in jpa?...
StringBuilder s=new StringBuilder();
s.append("select c from customerdetail c where )
if(customerName!=null)
{
s.append(" c.customerName = :customerName")
}
else if(memberShipNumber!=null)
{
s.append(" c.memberShipNumber = :memberShipNumber")
}
else if(nationality!=null)
{
s.append(" nationality = :nationality)
}
Here i use the same table with three conditions. So is there any possiblity to write only one named query or any other static query to satisfy all the three conditions in jpa?
Try reading ObjectDB's manual on JPA Queries. It provides information on selecting JPA entities and different variations with its custom fields. It has query examples expressed as in JPQL so with use of Criteria. And yes, you can define namedQuery using JPQL and later use-re-use it.
Named queries are static & their scope is persistence context, they can't be altered at runtime.
Below is the sample for adding parameter based on condition using Criteria API.
CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<CustomerDetail> cq = cb.createQuery(CustomerDetail.class);
Metamodel m = em.getMetamodel();
EntityType<CustomerDetail> CustomerDetail_ = m.entity(CustomerDetail.class);
Root<CustomerDetail> detail = cq.from(CustomerDetail.class);
if(customerName != null)
cq.where(cb.equal(detail.get(CustomerDetail_.customerName), customerName));
if(memberShipNumber != null)
cq.where(cb.equal(detail.get(CustomerDetail_.memberShipNumber), memberShipNumber));
if(nationality != null)
cq.where(cb.equal(detail.get(CustomerDetail_.nationality), nationality));
cq.select(detail);
TypedQuery<CustomerDetail> q = em.createQuery(cq);
List<CustomerDetail> customerList= q.getResultList();
Else, you can go with building a query with string by appending conditions, instead of named query.
You can use projection with NamedQuery, this example will obtain a single customerName field if MembershipNumber is unique or a List with every customerName that matches the where condition:
#Entity
#NamedQuery(name="getCustomerName", query="SELECT c.customerName FROM Customer c WHERE c.membershipNumber = :memNum")
public class Customer {
...
}
Then you can call it with: (em is your EntityManager)
String customerName = em.createNamedQuery("getCustomerName")
.setParameter("memNum", myMembershipNumber)
.getSingleResult();

Categories

Resources