Why does Hibernate ORM require a transaction


Hibernate is a framework for mapping objects on relational databases for the Java programming language - it is also known as the Object Relational MappingTool. Hibernate is implemented in the Java programming language and is available under the following. Link as open source for free use. Hibernate uses the concept of reflection in order to determine so-called meta-information about the structure of objects and the underlying classes during the runtime of a software, which is then mapped to the tables of a database.

Hibernate implements the Java Persistence API (JPA) - a standard API for object-relational mapping - and offers additional functionalities. This also includes database queries with the Hibernate Query Language (HQL) and the programmatic option for defining queries using the Hibernate Criteria API. Hibernate can be integrated into a wide variety of software and system architectures such as rich clients, client servers, web applications, J2EE or Java Enterprise Edition (JEE). Hibernate was developed with the support of JBoss Inc. and is also integrated in the JBoss application server. A ported version of Hibernate is available for the .NET platform with NHibernate. Fluent NHibernate and LINQ to NHibernate are required, which are listed under the following items. Links are available.

Object Relational Mapping (ORM) for structural mapping on relational databases

Object Relational Mapping (ORM) is a technique in which the objects of software created on the basis of object-oriented programming are mapped onto the structures of a relational database. Incidentally, this technology is widely used, since the use of purely object-oriented databases, OODB, has not yet become established in practice. Since the relational formations cannot always be connected 1: 1 with the object-oriented structures, this is an approach for tools that support OR mapping. In addition to commercial tools such as Oracle Toplink, Hibernate is one of the best-known and most developed persistence frameworks available as open source. Its use not only enables the connection to most relational database systems but also aspects such as cluster capability, caching and session management are taken into account. Hibernate is characterized by both extensive tool support and high performance.

Since most of the available databases can be used with Hibernate, access to the respective database is possible because Hibernate supports different database dialects. The information required to access the specific database is summarized in a separate configuration file, which is then loaded by Hibernate when the SessionFactory class is instantiated. The configuration file also contains the names of the classes for whose instances the persistence is to be generated at runtime. Hibernate uses similar mechanisms as is the case with the well-known Java Data Objects (JDO). For example, the type of ID generation or the inheritance hierarchy can be defined. Hibernate also offers functions such as a high-performance second level cache or transaction management similar to JDO. In addition to the SessionFactory class already mentioned, Session and Transaction are still central classes of the Hibernate framework.

Hibernate for ephemeral objects

The hibernate sessions organize the handling of the persistent objects and are therefore of central importance. Every access to a database is a session and thus connects the Java application with the database. The states of the business objects are managed, and the session is also responsible for their persistence, loading and deletion. In order to change persistent data, you have to work with transactions. It makes sense to have a maximum of one transaction object generated by a session. There is no support for nested transactions. As soon as several transactions are open, they run completely isolated from each other. Each session has its own cache, which ensures the corresponding performance. Hibernate supports various implementations of transactions. The mechanism of the database used is used with JDBC transactions. Java Database Connectivity (JDBC) is part of the Java Development Kit (JDK) and enables access to relational databases. Alternatively, the transaction mechanism of the application server can be used with the Java Transaction API (JTA).

There are various options for querying databases with the Hibernate Query Language (HQL) and with queries and criteria. The syntax of HQL is similar to that of SQL and the JDO query language JDOQL. In addition, the query is implemented using SQL statements and the Criterias API offers an object-oriented interface for use in a Java application. Since version 3 of Hibernate it is not only possible to read out data and objects, but these can also be modified or deleted with a query.

Compared to comparable persistence frameworks such as the commercial Oracle Toplink or iBATIS, which is one of the more lightweight frameworks supported by Spring, Hibernate is characterized by an advanced state of development. In addition, good documentation and comprehensive tool support make the implementation of Hibernate much easier. Hibernate can be used directly by Java applications and servlet engines as well as integrated into an application server.