dineshonjava

CascadeTypes in Hibernate

In this section, you will know about the jpa relationship. JPA supports the relationship between entities.

JPA Relationship

In this section, you will know about the jpa relationship. JPA supports the relationship between entities.
There are following types of relationship:
  1. One-to-one
  2. One-to-many
  3. Many-to-one
  4. Many-to-many
JPA relationships are not bi-directional. JPA annotations are added to define the relationship. If, you want to make a relationship bi-directional to use a special attribute.
  1. One-to-one: A OneToOne relation mapping to a single-value association to another entity. It has one-to-one multiplicity and infers the associated target entity from the type of the object being referenced.

    Use of the @OneToOne Annotation:
    1. Configure the fetch type to LAZY
    2. Configure the mapping to forbid null values (for non-primitive types) in case null values are inappropriate for your application
    3. Configure the associated target entity if it cannot be inferred from the type of the object being referenced
    4. Configure the operations that must be cascaded to the target of the association. For example, if the owning entity is removed, ensure that the target of the association is also removed
  2. One-to-many: JPA defines a OneToMany mapping for a many-valued association with one-to-many multiplicity.

    Use of the @OneToMany Annotation:
    1. Configure the fetch type to EAGER
    2. Configure the associated target entity because the Collection used is not defined using generics
    3. Configure the operations that must be cascaded to the target of the association: for example, if the owning entity is removed, ensure that the target of the association is also removed
    4. Configure the details of the join table used by TopLink JPA for uni-directional one-to-many relationships
  3. Many-to-one: JPA defines a ManyToOne mapping for a single-valued association to another entity class that has many-to-one multiplicity.

    Use the @ManyToOne Annotation to:
    1. Configure the fetch type to LAZY
    2. Configure the mapping to forbid null values (for non-primitive types) in case null values are inappropriate for your application
    3. Configure the associated target entity if it cannot be inferred from the type of the object being referenced
    4. Configure the operations that must be cascaded to the target of the association: for example, if the owning entity is removed, ensure that the target of the association is also removed
  4. Many-to-many: JPA defines a @ManyToMany mapping for a many-valued association with many-to-many multiplicity.

    Use of the @ManyToMany annotation:
    1. Declare the cardinality of the relationship
    2. Configure the fetch type to EAGER
    3. Configure the mapping to forbid null values (for non-primitive types) in case null values are inappropriate for your application
    4. Configure the associated target entity because the Collection used is not defined using generics
    5. Configure the operations that must be cascaded to the target of the association; for example, if the owning entity is removed, ensure that the target of the association is also removed
    There are following types of cascade:
    1.   CascadeType.PERSIST: When we persist and entity all the entities held in this field persist too. If you want to persist an entity and the fields dont use it fails. 
    2. CascadeType.REMOVE: When we delete an entity all the entities held in this field delete too.
    3. CascadeType.REFRESH: When we refresh an entity all the entities held in this field refresh too.
    4. CascadeType.MERGE: When we merde an entity all the entities held in this flied merged too
The property cascade = CascadeType.ALL indicates that when we persist, remove, refresh or merge this entity all the entities held in this field would be persist, remove, delete or update.


In the Next Chapter we will learn how to implements inheritance in the Hibernate.

                  <<Previous Chapter 20<<    >>Next Chapter22>>