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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T20:11:18+00:00 2026-06-15T20:11:18+00:00

In a non-transactional environment, multiple rows can be deleted in a batch in Hibernate

  • 0

In a non-transactional environment, multiple rows can be deleted in a batch in Hibernate something like the following.

Session session=HibernateUtils.getSessionFactory().getCurrentSession();
session.beginTransaction();

String temp[]=request.getParameter("delIds").split(",");
int len=temp.length;
countryService.delete(temp);

Configuration configuration=new Configuration();
configuration.setProperty("hibernate.jdbc.batch_size", "50");

for(int i=0;i<len;i++)
{
    Country c=(Country)session.get(Country.class, new Long(temp[i]));

    if(i%50==0)
    {
         session.flush();
         session.clear();
    }

    session.delete(c);
}

//session.flush();
session.getTransaction().commit();

50 rows will be considered for deletion per batch.


How can the same be made possible in a transactional environment?

@Service
@Transactional(readOnly = true, propagation=Propagation.REQUIRES_NEW)

final public class CountryDAO implements CountryService
{
    private SessionFactory sessionFactory;

    public void setSessionFactory(SessionFactory sessionFactory)
    {
        this.sessionFactory = sessionFactory;
    }

    @SuppressWarnings("unchecked")
    @Transactional(readOnly=false, propagation=Propagation.REQUIRES_NEW)
    public void delete(String[] id)
    {
        int len=id.length;
        Session session=sessionFactory.getCurrentSession();

        for(int i=0;i<len;i++)
        {
            Country country=(Country)session.get(Country.class, Long.valueOf(id[i]));
            session.delete(country);
        }
    }
}

This works but how to delete rows in a batch with specific rows per batch so that a stream towards the database can be minimized?


I could do it something like the following.

@SuppressWarnings("unchecked")
//@Transactional(readOnly=false, propagation=Propagation.REQUIRES_NEW)
public void delete(String[] id)
{
    int len=id.length;
    Session session=sessionFactory.openSession();       //<----------------
    Transaction transaction = session.beginTransaction();

    Configuration configuration=new Configuration();
    configuration.setProperty("hibernate.jdbc.batch_size", "50");

    for(int i=0;i<len;i++)
    {
        if(i%49==0)
        {
            session.flush();
            session.clear();
        }
        Country country=(Country)session.get(Country.class, Long.valueOf(id[i]));
        session.delete(country);
    }
    transaction.commit();
    session.close();
}

According to the answer below.

  • 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-15T20:11:20+00:00Added an answer on June 15, 2026 at 8:11 pm

    You can use a “batch” update command that includes delete in the HQL.

    http://docs.jboss.org/hibernate/orm/3.3/reference/en/html/batch.html#batch-direct

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

Sidebar

Related Questions

Is there an API for loading a large # of non-transactional rows directly into
I have a WCF service that posts messages to a private, non-transactional MSMQ queue.
I'm trying to make an application with Spring3 MVC and Hibernate, but something I'm
What is this error about? No Hibernate Session bound to thread, and configuration does
I have an ASP.NET MVC app (non-transactional, social) I am workong on. I started
I have 2 non-transactional tables. I want to perform an 'insert' on one and
I get the following error when i call my DAO with Spring + hibernate
How can an encapsulate non-database actions into transactions? For example, say I have some
I have an ERP application with about 50 small lookup tables containing non-transactional data.
EntityManager is not thread-safe by definition. Servlets specs says that in non-distributed environment and

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.