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.
- It's like ordering items separately – you get the burger first, and the fries are brought to you only if you ask for them.
- Lazy loading can be more efficient because it only retrieves what's needed, but you need to be mindful of potential issues like lazy loading exceptions if the associated data is accessed outside of the original database transaction.
Choosing between eager and lazy fetch types depends on the specific requirements of your application and how you want to balance performance and resource usage.
Cascadding
In Hibernate, a cascade is a mechanism that helps manage the relationships between entities in a database. When you perform an operation (like saving, updating, or deleting) on a "parent" entity, the cascade feature determines what should happen to its "child" entities.
Imagine you have two entities, let's say a `Parent` and a `Child`, and there's a relationship between them, like a parent having multiple children. Now, let's say you want to save a new `Parent` entity along with its associated `Child` entities.
Cascade comes into play here. If you have cascade set up appropriately, when you save the `Parent` entity, Hibernate can automatically save the associated `Child` entities without you explicitly calling the save operation for each child.
Here's a simple breakdown:
- CascadeType.ALL: This means if you perform any operation (save, update, delete, etc.) on the `Parent`, the same operation will be applied to its associated `Child` entities.
- CascadeType.SAVE_UPDATE: If you only want to save and update to cascade, but not delete.
- CascadeType.DELETE: If you only want delete operation to cascade.
Here's a simple example in Java code using annotations:
@Entity
public class Parent {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@OneToMany(mappedBy = "parent", cascade = CascadeType.ALL)
private List<Child> children;
// other fields and methods
}
@Entity
public class Child {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@ManyToOne
@JoinColumn(name = "parent_id")
private Parent parent;
// other fields and methods
}
In this example, when you save a `Parent` object, the associated `Child` objects will be saved automatically due to the `cascade = CascadeType.ALL` setting on the `children` field.
Comments
Post a Comment