Hibernate has layered Architecture. It helps the user to handle without having to know the underlying APIs. The Hibernate uses the database and the configuration data to implement resolution services to the applications.
Hibernate is an object-oriented mapping tool for the java programming language and helps to present a framework for mapping an object-oriented model to a relational database.Are you seeking for Hibernate Training in Chennai? Join FITA to learn and improve your hibernate skills.
1. Configuration Object
The Configuration object is the first Hibernate object, created in the Hibernate application. It is created once during application initialization. It can describe the properties and configuration files required by the Hibernate.
The Configuration Object Provides Two Keys Components
Database Connection- Database connection is managed through one or more configuration file support by Hibernate. These Files are hibernated. properties and Hibernate.cfg.xml.
Class Mapping Setup- This Component is created in a connection between the Java Classes and Database Tables.
2. Session Factory
Session Factory is collected from Configuration object and it is the heavyweight object, it can be
used with various threads as the session factory object is threaded safe.
SessionFactory class is available under org.hibernate.SessionFactory package.
We would always need one instance of Session Factory per database, that our application is interacting with. If we have two different databases, we would create two session factory objects.
Gathering factory is a huge weight object, creation of the session factory object is a valuable service and approved to prepare it created at application start up or can be accessed using JNDI.
Below code, the snippet is to get a session factory object.
Configuration configuration = new Configuration().configure();
SessionFactory sessionFactory = configuration.buildSessionFactory();
Once the session factory object is achieved, the configuration purpose is no longer needed.
Session objects are generated by managing the Session factory and are a lightweight object. Session objects produce a combination with a relational database.
By design, we should generate a new gathering purpose when database communication is required. A session object describes a part of the work.
Session objects do not thread-safe and hence should not remain open for a long time.
Use the below code snippet to open a new Session object
Configuration configuration = new Configuration().configure();
SessionFactory sessionFactory=configuration.buildSessionFactory();
Session session = sessionFactory.openSession();
Use session.close() to close the session.
We can transfer an optional connection object as evidence to the openSession() system if we would need to communicate with the database user associated link. However, this approach does not recommend.
Classes whose situations are expected to endure in a database are known as persistence classes. Any case of the determined class can be in any one of the below three states.
Transactions
A transaction is a unit of work that is either all operations must execute or none of them. The transaction is single-threaded and short-lived objects.
The transaction is available under org. hibernate. Transaction package.
There are four important terms related to transactions are:
Atomic- As explained above, atomicity wants to make sure that either all the operations are within a transaction must be successful or none of them.
Consistent- This Property makes sure that data should be in a consistent state once the transaction is completed.
Isolated- This property provided various users to obtain the same set of data and each user's processing should be isolated from others.
Durable- The result of the business should be permanent once the transaction is completed to avoid any loss of data.
Learn Spring Hibernate Training in Chennai at FITA, improve your hibernate skill and increase your career level.