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

Wednesday, July 10, 2013

Hibernate Many-to-One Mappings Tutorial by example

Many-to-One Mappings




  • In our cardinality discussion of the Employee and Department relationship ,  we first thought of an employee working in a department, so we just assumed that it was a one to- one relationship. 
  • However, when we realized that more than one employee works in the same department, we changed it to a many-to-one relationship mapping. It turns out that many-to-one is the most common mapping and is the one that is normally used when creating an association to an entity.

  • we show a many-to-one relationship between Employee and Department. Employee is the “many” side and the source of the relationship, and Department is the “one” side and the target. Once again, because the arrow points in only one direction, from Employee to Department, the relationship is unidirectional. 
  • Note that in UML, the source class has an implicit attribute of the target class type if it can be navigated to. For example, Employee has an attribute called department that will contain a reference to a single Department instance. 
  • A many-to-one mapping is defined by annotating the attribute in the source entity (the attribute that refers to the target entity) with the @ManyToOne annotation


Many-to-One Relationship from Employee to Department

@Entity
public class Employee {
// ...
@ManyToOne
private Department department;
// ...
}

  • we can see how the @ManyToOne annotation is used to map this relationship. The department field in Employee is the source attribute that is annotated.

Using Join Columns



  • In the database, a relationship mapping means that one table has a reference to another table. The database term for a column that refers to a key (usually the primary key) in another table is a foreign key column. 
  • In the Java Persistence API we call them join columns, and the @JoinColumn annotation is the primary annotation used to configure these types of columns.

  • Consider the EMPLOYEE and DEPARTMENT tables, that correspond to the Employee and Department entities. The EMPLOYEE table has a foreign key column named DEPT_ID that references the DEPARTMENT table. 
  • From the perspective of the entity relationship, DEPT_ID is the join column that associates the Employee and Department entities.
  • For example, the @JoinColumn(name="DEPT_ID") annotation means that the DEPT_ID column in the source entity table is the foreign key to the target entity table, whatever the target entity of the relationship happens to be.

Many-to-One Relationship Overriding the Join Column

@Entity
public class Employee {
@Id private int id;
@ManyToOne
@JoinColumn(name="DEPT_ID")
private Department department;
// ...
}




No comments:

Post a Comment