使用annotation配置hibernate(2):一对多关系配置

数据表结构

 

 

单向一对多

Student.java

 1 @Entity
 2 @Table(name = "student")
 3 public class Student {
 4     @Id
 5     @Column(name = "id")
 6     @GenericGenerator(name = "generator",strategy = "native")
 7     @GeneratedValue(generator = "generator")
 8     private int id;
 9     
10     @Column(name = "name")
11     private String name;
12     
13     @ManyToOne(cascade=CascadeType.ALL,fetch=FetchType.EAGER)
14     @JoinColumn(name="grade_id")
15     private Grade grade;
16 }

Grade.java

 1 @Entity
 2 @Table(name="grade")
 3 public class Grade {
 4     @Id
 5     @Column(name = "id")
 6     @GenericGenerator(name = "generator", strategy = "native")
 7     @GeneratedValue(generator = "generator")
 8     private int id;
 9     
10     @Column(name = "name")
11     private String name;
12 }

 

单向多对一

Student.java

@Entity
@Table(name = "student")
public class Student {
    @Id
    @Column(name = "id")
    @GenericGenerator(name = "generator",strategy = "native")
    @GeneratedValue(generator = "generator")
    private int id;
    
    @Column(name = "name")
    private String name;
    
    @ManyToOne(cascade=CascadeType.ALL,fetch=FetchType.EAGER)
    @JoinColumn(name="grade_id")
    private Grade grade;
}

  Grade.java

 1 @Entity
 2 @Table(name="grade")
 3 public class Grade {
 4     @Id
 5     @Column(name = "id")
 6     @GenericGenerator(name = "generator", strategy = "native")
 7     @GeneratedValue(generator = "generator")
 8     private int id;
 9     
10     @Column(name = "name")
11     private String name;
12 }

 

双向关联

Student.java

 1 @Entity
 2 @Table(name = "student")
 3 public class Student {
 4     @Id
 5     @Column(name = "id")
 6     @GenericGenerator(name = "generator",strategy = "native")
 7     @GeneratedValue(generator = "generator")
 8     private int id;
 9     
10     @Column(name = "name")
11     private String name;
12     
13     @ManyToOne(cascade=CascadeType.ALL,fetch=FetchType.EAGER)
14     @JoinColumn(name="grade_id")
15     private Grade grade;
16 }

 Grade.java

 1 @Entity
 2 @Table(name="grade")
 3 public class Grade {
 4     @Id
 5     @Column(name = "id")
 6     @GenericGenerator(name = "generator", strategy = "native")
 7     @GeneratedValue(generator = "generator")
 8     private int id;
 9     
10     @Column(name = "name")
11     private String name;
12     
13     @OneToMany(cascade=CascadeType.ALL,fetch=FetchType.EAGER,mappedBy="grade")
14     private Set<Student> students = new HashSet<Student>(); 
15 }

  

自身一对多

Category.java

 1 @Entity
 2 @Table(name = "category")
 3 public class Category {
 4     @Id
 5     @Column(name = "id")
 6     @GenericGenerator(name = "generator",strategy = "native")
 7     @GeneratedValue(generator = "generator")
 8     private int id;
 9     
10     @OneToMany(cascade=CascadeType.ALL,fetch=FetchType.LAZY,mappedBy="category")
11     private Set<Category> categorys = new HashSet<Category>();
12     
13     @ManyToOne(cascade=CascadeType.ALL,fetch=FetchType.EAGER)
14     @JoinColumn(name="father_category")
15     private Category category;
16 
17 }

 

关于mappedBy关键字的具体作用可以参考 http://xiaoruanjian.iteye.com/blog/1094253

使用annotation配置hibernate(2):一对多关系配置,古老的榕树,5-wow.com

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。