
- Hibernate - Home
- ORM - Overview
- Hibernate - Overview
- Hibernate - Architecture
- Hibernate - Environment
- Hibernate - Configuration
- Hibernate - Sessions
- Hibernate - Persistent Class
- Hibernate - Mapping Files
- Hibernate - Mapping Types
- Hibernate - Examples
- Hibernate - O/R Mappings
- Hibernate - Cascade Types
- Hibernate - Annotations
- Hibernate - Query Language
- Hibernate - Criteria Queries
- Hibernate - Native SQL
- Hibernate - Caching
- Hibernate - Entity Lifecycle
- Hibernate - Batch Processing
- Hibernate - Interceptors
- Hibernate - ID Generator
- Hibernate - Saving Image
- Hibernate - log4j Integration
- Hibernate - Spring Integration
- Hibernate - Struts 2 Integration
- Hibernate - Web Application
- Mapping Table Examples
- Hibernate - Table Per Hiearchy
- Hibernate - Table Per Concrete Class
- Hibernate - Table Per Subclass
Hibernate - Cascade Types
While configuring collections in hbm file, also in mapping one-to-many, many-to-many mappings, the collection element (say, list) in the hbm file contains an attribute cascade.
Example
... <class name="Student" table="student_tbl_100"> <id name="studentid"> <generator class="native"></generator> </id> <property name="name"></property> <map name="courses" table="course_tbl_100" cascade="all"> <key column="id"></key> <index column="course_id" type="string"></index> <element column="course_name" type="string"></element> </map> </class> ...
The cascade type can also be mentioned in an annotation as shown below:
@Entity public class Customer { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; @OneToMany(mappedBy = "customer", cascade = CascadeType.ALL) private Set<Order> orders = new HashSet(); // getters and setters }
@Entity public class Order { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; @ManyToOne @JoinColumn(name = "customer_id") private Customer customer; // getters and setters }
When a Customer entity is persisted, updated, or deleted, all associated Order entities will also be persisted, updated, or deleted.
Different Cascade Types in Hibernate
Hibernate provides several types of cascade options that can be used to manage the relationships between entities. Here are the different cascade types in Hibernate:
CascadeType.ALL − A cascading type in Hibernate that specifies that all state transitions (create, update, delete, and refresh) should be cascaded from the parent entity to the child entities.
CascadeType.PERSIST − A cascading type in Hibernate that specifies that the create (or persist) operation should be cascaded from the parent entity to the child entities.
CascadeType.MERGE − A cascading type in Hibernate that specifies that the update (or merge) operation should be cascaded from the parent entity to the child entities.
CascadeType.REMOVE − A cascading type in Hibernate that specifies that the delete operation should be cascaded from the parent entity to the child entities.
CascadeType.REFRESH − A cascading type in Hibernate that specifies that the refresh operation should be cascaded from the parent entity to the child entities.
CascadeType.DETACH − A cascading type in Hibernate that specifies that the detach operation should be cascaded from the parent entity to the child entities.
CascadeType.REPLICATE − A cascading type in Hibernate that specifies that the replicate operation should be cascaded from the parent entity to the child entities.
CascadeType.SAVE_UPDATE − A cascading type in Hibernate that specifies that the save or update operation should be cascaded from the parent entity to the child entities.
These cascade types can be used individually or in combination to manage the relationships between entities based on the requirements of the application. It is important to use cascade types carefully, as they can lead to unintended consequences if not used properly.