Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

315
Views
Hibernate : Objects are getting saved without transaction Object

I am having following piece of code for saving the object in the database,

import java.util.HashMap;
import java.util.Map;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import org.hibernate.Session;
import org.hibernate.Transaction;
import model.Hibernatetest;
public class Test 
{
   static EntityManagerFactory objEntityManagerFactory;
   static Session objSession;
   static Transaction objTransaction;
   public static void main(String[] args) 
   {
        objSession = (Session) 
        objEntityManagerFactory.createEntityManager().getDelegate();
        //objTransaction = objSession.getTransaction();
        //objTransaction.begin();
        Hibernatetest obj = new Hibernatetest();
        obj.setName("Nobal");
        obj.setAddress("wlfjegtjwdfhdg");
        objSession.save(obj);
        obj.setName("235611111");
        objSession.flush();
       //objTransaction.commit();
        objSession.close();
    }

    public static void getConnection()
    {
        Map<String,String> properties = new HashMap<String,String>();
            properties.put("hibernate.connection.username", "root");
            properties.put("hibernate.connection.password", "root");
            properties.put("hibernate.connection.driver_class", "com.mysql.jdbc.Driver");
            properties.put("hibernate.connection.url", "jdbc:mysql://localhost:3306/hibernatetest");
            properties.put("hibernate.dialect", "org.hibernate.dialect.MySQLDialect");

         objEntityManagerFactory = Persistence.createEntityManagerFactory("Hibereg", properties);
    }
}

and the Persistence file is as follows:

<persistence xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/persistence" version="2.0">
<persistence-unit name="Hibereg">
<class>entities.Hibernatetest</class>
</persistence-unit>
</persistence>

Case 1:

The problem is when I run the above code using the hibernate-core-5.2.8.jar, and don't create any transaction object(you can see I commented the code related to transaction) then the above code gives me following exception.

'TransactionRequiredException' no transaction is in progress.'

However if I uncomment the code for the transaction, everything works well.

Case 2:

But when I run the code using hibernate-core-4.1.4.jar, by commenting the code for the transaction in code snippet provided above, It gives no such exception. That is , it never complains for the absence of transaction object and saves the object in the database.. What is going on in here?

I read over internet that transaction object is must for performing inserts and updates, (I am not sure about selects) but how the second scenario is executing successfully?

about 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Since Hibernate 5.2 this functionality is included inline to JPA specification that does not allow any update flush outside of an transaction boundary. In order to override it pls use following on your hibernate settings:-

properties.put("hibernate.allow_update_outside_transaction", "true");
about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!