当前位置: IT大杂烩 > Ubuntu  > 使用annotation配置hibernate(2):一对多关系配置

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

www.someabcd.com  网友分享于:Jun 8, 2018 6:07:47 PM

标签:style   blog   http   java   color   使用   

数据表结构

 bubuko.com,布布扣

 

单向一对多

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):一对多关系配置,布布扣,bubuko.com

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

标签:style   blog   http   java   color   使用   

发布此文章仅为传递网友分享,不代表本站观点,若侵权请联系我们删除,本站将不对此承担任何责任。
Copyright ©2018  IT大杂烩  版权所有  京ICP备11030978号-1 网站地图