org.hibernate.Criteria.setFetchSize() doesn't work [duplicate] - java

This question already has answers here:
What is difference between setMaxResults and setFetchSize in org.hibernate.Query?
(3 answers)
Closed 7 years ago.
I have the below code and trying to limit the number of records retrieved. (LIMIT), but i always get all the rows. I see forums recommending me to use Query, but is not possible to achieve it using criteria ?
Criteria criteria = session.createCriteria(Application.class)
.add(Restrictions.gt("lastModifiedOn", applicationLastRunTime))
.add(Restrictions.eq("lead", false))
.addOrder(Order.asc("lastModifiedOn"));
criteria.setFetchSize(40);
criteria.list()

try to use:
public Criteria setMaxResults(int maxResults)
Set a limit upon the number of objects to be retrieved.
Parameters:
maxResults - the maximum number of results
Returns:
this (for method chaining)
Criteria criteria = session.createCriteria(Application.class)
.add(Restrictions.gt("lastModifiedOn", applicationLastRunTime))
.add(Restrictions.eq("lead", false))
.addOrder(Order.asc("lastModifiedOn"));
criteria.setMaxResults(40);
criteria.list()
more info: What is difference between setMaxResults and setFetchSize in org.hibernate.Query?

Related

How do i set double query of Firebase Realtime Database? [duplicate]

This question already has answers here:
Query based on multiple where clauses in Firebase
(8 answers)
Closed 3 years ago.
From below query I get the data in order of timestamp
Query Users = FirebaseDatabase.getInstance.getReference.child("Users").orderbychild("timestamp");
Now all children are ordered by time.
But how do I filter this query by CurrentUserId?
Query CurrentUser = Users.orderbychild("CurrentUserId");
Firebase does not support multiple orderbychild method. What you can do is, first fetch records from Firebase with current user id
Query Users = FirebaseDatabase.getInstance.getReference.child("Users").orderbychild("CurrentUserId");
Then do timestamp sorting at client end.

using postgresql NOT IN statement [duplicate]

This question already has answers here:
PreparedStatement IN clause alternatives?
(33 answers)
Closed 3 years ago.
i have this query in spring:
private String SQL_Clear_Deleted_Options = "DELETE FROM vote_votes WHERE poll_id=? AND option_id NOT IN ?"
my problem is with second ?. the correct form would be (id1,id2,id3,...). how can i pass a string like cl="0,1,2,3,6" to this query?
i'm using jdbcTemplate. so it would be
jdbcTemplate.update(SQL_Clear_Deleted_Options, id,cl)
what should be cl?
You can use setArray of PreparedStatement to set multiple values.
https://docs.oracle.com/javase/7/docs/api/java/sql/PreparedStatement.html#setArray(int,%20java.sql.Array)
And the array should be of the correct type, so you could create it with the Connection.createArrayOf function:
https://docs.oracle.com/javase/7/docs/api/java/sql/Connection.html#createArrayOf(java.lang.String,%20java.lang.Object[])
You can see a lot of discussions about that topic on SO already, e.g.:
How to use an arraylist as a prepared statement parameter
Edit: Forgot to mention: The JdbcTemplate should set the Parameter correctly when the Array is given as parameter.

MongoDB/Java: most efficient way to get values from query [duplicate]

This question already has answers here:
Mongodb Java - How to return restricted fields with find() or findOne()
(4 answers)
extracting values from HashMap
(5 answers)
Closed 3 years ago.
My MongoDB documents look something like this:
{
"person_id": 12345,
"first_name": "John",
"last_name": "Doe",
...
}
I now want a query that gives me a Collection<Long> of person_ids, with some filters applied. Filtering works fine, but MongoCollection.find returns a MongoCollection of Documents.
Is there an implicit way to just get the values (12345), not key-value-pairs ("person_id" : 12345)?
Right now I'm just filling a new Collection by iterating over the result, extracting the values one by one. If there's no other way to do it, is there any point in using a projection to restrict the returned fields to person_id, or is that just overhead?

Passing variable into IN clause in java [duplicate]

This question already has answers here:
Hibernate Criterion IN Clause 1000 break up
(3 answers)
Java Oracle exception - "maximum number of expressions in a list is 1000"
(7 answers)
Closed 5 years ago.
I am constructing a hibernate query and I pass a list of String values into the IN clause. That list sometimes happens to be more than 1000 values so I receive an error. I have looked up some solutions like breaking that clause into smaller ones or making temporary table, but none of them showed how actually it is better to work with variable list.
So if I had something like:
SELECT * FROM MY_TABLE WHERE NAME IN (list).
What would be the best way to handle this?

SQL in clause with items > 1000 [duplicate]

This question already has answers here:
How to load a large number of strings to match with oracle database?
(3 answers)
Closed 6 years ago.
0I have this method:
public List<Product> getProductsByListIds(List<Long> ids) {
String query = "from Product pr where pr.id in(:ids)";
List<Product> products= (List<Product>) getSession().createQuery(query)
.setParameterList("ids", ids).list();
return products;
}
This method is OK, my only problem is when the ids.size() >1000
I'm trying to find a convincing solution to this problem.
My advice would be to take a step back and look at the design and what you're trying to achieve, passing hundreds of parameters into an SQL statement is not going to be very efficient and I'd be surprised if it's the most elegant solution to your requirement.
Without knowing more about how this method is called and where this list of ids comes from it's difficult to give concrete advice, however I would recommend that you look into using joins if possible.

Categories

Resources