Using Hibernate Criteria i am trying to achieve pagination but the problem is that for every page fetch i have to make two db calls one for results and another for total records count.
Is there any efficient way so that in a single db call i can get both the data or i can reduce db calls.
Criteria criteria=session.createCriteria(Student.class);
criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
criteria.add(Restrictions.ne("enquiryStatus", ENQUIRY.JOINED));
criteria.setMaxResults(10);
criteria.setFirstResult((paginate.getStartIndex()-1)*10);
criteria.setProjection(Projections.rowCount());
//here i need to fetch total row count and records
Yes you need the separate query to get the total result count.
Query aCountQuery = session.createQuery("select count(s.id) from Student s
where s.enquiryStatus != :enquiryStatus");
aCountQuery.setParameter("enquiryStatus", ENQUIRY.JOINED);
Long resultCount = (Long)aCountQuery.uniqueResult();
or
Criteria criteria=session.createCriteria(Student.class);
criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
criteria.add(Restrictions.ne("enquiryStatus", ENQUIRY.JOINED));
criteria.setProjection(Projections.rowCount())
Long resultCount = (Long)criteria.uniqueResult();
Update
Now you can use same Criteria for results with pagination and result count
Criteria criteria=session.createCriteria(Student.class);
criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
criteria.add(Restrictions.ne("enquiryStatus", ENQUIRY.JOINED));
criteria.setMaxResults(10);
criteria.setFirstResult((paginate.getStartIndex()-1)*10);
List<Student> students = criteria.list();
criteria.setProjection(null);
criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
Long resultCount = (Long)criteria.uniqueResult();
As far As I know there is no direct method to do so.As simple as you can do is write a small hql query(my choice) ,Along with your main criteria.
Number count = (Number) session.createQuery(
"select count(s.id) from Student s").uniqueResult();
Related
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 want to execute a query, get the results and then get the cursor to the next item if there is any. The only relevant post I found was: Objectify paging with Cursors
Is there a way of doing this without iterating through the items?
Query<User> query = ofy().load().type(User.class).limit(RecordLimit).filter("gameId", gameId);
//execute and get the results
List<User> users = query.list()
//get the cursor for the next user
String cursor = query.iterator().getCursor().toWebSafeString();
Look at these unit tests to make things a bit more clear.
Query is itself iterable - like a cursor.
You can directly do:
Query<User> query = blah;
for(User u: query) {
//u is next item from the "cursor"
}
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?
So let's say I have an ArrayList full of Products that need to be committed to the database via Hibernate. There are already a large number of Products in the database. Each product has an ID. Note this is NOT the PK that is autogenerated by Hibernate.
My questions is: what is the best way to detect conflicts with this ID? I am looking for a relatively efficient method of obtaining, from the the database, a List of Products that share an ID with any of the Products in my ArrayList. This is all in a single table called Products and the ID attribute is in column ProductID.
The way I've done it is grabbing a list of all Products in the database, and compared each one with each entry in my ArrayList - but that is seriously inefficient and I don't think it would work well with a larger database. How should it be done? Thanks. I say "relatively" efficient because efficiency is not the primary concern, but it shouldn't take noticeably long to test against a table of ~1000-5000 rows. Help?
EDIT* I'm very new to hibernate and below is the best I've come up with. How does this look?
for(long id : idList){ //idList just holds the IDs of each Product in my ArrayList
Query query = session.createQuery("select product from Product product where product.id = :id");
query.setLong("id", id);
for(int i = 0; i < query.list().size(); i++){
listOfConflictingProducts.add((Product) query.list().get(i));
}
}
I would call query.list() only once: otherwise the query could be rerun each time:
for(long id : idList){ //idList just holds the IDs of each Product in my ArrayList
Query query = session.createQuery("select product from Product product where product.id = :id");
query.setLong("id", id);
List result = query.list();
for(int i = 0; i < result.size(); i++){
listOfConflictingProducts.add((Product) result.get(i));
}
}
Or, more simply:
for(long id : idList){ //idList just holds the IDs of each Product in my ArrayList
Query query = session.createQuery("select product from Product product where product.id = :id");
query.setLong("id", id);
for(Object obj: query.list()){
listOfConflictingProducts.add((Product)obj);
}
}