JPA Relationship
Posted on: June 11, 2009 at 12:00 AM
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
  5. Many-to-many Retrieve data

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.

Download JPA Annotation Relationship Application

Related Tags for JPA Relationship:
cjpaioannotationsipdefineaddtorelationshipnotationannotationshbienotddeinnopsrelationrelationsddjaddirectiondirotarelationshipsrelsrectdirectataddedirannarssthatifinono


More Tutorials from this section

Ask Questions?    Discuss: JPA Relationship   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 

Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.