Configure Hibernate with annotation (2): one-to-many relationship configuration

Source: Internet
Author: User
Tags generator

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

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.