Data table structure
One-way pair
Student.java
1 @Entity2@Table (name = "Student")3 Public classStudent {4 @Id5@Column (name = "id")6@GenericGenerator (name = "Generator", strategy = "native")7@GeneratedValue (generator = "Generator")8 Private intID;9 Ten@Column (name = "Name") One PrivateString name; A -@ManyToOne (cascade=cascadetype.all,fetch=Fetchtype.eager) -@JoinColumn (name= "grade_id") the PrivateGrade Grade; -}
Grade.java
1 @Entity2@Table (name= "Grade")3 Public classGrade {4 @Id5@Column (name = "id")6@GenericGenerator (name = "Generator", strategy = "native")7@GeneratedValue (generator = "Generator")8 Private intID;9 Ten@Column (name = "Name") One PrivateString name; A}
One-way many-to-one
Student.java
@Entity @table (name= "Student") Public classStudent {@Id @Column (name= "id") @GenericGenerator (name= "Generator", strategy = "native") @GeneratedValue (generator= "Generator") Private intID; @Column (Name= "Name") PrivateString name; @ManyToOne (Cascade=cascadetype.all,fetch=Fetchtype.eager) @JoinColumn (name= "grade_id") PrivateGrade Grade;}
Grade.java
1 @Entity2@Table (name= "Grade")3 Public classGrade {4 @Id5@Column (name = "id")6@GenericGenerator (name = "Generator", strategy = "native")7@GeneratedValue (generator = "Generator")8 Private intID;9 Ten@Column (name = "Name") One PrivateString name; A}
Bidirectional correlation
Student.java
1 @Entity2@Table (name = "Student")3 Public classStudent {4 @Id5@Column (name = "id")6@GenericGenerator (name = "Generator", strategy = "native")7@GeneratedValue (generator = "Generator")8 Private intID;9 Ten@Column (name = "Name") One PrivateString name; A -@ManyToOne (cascade=cascadetype.all,fetch=Fetchtype.eager) -@JoinColumn (name= "grade_id") the PrivateGrade Grade; -}
Grade.java
1 @Entity2@Table (name= "Grade")3 Public classGrade {4 @Id5@Column (name = "id")6@GenericGenerator (name = "Generator", strategy = "native")7@GeneratedValue (generator = "Generator")8 Private intID;9 Ten@Column (name = "Name") One PrivateString name; A -@OneToMany (cascade=cascadetype.all,fetch=fetchtype.eager,mappedby= "Grade") - Privateset<student> students =NewHashset<student>(); the}
Own one-to-many
Category.java
1 @Entity2@Table (name = "category")3 Public classCategory {4 @Id5@Column (name = "id")6@GenericGenerator (name = "Generator", strategy = "native")7@GeneratedValue (generator = "Generator")8 Private intID;9 Ten@OneToMany (cascade=cascadetype.all,fetch=fetchtype.lazy,mappedby= "category") One PrivateSet<category> Categorys =NewHashset<category>(); A -@ManyToOne (cascade=cascadetype.all,fetch=Fetchtype.eager) -@JoinColumn (name= "Father_category") the PrivateCategory category; - -}
The specific role of the Mappedby keyword can be consulted http://xiaoruanjian.iteye.com/blog/1094253