Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

318
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda