Skip to main content

Notice: This Wiki is now read only and edits are no longer possible. Please see: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/wikis/Wiki-shutdown-plan for the plan.

Jump to: navigation, search

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

m
m
Line 9: Line 9:
 
* 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;
 
* 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;
 
* configure the details of the join table used by the persistence provider for unidirectional one-to-many relationships {{EclipseLink_Spec|section=Section 9.1.25 "JoinTable Annotation"}}
 
* configure the details of the join table used by the persistence provider for unidirectional one-to-many relationships {{EclipseLink_Spec|section=Section 9.1.25 "JoinTable Annotation"}}
 
+
{{EclipseLink_AttributeTable
The <tt>@OneToMany</tt> annotation has the following attributes:
+
|caption=Attributes for the <tt>@OneToMany</tt> annotation
* <tt>cascade</tt> 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 <tt>javax.persitence.CascadeType</tt> array.<br>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 <tt>CascadeType</tt> instances, including the following:
+
|content=<tr>
** <tt>ALL</tt> – Any persistence operation performed on the owning entity is cascaded to the target of the association.
+
<td>'''<tt>cascade</tt>'''</td>
** <tt>MERGE</tt> – If the owning entity is merged, the merge is cascaded to the target of the association.
+
<td>By default, JPA does not cascade any persistence operations to the target of the association. 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 <tt>CascadeType</tt> instances, including the following:
** <tt>PERSIST</tt> – If the owning entity is persisted, the persist is cascaded target of the association.
+
* <tt>ALL</tt> – Any persistence operation performed on the owning entity is cascaded to the target of the association.
** <tt>REFRESH</tt> – If the owning entity is refreshed, the refresh is cascaded target of the association.
+
* <tt>MERGE</tt> – If the owning entity is merged, the merge is cascaded to the target of the association.
** <tt>REMOVE</tt> – If the owning entity is removed, the target of the association is also removed.<br><br>You are not required to provide value for this attribute.
+
* <tt>PERSIST</tt> – If the owning entity is persisted, the persist is cascaded target of the association.
* <tt>fetch</tt> By default, EclipseLink persistence provider uses a [[EclipseLink/UserGuide/JPA/Basic_JPA_Development/Mapping/Relationship_Mappings/Common_Relationship_Configurations/Fetch_Type|fetch type]] of javax.persitence.<tt>FetchType.LAZY</tt><nowiki>: this is a hint to the persistence provider that data should be fetched lazily when it is first accessed (if possible).</nowiki><br>If the default is inappropriate for your application or a particular persistent field, set <tt>fetch</tt> to <tt>FetchType.EAGER</tt><nowiki>: this is a requirement on the persistence provider runtime that data must be eagerly fetched.</nowiki><br><br>You are not required to provide value for this attribute.<br>For more information, see [[Using%20EclipseLink%20JPA%20Extensions%20(ELUG)#What You May Need to Know About EclipseLink JPA Lazy Loading|What You May Need to Know About EclipseLink JPA Lazy Loading]].
+
* <tt>REFRESH</tt> – If the owning entity is refreshed, the refresh is cascaded target of the association.
* <tt>mappedBy</tt> By default, if the relationship is unidirectional, EclipseLink persistence provider determines the field that owns the relationship.<br>If the relationship is bidirectional, then set the <tt>mappedBy</tt> element on the inverse (non-owning) side of the association to the name of the field or property that owns the relationship, as the [[#Example 18-21|Usage of @ManyToOne Annotation - Order Class with Generics]] example shows.<br><br>You are not required to specify the value of this attribute.
+
* <tt>REMOVE</tt> – If the owning entity is removed, the target of the association is also removed.</td>
* <tt>targetEntity</tt> By default, if you are using a <tt>Collection</tt> 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 <tt>Collection</tt> when defined using generics.<br>If your <tt>Collection</tt> does not use generics, then you must specify the entity class that is the target of the association: set the <tt>targetEntity</tt> element on owning side of the association to the <tt>Class</tt> of the entity that is the target of the relationship.<br><br>You are not required to specify the value of this attribute.
+
<td>An empty <tt>javax.persitence.CascadeType</tt> array<br></td>
 +
<td>No</td>
 +
</tr>
 +
<tr>
 +
<td>'''<tt>fetch</tt>'''</td>
 +
<td>By default, EclipseLink persistence provider uses a [[EclipseLink/UserGuide/JPA/Basic_JPA_Development/Mapping/Relationship_Mappings/Common_Relationship_Configurations/Fetch_Type|fetch type]] of javax.persitence.<tt>FetchType.LAZY</tt><nowiki>: this is a hint to the persistence provider that data should be fetched lazily when it is first accessed (if possible).</nowiki><br>If the default is inappropriate for your application or a particular persistent field, set <tt>fetch</tt> to <tt>FetchType.EAGER</tt><nowiki>: this is a requirement on the persistence provider runtime that data must be eagerly fetched.</nowiki><br>For more information, see [[Using%20EclipseLink%20JPA%20Extensions%20(ELUG)#What You May Need to Know About EclipseLink JPA Lazy Loading|What You May Need to Know About EclipseLink JPA Lazy Loading]].</td>
 +
<td>javax.persitence.<tt>FetchType.LAZY</tt></td>
 +
<td>No</td>
 +
</tr>
 +
<tr>
 +
<td>'''<tt>mappedBy</tt>'''</td>
 +
<td>By default, if the relationship is unidirectional, EclipseLink persistence provider determines the field that owns the relationship.<br>If the relationship is bidirectional, then set the <tt>mappedBy</tt> element on the inverse (non-owning) side of the association to the name of the field or property that owns the relationship, as the [[#Example 18-21|Usage of @ManyToOne Annotation - Order Class with Generics]] example shows.</td>
 +
<td></td>
 +
<td>No</td>
 +
</tr>
 +
<tr>
 +
<td>'''<tt>targetEntity</tt>'''</td>
 +
<td>By default, if you are using a <tt>Collection</tt> 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 <tt>Collection</tt> when defined using generics.<br>If your <tt>Collection</tt> does not use generics, then you must specify the entity class that is the target of the association: set the <tt>targetEntity</tt> element on owning side of the association to the <tt>Class</tt> of the entity that is the target of the relationship.</td>
 +
<td></td>
 +
<td>No</td>
 +
</tr>
 +
}}
  
 
The [[#Example 18-20|Usage of @OneToMany Annotation - Customer Class with Generics]] and [[#Example 18-21|Usage of @ManyToOne Annotation - Order Class with Generics]] examples show how to use this annotation to configure a one-to-many mapping between <tt>Customer</tt> (the owned side) and <tt>Order</tt> (the owning side) using generics.
 
The [[#Example 18-20|Usage of @OneToMany Annotation - Customer Class with Generics]] and [[#Example 18-21|Usage of @ManyToOne Annotation - Order Class with Generics]] examples show how to use this annotation to configure a one-to-many mapping between <tt>Customer</tt> (the owned side) and <tt>Order</tt> (the owning side) using generics.

Revision as of 23:05, 18 June 2010


Eclipselink-logo.gif
EclipseLink
Website
Download
Community
Mailing ListForumsIRCmattermost
Issues
OpenHelp WantedBug Day
Contribute
Browse Source

OneToMany

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

Use the OneToMany annotation to do the following:

  • configure the fetch type to EAGER;
  • 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;
  • configure the details of the join table used by the persistence provider for unidirectional one-to-many relationships
Elug javaspec icon.gif

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

Attributes for the @OneToMany annotation
Attribute Description Default Required?
cascade By default, JPA does not cascade any persistence operations to the target of the association. 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.
An empty javax.persitence.CascadeType array
No
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.
For more information, see What You May Need to Know About EclipseLink JPA Lazy Loading.
javax.persitence.FetchType.LAZY No
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 @ManyToOne Annotation - Order Class with Generics example shows.
No
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.
No

The Usage of @OneToMany Annotation - Customer Class with Generics and Usage of @ManyToOne Annotation - Order Class with Generics examples show how to use this annotation to configure a one-to-many mapping between Customer (the owned side) and Order (the owning side) using generics.


Usage of @OneToMany Annotation - Customer Class with Generics

 @Entity
 public class Customer implements Serializable {
     ...
     @OneToMany(cascade=ALL, mappedBy="customer")
     public Set<Order> getOrders() {  
         return orders;
     }
     ...
 }

Usage of @ManyToOne Annotation - Order Class with Generics

 @Entity
 public class Order implements Serializable {
     ...
     @ManyToOne
     @JoinColumn(name="CUST_ID", nullable=false)
     public Customer getCustomer() { 
         return customer;
     }
     ...
 }
Elug javaspec icon.gif

For more information, see Section 9.1.24 "OneToMany 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 One-to-Many Mapping, and for information on how to configure these mappings, see Configuring a Relational One-to-Many Mapping.


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

Back to the top