ssh的级联学习

来源:互联网 发布:淘宝助理快递模板导入 编辑:程序博客网 时间:2024/05/17 01:16

双向one-to-many

package com;

import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import org.springframework.orm.hibernate3.LocalSessionFactoryBean;

 


import com.yourcompany.hib.HibernateSessionFactory;
import com.yourcompany.struts.AppConPath;

public class ClassListTest {

 /**
  * @param args
  */
 public static void main(String[] args) {
  // TODO Auto-generated method stub
  ApplicationContext context = new FileSystemXmlApplicationContext(AppConPath.getPath());
  
  try{
   
/**************在主表操作**************************/
   //创建Student对象
   /**
   Student st1 = new Student();
   st1.setStudentName("st1");
   Student st2 = new Student();
   st2.setStudentName("st2");
   Student st3 = new Student();
   st3.setStudentName("st3");
   Student stu = new Student();
   stu.setStudentName("stu");
   
   //创建Teacher对象
   Set<Student> hs = new HashSet<Student>();
   Teacher t = new Teacher();
   t.setTeacherName("afuer");
   hs.add(st1);
   hs.add(st2);
   hs.add(st3);
   hs.add(stu);
   t.setStudents(hs);
   */
   
   TeacherDAO s =(TeacherDAO) context.getBean("TeacherDAO");
   
/**********保存方法***********/
  /**
   * 如果配置文件中 inverse="true"
   * 则在student表中teacher_id为NULL。
   *
   */
   //s.save(t);
   
/**********更新方法**********/
   /**
   t.setId(1);
   st1.setId(1);
   stu.setId(2);
   st2.setId(3);
   st3.setId(4);
   //s.findById(t.getId());//假如用这个则TeacherDAO类中的update()不用getHibernateTemplate().load方法
   t.setTeacherName("kk");
   s.update(t);
   */
   
/**********删除方法**********/
   /**
   t.setId(1);
   s.delete(t);
   */
   
/**********查找方法**********/
   /**
    * 如果 配置文件中lazy="true"
    * 则会报错:
    *
    * 第1个老师信息 :
    * 老师id:1   老师姓名:kk
    * org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: com.Teacher.students - no session or session was closed
    * at org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationException(AbstractPersistentCollection.java:191)
    * at org.hibernate.collection.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:183)
    * at org.hibernate.collection.AbstractPersistentCollection.read(AbstractPersistentCollection.java:48)
    * at org.hibernate.collection.PersistentSet.iterator(PersistentSet.java:134)
    * at com.ClassListTest.main(ClassListTest.java:93)
    */
   List list = s.findAll();
   Iterator it = list.iterator();
   for(int i=1;it.hasNext();i++){
    Teacher teacher =(Teacher) it.next();
    System.out.println("第"+i+"个老师信息 :");
    System.out.println("老师id:"+teacher.getId()+"   老师姓名:"+teacher.getTeacherName());
    Set<Student> hs = teacher.getStudents();
    Iterator hsIt = hs.iterator();
    for(int j=1;hsIt.hasNext();j++){
     Student student = (Student) hsIt.next();
     System.out.println("第"+j+"个学生信息 :");
     System.out.println("学生id:"+student.getId()+"学生姓名:"+student.getStudentName());
    }
   }
   
   
/**************在从表操作**************************/
   /**
   StudentDAO s1= (StudentDAO) context.getBean("StudentDAO");
   Student st4 = new Student();
   st4.setStudentName("st4");
   st4.setTeacher(t);
   s1.save(st4);
   */
  }catch(Exception e){
   e.printStackTrace();
  }finally{
   System.out.println("success");//看什么时候执行完
  }
  

 }

}
/**********************************************************************/
配置文件中的inverse属性是用来优化更新速度, 如果="true",表示在执行主表的时候不更新从表的值
如果去掉或者把它="false"则会更新。
lazy,如果="true"在查询主表的时候不会去查找从表,如果去掉或者="false"则会去查询,这样会增大
查询数据的数量,同时还增大内存。如果从表数据太多,在查询的时候将会耗费大量的内存。所以程序员开发
的时候设置为"true"

/**********************************************************************/