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