1 package oneToMany; 2 import java.util.Set; 3 import javax.persistence.*; 4 /* 5 注意导入时,是导入:import javax.persistence.*; 6 非导入org.hibernate的相关类:import org.hibernate.annotations.Entity; 7 */ 8 @Entity 9 @Table(name="classes") 10 public class Classes implements Serializable { 11 @Id 12 @GeneratedValue(strategy=GenerationType.AUTO) 13 private int id; 14 private String name; 15 16 @OneToMany(cascade=CascadeType.ALL,mappedBy="classes") 17 private Set<Student> students; 18 //getter,setter省略 19 } 20 21 22 package oneToMany; 23 import javax.persistence.*; 24 @Entity 25 @Table(name="student") 26 public class Student implements Serializable { 27 @Id 28 @GeneratedValue(strategy=GenerationType.AUTO) 29 private int sid; 30 31 private String sname; 32 33 //若有多个cascade,可以是:{CascadeType.PERSIST,CascadeType.MERGE} 34 @ManyToOne(cascade={CascadeType.ALL}) 35 @JoinColumn(name="classid") //student类中对应外键的属性:classid 36 private Classes classes; 37 //getter,setter省略 38 } 39 40 41 public class TestOneToMany { 42 /* 43 CREATE TABLE student ( --要定义外键!!!!!!! 44 `sid` double NOT NULL auto_increment, 45 `classid` double NULL, 46 `sname` varchar(255) NOT NULL, 47 PRIMARY KEY (sid), 48 INDEX par_ind (classid), 49 FOREIGN KEY (classid) REFERENCES classes(id) ON DELETE CASCADE ON UPDATE CASCADE 50 ) ENGINE=InnoDB 51 */ 52 public static void main(String[] args) throws SQLException 53 { 54 try 55 { 56 SessionFactory sf = new AnnotationConfiguration().configure().buildSessionFactory(); 57 Session session=sf.openSession(); 58 Transaction tx=session.beginTransaction(); 59 /* 60 因为mappedBy是定义在classes中,即classes类不负责维护级联关系.即维护者是student.所以, 61 1.要将clsses的数据,赋给student,即用student的setClasses()方法去捆定class数据; 62 2.在进行数据插入/更新session.save()/session.update()时,最后操作的是student. 63 */ 64 Classes classes=new Classes(); 65 classes.setName("access"); 66 67 Student st1=new Student(); 68 st1.setSname("jason"); 69 st1.setClasses(classes); 70 session.save(st1); 71 72 Student st2=new Student(); 73 st2.setSname("hwj"); 74 st2.setClasses(classes); 75 session.save(st2); 76 tx.commit(); 77 /* 78 输出如下: 79 Hibernate: insert into classes (name) values (?) 80 Hibernate: insert into student (classid, sname) values (?, ?) 81 Hibernate: insert into student (classid, sname) values (?, ?) 82 */ 83 /* 84 因为一端维护关系另一端不维护关系的原因,我们必须注意避免在应用中用不维护关系的类(class)建立关系,因为这样建立的关系是不会在数据库中存储的。 85 如上的代码倒过来,则插入时,student的外键值为空.如下: 86 */ 87 // Student st1=new Student(); 88 // st1.setSname("jason"); 89 // session.save(st1); 90 // 91 // Student st2=new Student(); 92 // st2.setSname("hwj"); 93 // session.save(st2); 94 // 95 // Set<Student> students=new HashSet<Student>(); 96 // students.add(st1); 97 // students.add(st2); 98 // 99 // Classes classes=new Classes(); 100 // classes.setName("access"); 101 // classes.setStudents(students); 102 // session.save(classes); 103 /* 104 输出如下: 105 Hibernate: insert into student (classid, sname) values (?, ?) 106 Hibernate: insert into student (classid, sname) values (?, ?) 107 Hibernate: insert into classes (name) values (?) 108 */ 109 } 110 catch(HibernateException e) 111 { 112 e.printStackTrace(); 113 } 114 } 115 }