Posts

Showing posts from January, 2024

8. Hibernate Interview Questions

  || Hibernate Interview Question || 1. What is ORM in Hibernate? Hibernate ORM stands for Object Relational Mapping. This is a mapping tool pattern mainly used for converting data stored in a relational database to an object used in object-oriented programming constructs. This tool also helps greatly in simplifying data retrieval, creation, and manipulation. 2. What are the advantages of Hibernate over JDBC? - The advantages of Hibernate over JDBC are listed below: - Clean Readable Code: Using hibernate, helps in eliminating a lot of JDBC API-based boiler-plate codes, thereby making the code look cleaner and readable. - HQL (Hibernate Query Language): Hibernate provides HQL which is closer to Java and is object-oriented in nature. This helps in reducing the burden on developers for writing database independent queries. In JDBC, this is not the case. A developer has to know the database-specific codes. - Transaction Management: JDBC doesn't support implicit transaction management. ...

7. Batch Processing

 Batch Processing - Batch Processing: What Does it Mean?    Batch processing is a way to efficiently process a large amount of data in chunks or groups, instead of one piece at a time. This is particularly useful when dealing with a large dataset. -  Batch Processing in Hibernate: Why Use It?    When you're dealing with a lot of data, doing database operations one at a time can be slow and resource-intensive. Batch processing allows you to group multiple operations together and send them to the database in a single go, reducing the overall time and resources needed. -  Saving Multiple Objects    Imagine you have a Java program using Hibernate, and you want to save a hundred new objects to the database. Instead of saving them one by one, you can use batch processing to save, let's say, 20 objects at a time. This means you're making fewer trips to the database, making the process faster. - Advantages:    - Performance Improvement: Bat...

6. Hibernate Query Language (HQL) & Caching in Hibernate

  Hibernate Query Language (HQL) Hibernate Query Language (HQL) is a powerful and flexible way to interact with a relational database using Hibernate, which is a popular Object-Relational Mapping (ORM) framework in Java.  1. HQL - Hibernate Query Language:    - HQL is like SQL, but it's designed to work with your Java objects, not directly with the database tables. It allows you to write queries using the names of your Java classes and properties, rather than database tables and columns. 2. Database Interaction - In traditional database operations, you use SQL (Structured Query Language) to interact with the database. SQL is a language that is specific to the database you are using (e.g., MySQL, PostgreSQL, Oracle). 3. Object-Relational Mapping (ORM): Hibernate helps you work with databases in a more object-oriented way. Instead of thinking in terms of database tables and SQL queries, you can work with Java objects that map to your database tables. Caching in Hiberna...

5. JPA Relational Mapping & Fetch Types & Cascadding

 RelationShip  -OneToOne -OneToMany -ManyToOne -ManytoMany Fetch Types 1. FetchType.LAZY  2. FetchType.EAGER In Hibernate, the "fetch type" determines how associated data should be loaded from the database.  It's like specifying whether you want to load related data eagerly (at the same time as the main entity) or lazily (only when needed). There are two main fetch types in Hibernate: 1. Eager Fetch Type    - When you fetch data eagerly, Hibernate loads the main entity along with its associated data immediately.    - It's like ordering a combo meal at a restaurant – you get the burger and fries together, even if you're not going to eat the fries right away.    - Eager loading can lead to loading more data than necessary, which may impact performance if not used carefully. 2. Lazy Fetch Type:    - When you fetch data lazily, Hibernate loads the main entity first and only loads associated data when explicitly requested. ...

4. Data Fetching Methods & @Embeddable annotation in Hibernate

  Data Fetching from Database In Hibernate, the `get` and `load` methods are used to retrieve an object from the database based on its primary key. Both methods are part of the `Session` interface in Hibernate. 1. get() method: - The get() method is a part of the Hibernate Session interface. - It retrieves an entity(persistence) object from the database by its primary key (ID). -  It takes the class of the object to be retrieved and the primary key of the object as arguments. - get() method only hits the database if the object is not present in the session cache.   - get() method returns null if there is no object present in the database.    Example:       Session session = sessionFactory.openSession();    Transaction transaction = session.beginTransaction();    // Get an object by its primary key    Employee employee = (Employee) session.get(Employee.class, 1L);    transaction.commit();    se...

3. Hibernate Annotation

Hibernate Annotation   - Hibernate is a popular Java-based framework for working with relational databases.  - It simplifies the process of database interactions by providing an object-relational mapping (ORM) solution.  - Annotations in Hibernate are metadata provided in the form of Java annotations that help configure and map Java objects to database tables.  - These annotations are used to define the mapping between Java classes and database tables, specify primary keys, relationships, and other aspects of the object-relational mapping. Some commonly used Hibernate annotations: 1. @Entity:      - Used for declaring any POJO class as an entity for a database.    - Marks a Java class as an entity, indicating that instances of this class will be persisted to the database. Each entity class typically corresponds to a table in the database.    - it tells Hibernate that this class represents a table in the database.     @En...

2. Hibernate Architecture

Image
  Hibernate Architecture   The architecture of Hibernate is based on the following key components: 1. Hibernate Configuration:    - The configuration file (`hibernate.cfg.xml`) is used to configure Hibernate properties such as database connection settings, dialect, mapping files, and other environment-specific settings.    - Alternatively, you can configure Hibernate programmatically using the `Configuration` class. The Configuration object provides two keys components − Database Connection − This is handled through one or more configuration files supported by Hibernate. These files are hibernate.properties and hibernate.cfg.xml. Class Mapping Setup − This component creates the connection between the Java classes and database tables. 2. SessionFactory:      - SessionFactory is an Interface which is present in org.hibernate package and it is used to create Session Object.    - The `SessionFactory` is a heavyweight.    ...

1. Introduction Hibernate

 Introduction Hibernate  - Hibernate is a java framework that simplifies the developement of java application to intract with the Database.  - It is invented by Gavin King in 2001. - Hibernate is the ORM framework/tool . - Hibernate Implement specifiaction of JPA ( Java Persistence API ) for the data persistence. - Hibernate is non-inversive framework , means it wont forces to programmer to extend / implement any class/interface. - We can build any type of application using hibernate.(like standalone, servlets , desktop etc) - It is a java framework which is used to develop persistence logic. Persistence logic means to store and process the data for long use. Advantages of Hibernates  - Hibernate takes care of mapping Java classes to database tables using XML files and without writing any line of code. - If there is change in the database or in any table, then you need to change the XML file properties only. - Minimizes database access with smart fetching strategies....