Hodglem Hodglem - 3 months ago 53
Java Question

Hibernate AutoCommit Example in Java

I just started learning Hibernate, but based on the reading I have done in the manual, the below code appears to be using auto-commit as commit() is not being explicitly called. All of the examples I found here show use of commit(). Since I am just learning I wanted to get confirmation from the community that I was thinking the right way.

this.session = HibernateUtil.getSessionFactory().getCurrentSession();
Transaction tx = session.beginTransaction();
session.clear();
entityReturned = (MultipleKeyTable) session.get(MultipleKeyTable.class, entityId);
session.close();


Edit
I should have mentioned that the
hibernate.connection.autocommit
is not defined in config and I believe that by default it is set to true.

Edit
I was wrong
hibernate.connection.autocommit
is set to false by default. Thanks Apostolos .

Answer

You can setup the autocommit mode at hibernate via hibernate.connection.autocommit property

please read the configuration documentation

Based on your updated question, you are not using autocommit since you didnt specify the autocommit in your configuration. You are also making a select query, so you wont see if something happens in your db. make some changes, save them and then check again. You need to commit your transaction in order to save the changes, or define autocommit = true at your properties.