Friday, July 26, 2013
Tuesday, July 23, 2013
Hibernate better understanding :: Object-relational mapping in JPA
via:: Nikojava
The Java Persistence API is the standard framework for persistence in both Java SE and Java EE platforms. It is the outcome of the collaborative work of the industry’s leading vendors in object-relational mapping, EJB and JDO, including a remarkable contribution from the open source community.
In this post we summarize, one-by-one, the essential relationships between entities. We examine each relationship from the prespectives of “Domain Model” and “Relation Model”, and provide the associated construction in JPA. Finally, for each relationship we present an example from the real world.
The relationships are divided in two categories. “For daily use” presents the correct way to use common relationships in our application development activities. On the other hand, “Containing pitfalls” identify common mistakes.
For daily use:
- One-to-One (one direction)
- One-to-One (both directions)
- Many-to-One (one direction)
- Many-to-One (both directions)
- One-to-Many (one direction)
- One-to-Many (both directions)
- Many-to-Many (both directions)
- Many-to-Many (one direction)
One-to-One (one direction)
@Entity class A { @Id int id; @OneToOne B b; } @Entity class B { @Id int id; }
Example 1, One-to-One unidirectional relationship in JPA
An employee has one desk.@Entity public class Employee implements Serializable { @Id private int id; private String name; @OneToOne private Desk desk; } @Entity public class Desk implements Serializable { @Id private int id; private String position; }
One-to-One (both directions)
In this case we would like to keep a reference between both sides.@Entity class A { @Id int id; @OneToOne B b; } @Entity class B { @Id int id; @OneToOne(mappedBy="b") A a; }Since the relation concerns both directions, we may change the owning side.
@Entity class A { @Id int id; @OneToOne(mappedBy="a") B b; } @Entity class B { @Id int id; @OneToOne A a; }
Example 2, One-to-One bidirectional relationship in JPA
A husband has exactly one wife. A wife has exactly one husband.@Entity public class Husband implements Serializable { @Id private int id; private String name; @OneToOne private Wife wife; } @Entity public class Wife implements Serializable { @Id private int id; private String name; @OneToOne(mappedBy="wife") private Husband husband; }We assume husband is the owning side, so he will hold the foreign key.
Now we can ask the wife “Who is your husband?”, as well as, ask the husband “Who is your wife?”.
Many-to-One (one direction)
@Entity class A { @Id int id; @ManyToOne B b; } @Entity class B { @Id int id; }
Example 3, Many-to-One unidirectional relationship in JPA
There are millions of music fans out there. Each fan has his favorite singer. Of course, a signer is not able to keep detailed records of his fans.@Entity public class Fan implements Serializable { @Id private int id; private String name; @ManyToOne private Singer favoriteSinger; } @Entity public class Singer implements Serializable { @Id private int id; private String name; }As a result, the foreign key goes to the fan.
Now we may ask any fan “Who is your favorite signer?”.
Many-to-One (both directions)
@Entity class A { @Id int id; @ManyToOne B b; } @Entity class B { @Id int id; @OneToMany(mappedBy="b") Collection<A> listOfA; }
Example 4, Many-to-One bidirectional relationship in JPA
The children of a father: A child knows his father and the father knows his children.@Entity public class Child implements Serializable { @Id private int id; private String name; @ManyToOne private Father father; } @Entity public class Father implements Serializable { @Id private int id; private String surname; @OneToMany(mappedBy="father") private Collection<Child> children; }
One-to-Many (one direction)
It is the case when an entity has a set of characteristics.@Entity class A { @Id int id; // A join table is assumed. @OneToMany Collection<B> listOfB; } @Entity class B { @Id int id; }Thus, when declaring an OneToMany annotation without a mappedBy element, then a join table is assumed.
Example 5, One-to-Many unidirectional relationship in JPA
A library which provides facilities.@Entity public class Library implements Serializable { @Id private int id; private String name; @OneToMany private Collection<Facility> facilities; } @Entity public class Facility implements Serializable { @Id private String code; private String description; }Now we can ask a library “What facilities do you provide?”. The answer could be “Scanning, Printing and Photocopying”. This set of facilities may be provided by one or more libraries at the same time.
If using a join table is not what you want, then you should use the next relationship.
One-to-Many (both directions)
It is exactly the same with Many-to-One (both directions), looking via a mirror.@Entity class A { @Id int id; @OneToMany(mappedBy="a") Collection<B> listOfB; } @Entity class B { @Id int id; @ManyToOne A a; }
Example 6, One-to-Many bidirectional relationship in JPA
A manager who manages projects.@Entity public class Manager implements Serializable { @Id private int id; private String name; @OneToMany(mappedBy="manager") private Collection<Project> projects; } @Entity public class Project implements Serializable { @Id private int id; private String title; @ManyToOne private Manager manager; }
Many-to-Many (both directions)
@Entity class A { @Id int id; @ManyToMany Collection<B> listOfB; } @Entity class B { @Id int id; @ManyToMany(mappedBy="listOfB") Collection listOfA; }Of course, a join table is used.
Note: In a birectional many-to-many relationship we should alwaysinclude the mappedBy element to either of the sides. This ensures that exactly one join table is used.
Example 7, Many-to-Many bidirectional relationship in JPA
An engineer works in many projects. A project occupies many engineers. This is a classic many-to-many relationship. Moreover it expands to both directions, as:- An engineer should in which projects he is working.
- The project should know the engineers it occupies.
@Entity public class Engineer implements Serializable { @Id private int id; private String name; @ManyToMany @JoinTable(name="ENGINEER_PROJECT", joinColumns=@JoinColumn(name="ENGINEER_ID"), inverseJoinColumns=@JoinColumn(name="PROJECT_ID")) private Collection<Project> projects; } @Entity public class Project implements Serializable { @Id private int id; private String title; @ManyToMany(mappedBy="projects") private Collection<Engineer> engineers; }
Pitfall: Many-to-Many (one direction)
Not specifying the mappedBy element in a many-to-many relationship, assumes two join tables and should be avoided.@Entity class A { @Id int id; @ManyToMany Collection<B> listOfB; } @Entity class B { @Id int id; @ManyToMany Collection<A> listOfA; }The same happens when the ManyToMany annotation is ommitted on one side.
@Entity class A { @Id int id; @ManyToMany Collection<B> listOfB; } @Entity class B { @Id int id; Collection<A> listOfA; }To avoid this pitfall, I would follow the instructions of Many-to-Many (both directions) relationship.
Subscribe to:
Posts (Atom)
-
Association, Aggregation, Composition, Abstraction, Generalization, Realization, Dependency....go to below link... http://javapapers.co...
-
EJB Interview Question and Answer Question1: What do you mean by EJB? Ans: Most of the time this EJB interview questions is the...