Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

316
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda