Implement one-to-one (one-to-one) mappings for entity beans
One-to-one mapping is very common. In general, a primary table forms a one-to-one relationship from a table through a foreign key. @OneToOne annotations are used in EJB3 to map. One-to-one relationships in addition to using foreign keys, you can connect two tables in the same way as a shared primary key. First look at the structure of the following two tables:
Figure 1 T_customers
Figure 2 T_referees
T_customers and T_referees tables form a one-to-one relationship. The T_customers table is connected to the T_referees table by referee_id. The code for the entity bean corresponding to the T_customers table is as follows:
Package entity;
Import java.util.Collection;
Import javax.persistence.Entity;
Import Javax.persistence.FetchType;
Import Javax.persistence.GeneratedValue;
Import Javax.persistence.GenerationType;
Import Javax.persistence.Id;
Import Javax.persistence.JoinColumn;
Import Javax.persistence.OneToOne;
Import javax.persistence.Table;
@Entity
@Table (name = "T_customers")
public class Customer
{
private int id;
private String name;
Private referee Referee;
@Id
@GeneratedValue (strategy = generationtype.identity)
public int getId ()
{
return ID;
}
public void setId (int id)
{
This.id = ID;
}
Public String GetName ()
{
return name;
}
public void SetName (String name)
{
THIS.name = name;
}
@OneToOne
Public referee Getreferee ()
{
return referee;
}
public void Setreferee (referee referee)
{
This.referee = Referee;
}
}