Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hibernate : Objects are getting saved without transaction Object

Tags:

java

hibernate

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?

like image 970
nobalG Avatar asked Apr 08 '17 17:04

nobalG


1 Answers

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");
like image 114
Avis Avatar answered Oct 04 '22 02:10

Avis