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

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

需要

班级表:

 package com.qcf.pox;

 import java.util.HashSet;
import java.util.Set; import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table; import org.hibernate.annotations.Cascade;
import org.hibernate.annotations.Formula;
//实体类
@Entity
@Table(name="_class")
public class ClassName {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)//设置id生成方式
private int classid;
@Column(name="classname")
private String name;
/*
* 用一个查询语句动态的生成一个类的属性. 表示这个属性是一个虚拟的列,表中并没有这个列。需要通过查询语句计算出来。
*/
//@Formula("(select count(*) from student s where s.className_classid=classid)")
private int num;
/*
* mappedBy指定谁来维护关系,mappedBy在一方表示由多方来维护关系,在进行操作时要注意不要使用这一方来save,update等操作
*/
@OneToMany(mappedBy="classname",cascade=CascadeType.ALL)
private Set<Student> students=new HashSet<Student>(); public ClassName(int classid, String name, int num, Set<Student> students) {
super();
this.classid = classid;
this.name = name;
this.num = num;
this.students = students;
}
public Set<Student> getStudents() {
return students;
}
public void setStudents(Set<Student> students) {
this.students = students;
}
public int getClassid() {
return classid;
}
public void setClassid(int classid) {
this.classid = classid;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
public ClassName(int classid, String name, int num) {
super();
this.classid = classid;
this.name = name;
this.num = num;
}
public ClassName() {
super();
} }

学生表:

 package com.qcf.pox;

 import java.util.Date;

 import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToOne; @Entity
public class Student { @Id
@GeneratedValue(strategy=GenerationType.AUTO)//代表主键的生成策略
private int stuno;
private String stuname;
private Date birthday; @ManyToOne
private ClassName classname;
public int getStuno() {
return stuno;
}
public ClassName getClassName() {
return classname;
}
public void setClassName(ClassName className) {
this.classname = className;
}
public void setStuno(int stuno) {
this.stuno = stuno;
}
public String getStuname() {
return stuname;
}
public void setStuname(String stuname) {
this.stuname = stuname;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public Student() {
super();
}
public Student(int stuno, String stuname, Date birthday) {
super();
this.stuno = stuno;
this.stuname = stuname;
this.birthday = birthday;
} }

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

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

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

第三步:编写测试代码

 package com.qcf.test;

 import java.util.Date;

 import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.cfg.Configuration; import com.qcf.pox.ClassName;
import com.qcf.pox.Student; public class TestManyToOne {
public static void main(String[] args) {
//获取hibernate配置文件并取得session对象
Configuration configuration=new AnnotationConfiguration().configure();
SessionFactory factory=configuration.buildSessionFactory();
Session session=factory.openSession();
//开启事务
Transaction transaction=session.beginTransaction();
//创建classname实例
ClassName className=new ClassName();
className.setName("java"); //创建student实例
Student student=new Student(); student.setStuname("zhangsan");
student.setBirthday(new Date());
student.setClassName(className);
session.save(className);
session.save(student);
//提交事务
transaction.commit();
//关闭session
session.close();
} }

问题:

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

上一篇:KMP算法(转)


下一篇:获取Jenkins project build结果