班级和学生的关联:
1 @Entity 2 @Table(name = "clazz") 3 @SequenceGenerator(name="classSEQ",sequenceName = "classSEQ") 4 public class Clazz { 5 6 @Id 7 @GeneratedValue 8 private Integer id; 9 10 private String name; 11 12 @OneToMany(mappedBy = "clazz") 13 private List<Student> students; 14 15 public Integer getId() { 16 return id; 17 } 18 19 public void setId(Integer id) { 20 this.id = id; 21 } 22 23 public String getName() { 24 return name; 25 } 26 27 public void setName(String name) { 28 this.name = name; 29 } 30 31 public List<Student> getStudents() { 32 return students; 33 } 34 35 public void setStudents(List<Student> students) { 36 this.students = students; 37 } 38 }
Student.
1 @Entity 2 @Table 3 @SequenceGenerator(name="stuSEQ",sequenceName = "stuSEQ") 4 public class Student { 5 6 @Id 7 @GeneratedValue 8 private Integer id; 9 10 private String name; 11 12 private String sex; 13 14 @ManyToOne(fetch = FetchType.EAGER,cascade = {CascadeType.PERSIST}) 15 @JoinColumn(name = "class_id")//外键为班级id 16 private Clazz clazz; 17 18 public Clazz getClazz() { 19 return clazz; 20 } 21 22 public void setClazz(Clazz clazz) { 23 this.clazz = clazz; 24 } 25 26 public Integer getId() { 27 return id; 28 29 } 30 31 public void setId(Integer id) { 32 this.id = id; 33 } 34 35 public String getName() { 36 return name; 37 } 38 39 public void setName(String name) { 40 this.name = name; 41 } 42 43 public String getSex() { 44 return sex; 45 } 46 47 public void setSex(String sex) { 48 this.sex = sex; 49 } 50 51 52 }
hibernate.cfg.xml
1 <?xml version='1.0' encoding='utf-8'?> 2 <!DOCTYPE hibernate-configuration PUBLIC 3 "-//Hibernate/Hibernate Configuration DTD//EN" 4 "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> 5 <hibernate-configuration> 6 <session-factory> 7 <property name="connection.url">jdbc:oracle:thin:@localhost:1521:orcl</property> 8 <property name="connection.username">test</property> 9 <property name="connection.password">123456</property> 10 11 <property name="dialect">org.hibernate.dialect.Oracle9iDialect</property> 12 13 <!--显示生成sql语句--> 14 <property name="show_sql">true</property> 15 <!--格式化sql语句--> 16 <property name="hibernate.format_sql">true</property> 17 <!--如果表不存在,将自动创建--> 18 <!--<property name="hibernate.hbm2ddl.auto">update</property>--> 19 20 <property name="connection.driver_class">oracle.jdbc.OracleDriver</property> 21 22 <!--使用currentSession配置事务--> 23 <property name="hibernate.current_session_context_class">thread</property> 24 25 <!--<mapping resource="com/ajax/model/Book.hbm.xml"/>--> 26 <!--<mapping resource="com/ajax/model/Category.hbm.xml"/>--> 27 <!--<mapping resource="com/ajax/model/Book.hbm.xml"/>--> 28 29 <!--以注解方式配置--> 30 <mapping class="com.ajax.model.Clazz"/> 31 <mapping class="com.ajax.model.Student"/> 32 33 <!--<mapping resource="com/model/Book.hbm.xml"/>--> 34 </session-factory> 35 </hibernate-configuration>
测试:
1 @Test 2 public void TestClazzStu(){ 3 Clazz clazz = new Clazz(); 4 clazz.setName("三年二班"); 5 6 Student stu1 = new Student(); 7 stu1.setName("张飞"); 8 stu1.setSex("男"); 9 10 Student stu2 = new Student(); 11 stu2.setName("赵云"); 12 stu2.setSex("男"); 13 14 /* 15 *注解的方式获取session 16 */ 17 Configuration cfg = new AnnotationConfiguration().configure(); 18 Session session = cfg.buildSessionFactory().openSession(); 19 session.beginTransaction(); 20 21 /* 22 *persit与save 23 *persist:将一个瞬态的实例持久化,但是并不保证标识符(主键)会立刻持久化到实例中,标识符的填入可能推到flush的时候 24 * save:把一个瞬态的实例持久化,会立刻执行 25 */ 26 27 //分别持久化两张表的相关信息 28 session.persist(clazz); 29 session.persist(stu1); 30 session.persist(stu2); 31 32 //设置班级,由于控制权配置了在多方,因此要通过student来保存实体间的关联 33 //下面是干嘛的,是用来保存两张表的依赖关系的,交由多的一方,student一方保存 34 stu1.setClazz(clazz); 35 stu2.setClazz(clazz); 36 37 session.save(stu1); 38 session.save(stu2); 39 40 session.getTransaction().commit(); 41 session.beginTransaction(); 42 43 //hql语句查询班级信息 44 Clazz c = (Clazz) session.createQuery("select c from Clazz c where c.name=:name") 45 .setParameter("name","三年二班").uniqueResult();//返回单个实体 46 //为防止Hibernate缓存,在保存为学生、班级后,执行session.refresh(c) 47 //从数据库刷新对象 48 session.refresh(c); 49 System.out.println("三年二班的所有学生:"); 50 for (Student s:c.getStudents()){ 51 System.out.println("\t 姓名:"+s.getName() +",性别:"+ 52 s.getSex()); 53 }