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>
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.
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?
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");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With