Hibernat之关系的处理一对多/多对一

第一步:编写两个pojo,比如一个学生表一个班级表  这里使用注解。

需要

班级表:

 1 package com.qcf.pox;

 2 

 3 import java.util.HashSet;

 4 import java.util.Set;

 5 

 6 import javax.persistence.CascadeType;

 7 import javax.persistence.Column;

 8 import javax.persistence.Entity;

 9 import javax.persistence.GeneratedValue;

10 import javax.persistence.GenerationType;

11 import javax.persistence.Id;

12 import javax.persistence.OneToMany;

13 import javax.persistence.Table;

14 

15 import org.hibernate.annotations.Cascade;

16 import org.hibernate.annotations.Formula;

17 //实体类

18 @Entity

19 @Table(name="_class")

20 public class ClassName {

21     @Id

22     @GeneratedValue(strategy=GenerationType.AUTO)//设置id生成方式

23     private int classid;

24     @Column(name="classname")

25     private String name;

26     /*

27      * 用一个查询语句动态的生成一个类的属性. 表示这个属性是一个虚拟的列,表中并没有这个列。需要通过查询语句计算出来。

28      */

29     //@Formula("(select count(*) from student s where s.className_classid=classid)")

30     private int num;

31     /*

32      * mappedBy指定谁来维护关系,mappedBy在一方表示由多方来维护关系,在进行操作时要注意不要使用这一方来save,update等操作

33      */

34     @OneToMany(mappedBy="classname",cascade=CascadeType.ALL)

35     private Set<Student> students=new HashSet<Student>();

36     

37     public ClassName(int classid, String name, int num, Set<Student> students) {

38         super();

39         this.classid = classid;

40         this.name = name;

41         this.num = num;

42         this.students = students;

43     }

44     public Set<Student> getStudents() {

45         return students;

46     }

47     public void setStudents(Set<Student> students) {

48         this.students = students;

49     }

50     public int getClassid() {

51         return classid;

52     }

53     public void setClassid(int classid) {

54         this.classid = classid;

55     }

56     public String getName() {

57         return name;

58     }

59     public void setName(String name) {

60         this.name = name;

61     }

62     public int getNum() {

63         return num;

64     }

65     public void setNum(int num) {

66         this.num = num;

67     }

68     public ClassName(int classid, String name, int num) {

69         super();

70         this.classid = classid;

71         this.name = name;

72         this.num = num;

73     }

74     public ClassName() {

75         super();

76     }

77     

78 

79 }
View Code

学生表:

 1 package com.qcf.pox;

 2 

 3 import java.util.Date;

 4 

 5 import javax.persistence.Entity;

 6 import javax.persistence.FetchType;

 7 import javax.persistence.GeneratedValue;

 8 import javax.persistence.GenerationType;

 9 import javax.persistence.Id;

10 import javax.persistence.ManyToOne;

11 

12 @Entity 

13 public class Student {

14     

15     @Id

16     @GeneratedValue(strategy=GenerationType.AUTO)//代表主键的生成策略

17     private int stuno;

18     private String stuname;

19     private Date birthday;

20     

21     @ManyToOne

22     private ClassName classname;

23     public int getStuno() {

24         return stuno;

25     }

26     public ClassName getClassName() {

27         return classname;

28     }

29     public void setClassName(ClassName className) {

30         this.classname = className;

31     }

32     public void setStuno(int stuno) {

33         this.stuno = stuno;

34     }

35     public String getStuname() {

36         return stuname;

37     }

38     public void setStuname(String stuname) {

39         this.stuname = stuname;

40     }

41     public Date getBirthday() {

42         return birthday;

43     }

44     public void setBirthday(Date birthday) {

45         this.birthday = birthday;

46     }

47     public Student() {

48         super();

49     }

50     public Student(int stuno, String stuname, Date birthday) {

51         super();

52         this.stuno = stuno;

53         this.stuname = stuname;

54         this.birthday = birthday;

55     }

56     

57 }
View Code

第二步:在hibernate.cfg.xml文件中引入这两个po类

1         <!-- 引入映射文件 -->

2 

3         <mapping class="com.qcf.pox.Student"/>

4         <mapping class="com.qcf.pox.ClassName"/>
View Code


第三步:编写测试代码

 

 1 package com.qcf.test;

 2 

 3 import java.util.Date;

 4 

 5 import org.hibernate.Session;

 6 import org.hibernate.SessionFactory;

 7 import org.hibernate.Transaction;

 8 import org.hibernate.cfg.AnnotationConfiguration;

 9 import org.hibernate.cfg.Configuration;

10 

11 import com.qcf.pox.ClassName;

12 import com.qcf.pox.Student;

13 

14 public class TestManyToOne {

15     public static void main(String[] args) {

16         //获取hibernate配置文件并取得session对象

17         Configuration configuration=new AnnotationConfiguration().configure();

18         SessionFactory factory=configuration.buildSessionFactory();

19         Session session=factory.openSession();

20         //开启事务

21         Transaction transaction=session.beginTransaction();

22         //创建classname实例

23         ClassName className=new ClassName();

24         className.setName("java");

25         

26         //创建student实例

27         Student student=new Student();

28         

29         student.setStuname("zhangsan");

30         student.setBirthday(new Date());

31         student.setClassName(className);

32         session.save(className);

33         session.save(student);

34         //提交事务

35         transaction.commit();

36         //关闭session

37         session.close();

38     }

39 

40 }
View Code

 

问题:

  在测试代码中,我让学生多方来维护关系,而且级联设置的为全部,这里为什么在测试代码中还有先save班级表再save学生呢?为什么不能只在save学生表?
 

你可能感兴趣的:(一对多)