One-to-Many Mappings


  • When an entity is associated with a Collection of other entities, it is most often in the form of a one-to-many mapping. For example, a department would normally have a number of employees







  • Employee and Department relationship that we showed earlier in the section Many-to-One Mappings, only this time the relationship is bidirectional in nature.

  • When a relationship is bidirectional, there are actually two mappings, one for each direction. A bidirectional one-to-many relationship always implies a many-to-one mapping back to the source, so in our Employee and Department example there is a one-to-many mapping from Department to Employee and a many-to-one mapping from Employee back to Department.


One-to-Many Relationship




@Entity

public class Department {
@Id private int id;
private String name;
@OneToMany(mappedBy="department")
private Collection<Employee> employees;
// ...
}

  • There are a couple of noteworthy points to mention about this class. The first is that a generic type-parameterized Collection is being used to store the Employee entities. This provides 
     the strict typing that guarantees that only objects of type Employee will exist in the 
     
    Collection. This, in and of itself, is quite useful since it not only provides compile-time checking 
     
    of our code but also saves us having to perform cast operations when we retrieve the 
     
    Employee instances from thecollection.
  • The Java Persistence API assumes the availability of generics; however, it is still perfectly acceptable to use a Collection that is not type-parameterized. We might just as well have defined the Department class without using generics but defining only a simple Collection type, as we would have done in releases of standard Java previous to Java SE 5 (except for JDK 1.0 or 1.1 when java.util.Collection was not even standardized!). If we did, then we would need to specify the type of entity that will be stored in the Collection that is needed by the persistence provider.

Using targetEntity


@Entity
public class Department {
@Id private int id;
private String name;
@OneToMany(targetEntity=Employee.class, mappedBy="department")
private Collection employees;
// ...
}


     for the targetEntity element that indicates the entity type.



There are two important points to remember when defining bidirectional one-to-many
(or many-to-one) relationships:


The many-to-one side is the owning side, so the join column is defined on that side.
The one-to-many mapping is the inverse side, so the mappedBy element must be used.

Leave a Reply

Subscribe to Posts | Subscribe to Comments

About This Site

Howdy! My name is Suersh Rohan and I am the developer and maintainer of this blog. It mainly consists of my thoughts and opinions on the technologies I learn,use and develop with.

Blog Archive

Powered by Blogger.

- Copyright © My Code Snapshots -Metrominimalist- Powered by Blogger - Designed by Suresh Rohan -