hibernate:基于注解一对多双向关联关系外键方式

来源:互联网 发布:尽知天下事前面一句 编辑:程序博客网 时间:2024/05/22 19:36

有两个pojo类,Grade类和Student类,Grade代表一,Student代表多,测试类Test,Grade类和Student类对应的表。

//Grade类,一的一方import java.util.HashSet;import java.util.Set;import javax.persistence.CascadeType;import javax.persistence.Entity;import javax.persistence.Id;import javax.persistence.OneToMany;import javax.persistence.Table;@Entity@Table(name="grade")  //对应的表public class Grade {private Integer id;private String name;private Set<Student> students = new HashSet<Student>();public Grade() {}public Grade(Integer id, String name, Set<Student> students) {this.id = id;this.name = name;this.students = students;}@Id  //idpublic Integer getId() {return id;}public void setId(Integer id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}@OneToMany(mappedBy="grade",cascade=CascadeType.ALL) //grade为Student类中的grade属性public Set<Student> getStudents() {return students;}public void setStudents(Set<Student> students) {this.students = students;}@Overridepublic String toString() {return "Grade [id=" + id + ", name=" + name + ", students=" + students + "]";}}


//Student类,多的一方import javax.persistence.Entity;import javax.persistence.Id;import javax.persistence.JoinColumn;import javax.persistence.ManyToOne;import javax.persistence.Table;@Entity@Table(name="student1") //对应的表public class Student {private Integer id;private String name;private Grade grade;public Student(){}public Student(Integer id, String name, Grade grade) {this.id = id;this.name = name;this.grade = grade;}@Overridepublic String toString() {return "Student [id=" + id + ", name=" + name + "]";}@Idpublic Integer getId() {return id;}public void setId(Integer id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}@ManyToOne@JoinColumn(name="grade_id")  //grade_id为Student对应的表中的外键public Grade getGrade() {return grade;}public void setGrade(Grade grade) {this.grade = grade;}}

grade表


CREATE TABLE grade(  id int(11) NOT NULL,  name varchar(20) NOT NULL,  PRIMARY KEY (`id`));


student表

-------------------------------------------------------------------------+|CREATE TABLE student1 (  id int(11) NOT NULL,  name varchar(20) NOT NULL,  grade_id int(11) DEFAULT NULL,  PRIMARY KEY (id),  CONSTRAINT grade_fk_student1 FOREIGN KEY (grade_id) REFERENCES grade (id));

hibernate.cfg.xml中添加:

<mapping class="com.liukun.association.one2many.pojo.Grade"/>
<mapping class="com.liukun.association.one2many.pojo.Student"/>

//Test测试类import java.util.List;import javax.persistence.Table;import org.hibernate.Query;import org.hibernate.Session;import org.hibernate.Transaction;import com.liukun.association.one2many.pojo.Grade;import com.liukun.association.one2many.pojo.Student;import com.liukun.common.HibernateSessionFactory;public class Test {public static void main(String[] args) {Grade g = new Grade();g.setId(2);g.setName("二班");Student s = new Student();s.setId(3);s.setName("lisi");Student s1 = new Student();s1.setId(4);s1.setName("tome");g.getStudents().add(s);g.getStudents().add(s1);s.setGrade(g);s1.setGrade(g);Transaction trans = null;try{Session session = HibernateSessionFactory.getSession(); //查看我的另一篇博客多对多里面有这个方法的实现代码trans = session.beginTransaction();//Grade s = (Grade)session.get(Grade.class, 2);//System.out.println(s);session.save(g);  //Student gr = (Student)session.get(Student.class, 3);////System.out.println(gr);//session.update(gr);trans.commit();}catch(Exception ex){ex.printStackTrace();trans.rollback();}}}

一对多双向关联关系基于外键,就是一的一方类中有一个多的类的集合属性,而多的一方类中有一个一方的属性,而在多的一方对应的表中存在指向一方表的外键。

阅读全文
0 0
原创粉丝点击