Is there a difference between the two? For example within a hibernate transaction we can access the database, run some java code and then access the database again. We can't do that within a transaction done via SQL can we? Is this the difference?
Here a simple explanation: Queries are operations to CRUD (create (insert), update (set), read (select), delete (delete)) data inside a table. The transaction is more or less the process of a single or multiple statements/queries/operations getting executed.
Spring framework is useful for transaction management, dependency injection; aspect-oriented programming for applications whereas Hibernate framework is useful for object-relational persistence, access data layers, and query retrieval services for enterprise-level applications.
Transaction Interface in Hibernate In hibernate framework, we have Transaction interface that defines the unit of work. It maintains abstraction from the transaction implementation (JTA,JDBC). A transaction is associated with Session and instantiated by calling session. beginTransaction().
A transaction is a single unit of work. If a transaction is successful, all of the data modifications made during the transaction are committed and become a permanent part of the database. If a transaction encounters errors and must be canceled or rolled back, then all of the data modifications are erased.
The 2 directly relate to each other - a Hibernate transaction maps to and controls the JDBC (database) transaction.
You can do the same thing with direct JDBC / SQL, without Hibernate - though you'll need to call Connection.setAutoCommit(false)
to get started. Otherwise, by default, a commit is called after each statement - making each statement run in its own transaction.
Some additional details are available at http://docs.oracle.com/javase/tutorial/jdbc/basics/transactions.html.
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