Eclipse + JBoss 5 + EJB3 Development Guide (7)

Source: Internet
Author: User
Tags jboss

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.