Related Tutorials


 
 

Share on Google+Share on Google+

JPA Relationship

Advertisement
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

Advertisements

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: June 11, 2009

Related Tutorials

Discuss: JPA Relationship   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:1
Sandy Noa Cabrera
September 11, 2012
Generate entities from tables in Eclipse

By some days I 've been reading about JPA in Internet. I would like to make a simple question, well, this is my situation: I have a environment with Eclipse Ingigo like IDE and I have created an EJB project (ready to use JPA[Hibernate]), also I have my own Oracle DB with all my tables, indexes, sequences, etc... and I want to generate entities from tables using JBossAS7.1.0 libraries (this is the server I will use to deploy my application in the future). When I try to do it, Eclipse generates only a subset of classes and I don't understand why. How should I generate my entities, manually or assisted by Eclipse ??? What I'm missing ??? Thanks, Sandy...
DMCA.com