- A foreign key is held by one of the entities (note that this FK column in the database should be constrained unique to simulate one-to-one multiplicity).
- Associated entities share the same primary keys values.
- Association table is used to store the link between the 2 entities (a unique constraint has to be defined on each FK to ensure the one to one multiplicity).
In this tutorial we will explain One-To-One Bidirectional mapping Using Foreign-key.
In this tutorial we will explain how to define bidirectional one to one assocation in JPA. This is actually continuation of our previous tutorial One-To-One Uidirectional Using Foreign-key. In order to create bidirectional association, we need to first create unidirectional association which is explained in our previous tutorial.
One-To-One Bidirectional Using Foreign-key
In our previous tutorial we had defined a unidirectional association between Person and Passport entity where Person was the owning side of the association. As a result of which if we have a fetched person object, we will be able to retrieve the corresponding passport. Vice a versa was not possible as no mapping to Person entity was defned on the Passport entity. In this tutorial we will define the inverse one to one association from Passport to Person. Note that the change is only in the Passport entity. With this change if we have Passport object, we will be able to get the corresponding Person object and vice versa as this becomes a bidirectional association.In a bidirectional relationship, the side that stores the data (the Person class in our example which contains the foreign key) is the owner. Note that only changes to the owner side affect the database.
@OneToOne annotation indicates that there is one to one relationship between Person and Passport. The mappedBy element (mappedBy=”passport”) specifies that the person field is an inverse field rather than a persistent field. The content of the person is not stored as part of a Passport entity. Instead, person is automatically populated when a Passport entity is retrieved from the database.
////-----------------------------------------------------------------------------------------------------------------------////
@Entity
@Table(name = "PERSON")
public class Person implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.TABLE,generator="GEN_PERSON")
@Column(name = "PERSON_ID")
private Long id;
@Column(name = "NAME")
private String name;
@OneToOne(cascade=CascadeType.ALL,fetch=FetchType.LAZY)
@JoinColumn(name="PASS_ID",insertable=true,updatable=true,nullable=true,unique=true)
private Passport passport;
public Person() {
super();
}
public Long getId() {
return this.id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
}
///------------------------------------------------------------------------------------------------------------//////////////
@Entity
@Table(name="PASSPORT")
public class Passport implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.TABLE,generator="GEN_PASSPORT")
@Column(name = "PASSPORT_ID")
private Long passportId;
@Column(name = "PASSPORT_NO")
private String passportNo;
@OneToOne(mappedBy="passport")
private Person person;
public Passport() {
super();
}
public Long getPassportId() {
return passportId;
}
public void setPassportId(Long passportId) {
this.passportId = passportId;
}
public String getPassportNo() {
return passportNo;
}
public void setPassportNo(String passportNo) {
this.passportNo = passportNo;
}
public Person getPerson() {
return person;
}
public void setPerson(Person person) {
this.person = person;
}
}
Desde el EJB desde donde tenemos la clase que carga los datos colocamos
public static void createPerson(){
entityManager.getTransaction().begin();
Person person = new Person();
person.setName("Sara Kamerkar");
Passport passport = new Passport();
passport.setPassportNo("GHABD877H");
person.setPassport(passport);
entityManager.persist(person);
entityManager.getTransaction().commit();
}
de esta manera el registro que entre alli, el pasaporte, sera unico no podra ser tomado por nadie mas, ya si quisiesemos que un pasaporte lo tengan muchas personas, ya seria una Relacion @ManyToOne, donde muchos usuarios pueden tener un solo pasaporte "es un ejemplo ilustrativo"
No comments:
Post a Comment