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

EclipseLink/UserGuide/JPA/Basic JPA Development/Mapping/Relationship Mappings/Collection Mappings/OneToMany

EclipseLink JPA

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

Elug api package icon.png Key API


One-to-Many Mapping

One-to-many mappings are used to represent the relationship between a single source object and a collection of target objects. They are a good example of something that is simple to implement in Java using a Collection (or other collection types) of target objects, but difficult to implement using relational databases.

In a Java Collection, the owner references its parts. In a relational database, the parts reference their owner. Relational databases use this implementation to make querying more efficient.

One-to-Many Relationships

One-to-Many Relationships

Elug note icon.png

Note: The phone attribute shown in the One-to-Many Relationships is of type Vector. You can use a Collection interface (or any class that implements the Collection interface) for declaring the collection attribute.

JPA Mapping

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 11.1.23 "JoinTable Annotation" in the JPA Specification.

@OneToMany Annotation Attributes
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..
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 @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 @OneToMany Annotation - Customer Class with Generics and @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.


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

Example: @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 11.1.36 "OneToMany Annotation" in the JPA Specification.

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

Back to the top