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

  • SEARCH
  • Home
  • 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 7569389
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T15:09:13+00:00 2026-05-30T15:09:13+00:00

Which is the best method(performance wise) to delete an object if only its id

  • 0

Which is the best method(performance wise) to delete an object if only its id is available.

  1. HQL. Will executing this HQL load the SessionContext object into hibernate persistence context ?

    for(int i=0; i<listOfIds.size(); i++){
        Query q = createQuery("delete from session_context where id = :id ");
            q.setLong("id", id);
            q.executeUpdate();
    }
    
  2. Load by ID and delete.

    for(int i=0; i<listOfIds.size(); i++){
        SessionContext session_context = (SessionContext)getHibernateTemplate().load(SessionContext.class, listOfIds.get(i));
        getHibernateTemplate().delete(session_context) ;
    }
    

Here SessionContext is the object mapped to session_context table.
Or, well off course is there an all together different and better approach ?

  • 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-30T15:09:14+00:00Added an answer on May 30, 2026 at 3:09 pm

    Out of the two, the first one is better, where you will save memory. When you want to delete the Entity and you have the id with you, writing a HQL is preferred.

    In you case there is a third and better option,

    Try the below,

    //constructs the list of ids using String buffer, by iterating the List.
    String idList = "1,2,3,....."
    
    Query q = createQuery("delete from session_context where id in (:idList) ");
    q.setString("idList", idList);
    q.executeUpdate();
    

    Now if there are 4 items in the list only one query will be fired, Previously there would be 4.

    Note:- For the above to work, session_context should be an independent table.

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

Sidebar

Related Questions

Which of the following has the best performance? I have seen method two implemented
This question originally asked which is the best method for uploading files via SFTP
Which method provides the best performance when removing the time portion from a datetime
Using SQL Server, which is the fastest or best practice method to use for
i want to know which parsing method is best to use among Simple XML
Which method do you think is the best. Use the System.IO.Packaging namespace? Use interop
There is a related question to this: What's the best method to pass parameters
How can I find out which method is best for a situation? Can anybody
quandry is - which of the following two method performs best Goal - get
What is best way to write query performance wise. e.g. i can write query

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.