Mapping relationships for EJB3 JPA database tables

Source: Internet
Author: User

1) Many-to-one mapping relationship (unidirectional)

Using foreign Key Association, the foreign key is selected on the main side, that is, the external key to be reflected in more than one side

@Entitypublic class Company implements serializable{    @Id    @Column (name = "c_id")    private int Id;    private String name;}
@Entitypublic class Employee implements Serializable {    @Id    @Column (name= "e_id")    private int Id;        private String name;        @ManyToOne    @JoinColumn (name = "c_id")    private company Company;}

2) One-to-many

1. One-way Association (one party is the main, that is, a one-to-many relationship should be reflected in a party)

Create an intermediate table with two table ID fields as content (Hibernate's build policy: Generate a foreign key on more than one side)

@Entitypublic class Company implements serializable{    @Id    @Column (name = "c_id")    private int Id;    private String name;        @OneToMany    Private Set<employee>  employee;}
@Entitypublic class Employee implements Serializable {    @Id    @Column (name= "e_id")    private int Id;        private String name;}

2. Bidirectional correlation

Does not create an intermediate table to hold a foreign key as a record on more than one side, embodying a pair of multiple

* * Unlike one-way, two-way correlation must be defined on one side of the mappedby, pointing to the many side

@Entitypublic class Company implements serializable{    @Id    @Column (name = "c_id")    private int Id;    private String name;        @OneToMany (mappedby= "companys")    private Set<Employee> employees;
@Entitypublic class Employee implements Serializable {    @Id    @Column (name= "e_id")    private int Id;        private String name;    @ManyToOne    Private company Companys;}

3) One-to-one

1. One-to-one primary key association (the resulting table does not have any additional fields, there is no intermediate table, and the constraint of the ID is the primary key)

@Entitypublic class Company implements serializable{    @Id    @Column (name = "c_id")    private int Id;    private String name;        @OneToOne    @PrimaryKeyJoinColumn    private boss boss;}
@Entitypublic class Boss {    @Id    private int Id;        private String name;        @OneToOne (mappedby= "Boss")    private company company;    }

2. A one-to-two foreign key association (a foreign key is added to the primary party A, and the foreign key is unique, and the same rule is introduced in the other party B, but no foreign key is generated, only the primary key of the party A is unique)

@Entitypublic class Company implements serializable{    @Id    @Column (name = "c_id")    private int Id;    private String name;        @OneToOne    @JoinColumn (name= "boss_id", unique=true)    private boss boss;    }
@Entitypublic class Boss {    @Id    private int Id;        private String name;        @OneToOne (mappedby= "Boss")    private company company;    }

Mapping relationships for EJB3 JPA database tables

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.