Hodglem Hodglem - 1 year ago 204
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();
entityReturned = (MultipleKeyTable) session.get(MultipleKeyTable.class, entityId);

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

I was wrong
is set to false by default. Thanks Apostolos .

Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download