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();
session.close();
2. load() method:
- The load() method is also a part of the Hibernate Session interface.
- It retrieves an entity object from the database by its primary key (ID).
- If the requested entity does not exist in the database, load() throws an ObjectNotFoundException.
- load() does not immediately hit the database.
- load() method does not retrieve the object from the database when it is called. Instead, it returns a proxy object that represents the object. The actual object is only retrieved from the database when it is needed, such as when a method of the object is called or a property is accessed. This technique is known as “lazy loading” and it is used to improve the performance of Hibernate by avoiding unnecessary database queries.
- The load method is useful when you want to load an object lazily, i.e., only when its properties are needed.
It is typically used when you are sure that the entity exists in the database and you want to fetch it lazily, i.e., only when its properties are accessed.
Example:
Session session = sessionFactory.openSession();
Transaction transaction = session.beginTransaction();
// Load an object by its primary key
Employee employee = (Employee) session.load(Employee.class, 1L);
// At this point, the actual object is not loaded from the database
// Accessing any property of the loaded object will trigger the database query
System.out.println("Employee Name: " + employee.getName());
transaction.commit();
session.close();
both `get` and `load` methods are used to retrieve objects from the database by their primary key, but the `load` method allows for lazy loading of the object, fetching it from the database only when its properties are accessed.
Uses :
- In most cases, the get method is preferred when you expect the object to exist, and you want to handle a null result.
- The load method is suitable when you want to take advantage of lazy loading and don't need the object immediately.
- get : Use if you are sure that object exist.
- load : Use if you are not sure that object exist in db or not.
@Embeddable and @Embedded Annotation
(Embeddable means Inserting and forcing things into other things.)
- The @Embeddable and @Embedded annotations in Hibernate are used to map an object’s properties to columns in a database table.
- These annotations are used in combination to allow the properties of one class to be included as a value type in another class and then be persisted in the database as part of the containing class.
@Embeddable:
- @Embeddable is an annotation used to mark a class as embeddable in another entity.
- It is typically applied to a class that represents a group of related fields or properties that you want to embed within another entity.
- Think of it as a way to define a reusable component that can be included as part of other entity classes.
Ex :
@Embeddable
public class Address {
private String street;
private String city;
private String zipcode;
// Getters and setters
}
@Embedded:
- @Embedded is an annotation used to specify that an entity should embed an instance of the marked @Embeddable class.
- It is typically applied to a field or property within an entity class that represents the embedded object.
- When Hibernate maps the entity to the database, it will store the embedded object's properties as part of the enclosing entity's table.
Ex :
@Entity
public class Employee {
@Id
@GeneratedValue
private Long id;
private String name;
@Embedded
private Address address;
// Getters and setters
}
Benefits using
- Code Reusability
- Normalization (reduce number of tables)
- Ease of maintenance.
- Increase Readability of Code.
Using @Embeddable and @Embedded annotations allows you to organize your entity model more effectively by grouping related fields together and avoiding duplication of schema information in the database. It's particularly useful for modeling composite(Together) objects or complex data structures within your domain model.
Comments
Post a Comment