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 8158851
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T17:44:42+00:00 2026-06-06T17:44:42+00:00

I have a MySQL database includes a table named ‘Task’. and I used Hibernate

  • 0

I have a MySQL database includes a table named ‘Task’. and I used Hibernate to map the data to the database.

it includes these fields : id, user_id, project_id, deliverable_id, activity_id, …

When I retrieve Tasks from Db, I need to sort them by Project at first, and then by Deliverable and finally by Activty.

         Session session = HibernateUtil.getSessionFactory().getCurrentSession();
            session.beginTransaction();
            List<Task> tasks = (List<Task>)session.createQuery(
"from Task as t where t.user.username = :name order by t.project.key ASC").setString("name", username).list();
            orderByDeliverable(tasks);
            orderByActivity(tasks);



    private void orderByDeliverable(List<Task> tasks) {
       Collections.sort(tasks, new Comparator<Task>(){   
          public int compare(Task t1, Task t2) {
             if(t1.getProject().getId() == t2.getProject().getId()) 
               return t1.getDeliverable().getKey().compareToIgnoreCase(t2.getDeliverable().getKey());
             return 0;
          }
       });
    }

    private void orderByActivity(List<Task> tasks) {
        Collections.sort(tasks, new Comparator<Task>(){  
           public int compare(Task t1, Task t2) {
              if(t1.getProject().getId() == t2.getProject().getId()) 
             if(t1.getDeliverable().getId() == t2.getDeliverable().getId()) 
                return t1.getActivity().getKey().compareToIgnoreCase(t2.getActivity().getKey());
            return 0;
           }
       });
    }

As you see in the code I used two methods to sort them by deliverable and activity, but I sort them by project in the Query.

I am not good in Query writing, do you know any way that I can do all sorting stuff in the query and get the same result ?

  • 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-06-06T17:44:43+00:00Added an answer on June 6, 2026 at 5:44 pm

    I found the answer. I just need to use this query:

    tasks = (List<Task>)session.createQuery("from Task as t where t.user.username = :name 
    order by t.project.key ASC, t.deliverable.key ASC, t.activity.key ASC")
    .setString("name", username).list();
    

    It worked the same for me.

    Actually I looked for this solution to speed-up the application that I have developed.
    I tested that which one is faster. The result was the same. It does not speed-up the application.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So I have a mySQL database with a 'users' table which includes a username,
I have a MySql database on my server with a table named table_1. However
I have these codes, used to update mysql database AIS.php-used to input pnum and
So. I have a table of data, in a MySQL database, eg: Artist Name
I have mysql database structure like below: CREATE TABLE test ( id int(11) NOT
I have a MySQL database, and a particular table in that database will need
I have a MySQL database with a fairly large table where the products are.
I have a database with a table which includes among others, one column of
I have a form that uses ajax to submit data to a mysql database,
I have a site that displays data on travel products from a MySQL database.

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.