1. What is hibernate java? Deliver a practical example.
The Hibernate framework for Java streamlines database interactions by mapping Java objects to tables.
Example:
- Session session = sessionFactory.openSession();
- User user = (User) session.get(User.class, 1);
2. Why do you use Hibernate in Java over JDBC?
Compared to JDBC, ORM offers the following benefits:
- The development of applications is quick.
- Handling of transactions.
- Automatically generates the key.
- SQL query details are concealed.
3. What are the core interfaces of Hibernate?
The Hibernate framework’s primary interfaces are:
- Configuration: Used to set up external resources and database properties to hibernate.
- Session Factory: Also referred to as the second-level cache, this component is utilized to create Session instances. It has an application-level scope.
- Session: The connection object needed to connect with the database is stored in the session. Another name for it is the level cache that keeps the object alive.
- Query: Its API runs and modifies queries and supports Native SQL and HQL (Hibernate Criteria Query Language).
- Criteria: One of the criteria was the ability to generate queries programmatically using entity properties.
- Transaction: It manages transactions with hibernation and serves as a committable or rollbackable unit of work.
4. Describe the architecture of hibernation.
Various interfaces, including Configuration, SessionFactory, Session, Transaction, and others, make up the Hibernate architecture.
5. Why is Hibernate better than JDBC in Java?
By managing object-relational mapping (ORM) and cutting down on boilerplate code and SQL, Hibernate streamlines database interactions. Additionally, it offers features that JDBC does not, including caching, automated table formation, and query optimization.
6. List some of the databases supported by Hibernate.
Among the databases that Hibernate supports are:
DB2, MySQL, Oracle, FrontBase, HSQL, PostgreSQL, Sybase SQL Server, and Informix Dynamic Server
7. What is Hibernate ORM.
Java objects are mapped to relational database tables using the Hibernate ORM (Object-Relational Mapping) framework. By managing SQL queries and transactions automatically, it streamlines database processes. By controlling Java object persistence in a relational database, Hibernate eliminates human JDBC programming requirements.
8. Why use Hibernate over using SQL directly?
By managing SQL creation and transactions automatically, Hibernate minimizes boilerplate code. It provides object-oriented mapping, which facilitates code management and maintainability. Furthermore, Hibernate offers characteristics that raw SQL does not directly provide, such as database independence, slow loading, and caching.
9. What makes Hibernate in Java preferable to JDBC?
By providing automatic object-relational mapping (ORM) and minimizing boilerplate code, Hibernate abstracts away the complexities of raw SQL. Its supported features include lazy loading, caching, and automatic table building. Database independence is another feature of Hibernate that allows users to switch databases easily and without modifying the application code.
10. How to join multiple tables in an HQL query in Hibernate?
Using the JOIN or LEFT JOIN keyword with entity relationships allows HQL queries to join several tables. As an illustration:
SELECT e FROM Employee e JOIN e.department d WHERE d.name = ‘HR’
11. Where should you use the Hibernate framework?
Java applications that need effective database interaction—particularly for intricate object-relational mapping (ORM)—are the ideal candidates for Hibernate. It is perfect for business applications that require database independence, caching, and scalability.
12. When does Hibernate send updates to the database?
When Session.flush() is explicitly called or a transaction commit occurs, Hibernate updates the database. It guarantees that the database and the in-memory state are in sync.
13. What distinguishes a JPQL from a native query?
Complete control over queries is provided by a native query, which employs raw SQL unique to the database. Database independence, object-oriented syntax, and entity object functionality are features of JPQL (Java Persistence Query Language).
14. What distinguishes HQL written in Hibernate from SQL queries?
SQL queries communicate directly with database tables and columns using raw SQL syntax. Using object-oriented syntax, HQL (Hibernate Query Language) manipulates Hibernate entities and their properties. SQL is database-specific, but HQL is database-independent.
15. Describe the function of Hibernate’s Session and Session Factory.
A heavyweight object called the SessionFactory connects to the database and builds and maintains Session instances. A lightweight, single-threaded object called the Session is used to manage transactions and carry out CRUD tasks. Session is used for each transaction or request, whereas SessionFactory is usually produced once.
16. How can we add criteria to a SQL query?
Hibernate’s CriteriaBuilder and CriteriaQuery APIs allow you to add criteria to a SQL query. Programmatically defining conditions enable the design of dynamic queries.
Example:
CriteriaBuilder cb = session.getCriteriaBuilder();
CriteriaQuery<User> cq = cb.createQuery(User.class);
cq.where(cb.equal(root.get(“name”), “John”));
17. How is the Hibernate dialect property used?
The hibernate dialect attribute specifies the type of database used to generate the queries.
18. What is a Session?
It keeps the database and the hibernate program connected. It offers functions like persist(), update(), delete(), load(), get(), and others for storing, updating, deleting, or retrieving data from the database.
It has factory methods that return instances of Query, Criteria, and Transaction, making it a factory of these types of objects.
19. What distinguishes the session.persist() function from session.save()?
i) save(): yields the instance’s serializable identifier
Syntax: public Saveable serializable (Object o)
ii)persist (): Since its return type is void, it returns nothing.
Public void persist(Object o) is the syntax.
20. What makes the merge and hibernation update techniques different?
Update (): makes a detached entity manageable by reattaching it to the session. An exception will be raised if the entity is already in the session.
Merge (): Transfers a detached entity’s state to a managed entity, either new or old. It is safer to utilize if the entity is already in the session or altered elsewhere.
21. What are annotations in Hibernate?
The answer is that Hibernate annotations replace XML mapping files by providing info directly in Java classes.
22. In what conditions is the item in hibernation?
The object (instance) in hibernation can be in one of three states.
- Transitory: If an object has just been formed, has no primary key (identifier), and isn’t connected to a session, it’s in a transitory state.
- Persistent: An object is considered persistent if it is currently in an open session and its instance has been recently saved or retrieved from a database.
- Detached: If a session is closed, the object is detached. If you use the lock() or update() methods after the object has been detached, it enters a persistent state.
23. What is HQL, or Hibernate Query Language?
One type of object-oriented query language is Hibernate Query Language. It is similar to a structured query language called SQL.
HQL’s primary benefit over SQL is:
- Writing a query doesn’t require you to alternate between SQL and Java code.
- Independence of databases
- Writing an inquiry is easy.
24. How does first-level cache vary from second-level cache?
First Level Cache:
By default, it is activated.
Session and First Level Cache are related.
Second Level Cache:
By default, it is not activated.
SessionFactory is linked to the second-level cache.
25. What are some typical Hibernate performance improvement strategies?
- Turn on caching.
- Make use of batch fetching.
- Steer clear of needless lazy loading.
- Optimize your SQL and HQL queries.
- Make use of pagination and projections.