I'm trying to make a new calculated col with criteria query like this SQL:
SELECT *, SUM(quantity) + SUM(pledged) as total, SUM(pledged) as pledged FROM stock_position GROUP BY partner_id, product_id
I need to get this result and set to another entity, so I need to use the alias total and map to entity. But I don't find a way to do this. I tried the code below.
final CriteriaQuery<Stock> query = getCriteriaBuilder().createQuery(Stock.class);
final Root<StockPosition> root = query.from(StockPosition.class);
Expression<Double> quantity = getCriteriaBuilder().sum(root.<Double> get("quantity"));
Expression<Double> pledged = getCriteriaBuilder().sum(root.<Double>get("pledged"));
Expression<Double> total = getCriteriaBuilder().sum(quantity, pledged);
query.select(total); //THIS GIVES ME ERROR
query.groupBy(root.get("partner"), root.get("product"));
Of course the types are different, but I don't know how to map. Anyone can give a hand?
Thanks
Simple: You want to sum up some values, but the query expects an entity of the class Stock.
The correct declaration would be as follows:
final CriteriaQuery<Double> query = getCriteriaBuilder().createQuery(Double.class);
Related
I have a problem while converting the following statement into jooq API:
SELECT t1.col1, t1.col2, t1.col3, (SELECT count(*) FROM table2 where table2.col2 = t1.col1)
FROM table1 t1
I tried it with DSL.count() and DSL.selectCount() but I failed while searching a way to add the where clause to the count subquery.
The database is PostgreSQL 9.6.
Lukas suggestion to use DSL.field is the better solution because it preserves the <T> type.
More typesafe version:
TableField<Table1Record, Long> col1 = TABLE1.COL1;
Field<Integer> count = DSL.field(DSL.selectCount().from(TABLE2).where(TABLE2.COL2.eq(col1)));
using(configuration).select(col1, count).from(TABLE1).fetch();
My first (less typesafe) solution:
TableField<Table1Record, Long> col1 = TABLE1.COL1;
Field count = DSL.selectCount().from(TABLE2).where(TABLE2.COL2.eq(col1)).asField("count");
using(configuration).select(col1, count).from(TABLE1).fetch();
Maybe there is a more elegant solution, but it works. The generated query looks like my original query.
Here is another example using DSL.field(...):
Field<Integer> COUNT = DSL.field("COUNT(*) OVER ()", Integer.class);
List<Map<String, Object>> records = DSL.select(ACCESSORY.ID,
ACCESSORY.NAME,
ACCESSORY.TYPE,
ACCESSORY.PRICE,
BRAND.NAME,
COUNT.as("total"))
.from(ACCESSORY)
.innerJoin(BRAND).onKey()
.fetchMaps();
The ResultSet will contain a column called total which will be treated as a type java.lang.Integer. This works with PostgreSQL 9.6.
A detailed description of COUNT(*) OVER () can be found here: here.
How can i get a single object by using JPA Native query. I did some researches but all give an answer is use "getSingleResult", however it didn't return what i want to get. For example, what should i do if i want to get a count of table in my database and fetch it into a Integer.
This code below shows how i get this by using Sessison hibernate:
int check = Integer.parseInt((String) sessionF.createSQLQuery("select to_char(count(*)) as count from user_tables where table_name upper('TEST')").uniqueResult());
And what i hope to be fine in JPA:
int check = Integer.parseInt((String) getEntityManager().createNativeQuery("select to_char(count(*)) as count from user_tables where table_name upper('TEST')").getSingleResult());
Obviously, the code doesn't return what i want. Therefore, please help me to cope with this problem. Thank you !
With JPA you need to do like following
int num = ((Number)this.entityManager.createNativeQuery("select count(*) from your_table_name")
.getSingleResult()).intValue();
edited :
String name = this.entityManager.createNativeQuery("select t.name from your_table_name t limit 1").getSingleResult().toString();
you will got count with num object
Hope its will help to you.
In case of native query with multiple select expressions returns an Object[] (or List<Object[]>).
you can use below example code as per your requirement .
Query q = em.createNativeQuery("SELECT id,name FROM user WHERE id = ?1");
q.setParameter(1, userId);
Object[] result = (Object[])q.getSingleResult();
String id= result[0];
int name = result[1];
You might need to typecast the result Array values.
This may help someone.
To get a single object you can choose either of the below:
Method 1:
Query theQuery = entityManager.createNativeQuery("SELECT * from yourTable where yourTableId = (Select MAX(yourTableId) from yourTable where empId = ?1 and instId = ?2 GROUP BY empId,instId)",YourTableEntity.class);
theQuery.setParameter(1, empId);
theQuery.setParameter(2, instId);
System.out.println(theQuery.getSingleResult());
Method 2:
Query theQuery = entityManager.createNativeQuery("SELECT * from yourTable where empId = ?1 and instId = ?2 order by yourTableId DESC");
theQuery.setParameter(1, empId);
theQuery.setParameter(2, instId);
theQuery.setMaxResults(1); //Use if it makes good impact in complexity
System.out.println(theQuery.getResultList().get(0));
NOTE: For simplicity sake I just printed it. You may need to typecast.
Check if the time taken by method 1 or method 2 works better for you.
I am trying to get hibernate to return an object that has the lowest ID number that is greater than a certain value.
The code I have is returning an integer though, instead of the object.
Code
Criteria criteria = session.createCriteria(Story.class);
Criterion storyId = Restrictions.ge("id", 2);
ProjectionList projList = Projections.projectionList();
projList.add(Projections.min("id"));
criteria.add(storyId);
criteria.setProjection(projList);
List<?> storyResult = criteria.list();
session.close();
Story story = (Story) storyResult.get(0);
Right now this is returning "3" as an integer. 3 is the next available ID, but why is hibernate giving me back an integer instead of the object?
Thanks
By adding a Projection, you are essentially asking Hibernate to change your query from SELECT * FROM TABLE to a SELECT MIN(ID) FROM TABLE query. That's one of the features of a projection.
To achieve what you actually want to achieve, you need to use the projection to get the ID and then use another query to retrieve the object.
Criteria idQuery = session.createCriteria(Story.class);
idQuery.add(Restrictions.ge("id", 2));
idQuery.setProjection(Projections.min("id"));
int returnedId = criteria.uniqueResult();
Criteria storyQuery = session.createCriteria(Story.class);
storyQuery.add(Restrictions.idEq(returnedId);
Story story = (Story) storyQuery.uniqueResult();
return story;
I need to fetch the result of the following query but i am getting a typecast exception. Kindly help out!
SELECT COUNT(*) FROM ( SELECT DISTINCT a.PROPSTAT_CODE,a.PROPSTAT_DESC,a.PROPSTAT_TYPE FROM CNFGTR_PROPSTAT_MSTR a WHERE 1 = 1 )
My code is given below,
Query query = session.createSQLQuery(sqlQuery);
listRes = query.list();
int ans = ((Integer)listRes.get(0)).intValue();
Thanks in advance
Since you say that you are wrapping the above query in another query that returns the count, then this will give you want, without having to convert to any other data types.
Integer count = (Integer) session.createSQLQuery("select count(*) as num_results from (SELECT DISTINCT a.PROPSTAT_CODE,a.PROPSTAT_DESC,a.PROPSTAT_TYPE FROM CNFGTR_PROPSTAT_MSTR a WHERE 1 = 1)")
.addScalar("num_results", new IntegerType())
.uniqueResult();
System.err.println(count);
The trick is the call to "addScalar". This tells Hibernate you want the data type of "num_results" pre-converted to an Integer, regardless of what your specific DB implementation or JDBC driver prefers. Without this, Hibernate will use the type preferred by the JDBC driver, which explains why different answers here have different casts. Setting the desired result type specifically removes all guesswork about your returned data type, gives you the correct results, and has the added bonus of being more portable, should you ever wish to run your application against a different relational database. If you make the call to "list" instead of "uniqueResult" then you can assign the results directly to a List
Use long instead of int. Hibernate returns count(*) as long not int.
Query query = session.createSQLQuery(sqlQuery);
listRes = query.list();
long ans = (long)listRes.get(0);
Well.. I suppose this should work:
Query query = session.createSQLQuery(sqlQuery);
List listRes = query.list();
int ans = ((BigDecimal) listRes.get(0)).intValue();
Note: you need to import java.math.BigDecimal
List number=session.createSQLQuery("SELECT COUNT(*) FROM devicemaster WHERE ClientId="+id).list();
session.getTransaction().commit();
int ans = ((java.math.BigInteger) number.get(0)).intValue();
I make this query:
String query = FROM Account acc WHERE acc.id = ? OR acc.id = ? or acc.id = ?...
I have array of ids:
long[] accountIds= {327913,327652,327910,330511,330643};
Then I make
getHibernateTemplate().find(query, accountIds);
I see that the list of accounts I get back from this query is:
327652,327910,327913,330511,330643, obviously , ordered by id.
Any chance I get it back in the order I wrote the ids?
Will appreciate all the help
You may want to use Criteria and its addOrder.
Something like this:
DetachedCriteria cr = DetachedCriteria.forClass(entityClass);
//Add expressions or restrictions to your citeria
//And add your ordering
cr.addOrder(Order.asc("yourID"));
List<T> ls = getHibernateTemplate().findByCriteria(cr);
return ls;
You can't do it on query level.
You can sort after loading from db, something like this
long[] accountIds= {327913,327652,327910,330511,330643};
List<Account> afterHql = getHibernateTemplate().find(query, accountIds);
List<Account> sortedList = new ArrayList<Acount>();
for (long id : accountIds)
{
for (Account account : afterHql)
{
if (account.getId() == id)
{
sortedList.add(account);
}
}
}
It is not possible to fetch results by providing any entity in OR Query or Restrictions.in(). As by deafult when you fire this kind of query it will search for the results id wise. So it will give you results id wise only. You can change the order by using Criteria either in asc or desc. And if you want to have results as per you enter id, then second answer is the only option.
You can only order by column values returned by the query, in a sequence defined by the data type . Wouldn't it be better to pre-order the IDs you supply, and order the query result by ID, so they come out in the same order?