how to retrieve the data and set into a list upto list size 10

Hi Experts,

Can some help me with code logic

I have to fetch some data from db and i will convert object []  into list.

if listsize is less than 10 (list.size()<10)

i have to execute another query to fetch the data from db and convert this data into list

finally i have to set this list data into old list

Example:
1.from first query if i fetch data upto 5reocords  then
2.rest of the records i will fetch it from second query
3.finally i have to set all 10 records into one list

Can some one help me with sample code

Thanks,
LVL 2
srikoteshAsked:
Who is Participating?
 
srikoteshAuthor Commented:
With this approach there is performance issue

while fetching second query
let suppose second query fectch 100 records
we are taking only less than 10 records
as per above as u mentioned.

i want to set limit at dao layer itself while hitting the DB.
I have a common method in dao layer with pagenation limit with 10 records
i will pass recordsToAdd value to that method and i will limit here itselft w/o fetching all the records from DB.
Please find the attachment.

Example:
recordsToAdd  = 17
first time it will fetch 10 records and  then 7 records
DAO.TXT
0
 
Mark BullockQA Engineer IIICommented:
If you're asking how to append one list to another, you would do it like this:
if (list.size() < 10) {
   list2 = //fetch more records
   //append the second list to the first
   list.addAll(list2);
}

Open in new window

0
 
srikoteshAuthor Commented:
HI

FROM first query i may get 10 records some time.so that time i will not use second query.
if first query fetch only 6 records
i have to fetch rest of records from second query but second query will give n no of records.
i have to get only 4 records i dont want rest of the records from second query.
0
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

 
Mark BullockQA Engineer IIICommented:
Add as many records as you need from the second query
int recordsToAdd = 10 - list.size();
if (recordsToAdd > 0) {
   list2 = //fetch more records
   //append the second list to the first
   for (Object element : list2) {
      list.addAll(element);
      recordsToAdd--;
      if (recordsToadd == 0) {
         break;
      }
}

Open in new window

0
 
srikoteshAuthor Commented:
is it possible to set limit in the above attached method

or

i have to write a query with limit
0
 
Mark BullockQA Engineer IIICommented:
setMaxResults should work.
0
 
srikoteshAuthor Commented:
thanks
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.