Configuration
In any Hibernate application, the first Hibernate object you create is the Configuration object. The Configuration class, which is a part of org.hibernate.cfg package, activates hibernate framework and reads configuration and mapping files.
The Configuration object includes two essential components.
- Database Connection: Hibernate handles this through one or more configuration files. These are called hibernate.properties and hibernate.cfg.xml files.
- Class Mapping Setup: This component establishes the link between Java classes and database tables.
Demo Code
Configuration cfg=new Configuration(); // activate Hibernate framework
cfg.configure(); // Read config and mapping files, throw errors if syntactically wrong
Session Factory
A Session is used to establish a physical link with a database. The Session object is lightweight and intended to be instantiated each time a database interaction is required. A Session object is used to save and retrieve persistent objects and perform CRUD operations.
Because session objects are not normally thread safe, they should not be kept open for lengthy periods of time and should be generated and removed as needed.
Transaction
A transaction is a unit of work with the database that is supported by the majority of RDBMS. Hibernate's transactions are handled by an underlying transaction manager and transaction manager (from JDBC or JTA). By invoking the commit() function, the transaction object instructs the database to make the changes that occur as a result of the operation permanent.
This object is optional, and Hibernate applications may choose not to use it, instead managing transactions in their own application code.
Demo Code
Session session=factory.buildSession(); // Create the session.
Transaction tx=session.beginTransaction(); // Begin transaction.
tx.commit(); // Commit changes to the database.
Query
A Query instance is obtained by invoking the Session.createQuery() function.
Query objects retrieve data from the database and construct objects using SQL or Hibernate Query Language (HQL) strings. A Query instance is used to bind query parameters, limit the number of results returned by the query, and execute the query.
Criteria
The Session object is used to build the Criteria object. It's used to create and run object-oriented criterion queries and retrieve entities by assembling Criterion objects.
The Session is a Criteria factory. The factory methods on Restrictions are typically used to obtain criterion instances.
Frequently Asked Questions
How many layers are there in Hibernate architecture?
Hibernate's architecture is divided into four layers. Hibernate's high-level architecture, complete with mapping and configuration files. The Hibernate framework makes extensive use of objects such as session factory, session, transaction, and so on.
Describe the layout of Hibernate Architecture?
Hibernate offers a layered architecture that allows users to work without knowing the underlying APIs. Hibernate uses the database and configuration data to offer the application with persistence services (and persistent objects).
Is Session thread safe in Hibernate?
No, Session object is not thread-safe in Hibernate and intended to be used with-in single thread in the application.
What is lazy loading in Hibernate Architecture?
The Lazy setting determines whether child items are loaded while the Parent Object is loaded. You must achieve this by modifying the parent class's hibernate mapping file. Lazy = True (means not to load child) The sluggish loading of child objects is enabled by default.
Conclusion
Cheers on completing the blog! This blog discussed the hibernate architecture in detail and discussed the objectives of various core components of the hibernate framework.
If you want to explore more blogs on this topic, please follow these blogs especially curated for readers like you - JDBC, JDBC Connection, Guide to ORM, Spring Boot JDBC.
Recommended Reading, Data Warehouse Architecture
Refer to our Guided Path on Coding Ninjas Studio to upskill yourself in Data Structures and Algorithms, Competitive Programming, JavaScript, System Design, and many more! If you want to test your competency in coding, you may check out the mock test series and participate in the contests hosted on Coding Ninjas Studio! But if you have just started your learning process and are looking for questions asked by tech giants like Amazon, Microsoft, Uber, etc; you must look at the problems, interview experiences, and interview bundle for placement preparations.
Nevertheless, you may consider our paid courses to give your career an edge over others!
Happy Learning!