Suresh Rohan's Blog

This blog is all about Java, J2EE,Spring, Angular, React JS, NoSQL, Microservices, DevOps, BigData, Tutorials, Tips, Best practice, Interview questions, Views, News, Articles, Techniques, Code Samples, Reference Application and much more

Thursday, July 11, 2013

Hibernate One-to-Many Mappings using JPA Annotation

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;
// ...
}


  • looks almost identical, except 
    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:

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


No comments:

Post a Comment