Difference between revisions of "EclipseLink/UserGuide/JPA/Basic JPA Development/Mapping/Relationship Mappings/Collection Mappings/ManyToMany"

From Eclipsepedia

Jump to: navigation, search
m (ManyToMany)
m
Line 1: Line 1:
 +
{EclipseLink_UserGuide|info=y}}
 
=ManyToMany=
 
=ManyToMany=
 
By default, JPA automatically defines a <tt>ManyToMany</tt> mapping for a many-valued association with many-to-many multiplicity.
 
By default, JPA automatically defines a <tt>ManyToMany</tt> mapping for a many-valued association with many-to-many multiplicity.

Revision as of 15:26, 18 June 2010

{EclipseLink_UserGuide|info=y}}

ManyToMany

By default, JPA automatically defines a ManyToMany mapping for a many-valued association with many-to-many multiplicity.

Use the @ManyToMany annotation to do the following:

  • configure the Fetch Type to EAGER;
  • configure the mapping to forbid null values (for nonprimitive types) in case null values are inappropriate for your application;
  • configure the associated target entity because the Collection used is not defined using generics;
  • 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).

The @ManyToMany annotation has the following attributes:

  • cascade – By default, JPA does not cascade any persistence operations to the target of the association. Thus, the default value of this attribute is an empty javax.persitence.CascadeType array.
    If you want some or all persistence operations cascaded to the target of the association, set the value of this attribute to one or more CascadeType instances, including the following:
    • ALL – Any persistence operation performed on the owning entity is cascaded to the target of the association.
    • MERGE – If the owning entity is merged, the merge is cascaded to the target of the association.
    • PERSIST – If the owning entity is persisted, the persist is cascaded target of the association.
    • REFRESH – If the owning entity is refreshed, the refresh is cascaded target of the association.
    • REMOVE – If the owning entity is removed, the target of the association is also removed.

      You are not required to provide value for this attribute.
  • fetch – By default, EclipseLink persistence provider uses a fetch type of javax.persitence.FetchType.LAZY: this is a hint to the persistence provider that data should be fetched lazily when it is first accessed (if possible).
    If the default is inappropriate for your application or a particular persistent field, set fetch to FetchType.EAGER: this is a requirement on the persistence provider runtime that data must be eagerly fetched.

    You are not required to provide value for this attribute.
    For more information, see What You May Need to Know About EclipseLink JPA Lazy Loading.
  • mappedBy – By default, if the relationship is unidirectional, EclipseLink persistence provider determines the field that owns the relationship.
    If the relationship is bidirectional, then set the mappedBy element on the inverse (non-owning) side of the association to the name of the field or property that owns the relationship, as the Usage of @ManyToMany Annotation - Project Class with Generics example shows.

    You are not required to specify the value of this attribute.
  • targetEntity – By default, if you are using a Collection defined using generics, then the persistence provider infers the associated target entity from the type of the object being referenced. Thus, the default is the parameterized type of the Collection when defined using generics.
    If your Collection does not use generics, then you must specify the entity class that is the target of the association: set the targetEntity element on owning side of the association to the Class of the entity that is the target of the relationship.

    You are not required to specify the value of this attribute.

The Usage of @ManyToMany Annotation - Employee Class with Generics and Usage of @ManyToMany Annotation - Project Class with Generics examples show how to use this annotation to configure a many-to-many mapping between Employee and Project using generics.


Usage of @ManyToMany Annotation - Employee Class with Generics

 @Entity
 public class Employee implements Serializable {
     @Id
     private int id;
     private String name;
     @ManyToMany
     @JoinTable(name="EMP_PROJ",
                 joinColumns=
                      @JoinColumn(name="EMP_ID"),
                 inverseJoinColumns=
                      @JoinColumn(name="PROJ_ID)
     )
     private Collection<Project> projects;
     ...
 }

Elug note icon.png

Note: Use a @JoinTable annotation to define a many-to-many join table; if you do not specify this annotation, EclipseLink will default to @JoinTable with the join table name format of <source-table-name>_<target-table-name> in uppercase characters, and with columns format of <source-entity-alias>_<source-primary-key-column>, <source-field-name>_ <target-primary-key-column> (or <source-property-name>_ <target-primary-key-column>) in uppercase characters.

Elug javaspec icon.gif

For more information, see Section 9.1.25 "JoinTable Annotation" in the JPA Specification.


Usage of @ManyToMany Annotation - Project Class with Generics

 @Entity
 public class Project implements Serializable {
     ...
     @ManyToMany(mappedBy="projects")
     public Set<Employee> getEmployees() { 
         return employees;
     }
     ...
 }
Elug javaspec icon.gif

For more information, see Section 9.1.26 "ManyToMany Annotation" in the JPA Specification.

For more information on EclipseLink direct mappings and relationship mappings, see Relational Mapping Types.

For more information on EclipseLink one-to-one mappings, see Many-to-Many Mapping, and for information on how to configure these mappings, see Configuring a Relational Many-to-Many Mapping.


Eclipselink-logo.gif
Version: DRAFT
Other versions...