Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7073853
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T05:58:47+00:00 2026-05-28T05:58:47+00:00

I have an project using Hibernate. This has a class named Question . Each

  • 0

I have an project using Hibernate.
This has a class named Question.
Each Question has a difficulty level (1,2,3);
Each Question also has an attribute is field (a,b,c,d,e,f,g,h,i,k);

Suppose I have 100 questions. I want to get randomly 20 question with conditions:

  • 7 questions in level 1
  • 7 questions in level 2
  • 6 questions in level 3.
  • Each field have at least 1 question

Thanks to all ^^

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T05:58:48+00:00Added an answer on May 28, 2026 at 5:58 am

    First of all, I think this problem is too difficult to be solved by using a Hibernate query, or even a set of Hibernate queries.

    Here’s how I would do:

    • load all 100 questions in memory
    • create a Map<Field, List<Question>>, and shuffle all the lists in this map
    • for each field, take the first question of the corresponding list that has an acceptable level
    • Once you have one question in every field, take all the remaining questions, put them in a list, shuffle the list, and iterate over the list. Each time a question has a level which is acceptable, take it. Do that until you have your 20 questions.

    This should work if, for each field, you’re guaranteed to have at least a question for every level. If it’s not the case, then it’s more difficult.

    If you have much more questions that 100, and they can’t be loaded in memory, you could use the same kind of algorithm, but use random queries to select the questions:

    • issue one query per field to find a question in each field. The where clause should only accept the given field, and should only accept the levels which are still acceptable.
    • issue a query to find N questions randomly (with N being equal to 50, for example, and with a where clause which only accepts the remaining levels), and take the first 10 acceptable questions. If there are less than 10 acceptable questions, try again. There should be at most 3 such queries. Make sure your where clause rejects the IDs of the questions which have already been loaded.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using Nhibernate for my ORM. I have a class Control that has
I have some web-project (Spring MVC + Hibernate). For hibernate objects I am using
I am using Hibernate with my Spring MVC project. Lets say my model has
I have just restarted a project in which I am using NHibernate. The project
I have a project using ASIHTTP to multi download files from a site when
We have a project using FluentNibernate to map the entities. Now I need to
I have a test project using MbUnit and TestDriven.Net. If I right-click on an
I have deploy my project using project:deploy --go. Now i have my folders apps,
We have an old project written using Managed C++ syntax. I would like to
I have created a setup project using Visual Studio 2008. After the application is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.