Hibernate 基于List集合映射

来源:互联网 发布:项城市乡镇人口数据 编辑:程序博客网 时间:2024/04/19 19:24

基于List集合映射

1、  实体类中使用List集合

publicclass Grade {

   privateintid;

   private Stringname;

   private List<Student>students = new ArrayList<Student>(0);

   }

2、  映射文件

<hibernate-mappingpackage="cn.siggy.pojo">

   <classname="Grade">

      <idname="id">

         <generatorclass="native"></generator>

      </id>

      <propertyname="name"/>

      <listname="students"cascade="all">

         <!-- key表示外键 column外键列名-->

         <keycolumn="grade_id"></key>

         <!--在多的一端产生一列用来表示顺序如果不指明列名默认为idx

            值由hibernate来维护

          -->

         <list-indexcolumn="sort">

         </list-index>

         <!-- one-to-many一对多  Grade  students 所表示类型 -->

         <one-to-manyclass="Student"/>

      </list>

   </class>

3、  测试代码

@Test

   publicvoidtestSave() throws HibernateException, SerialException, SQLException{

      Configuration cfg = new Configuration().configure();

      SessionFactory factory = cfg.buildSessionFactory(new StandardServiceRegistryBuilder()

      .applySettings(cfg.getProperties()).build());

      Session session = null;

      Transaction tx = null;

      try{

         session = factory.openSession();

         tx = session.beginTransaction();

         Grade grade = new Grade();

         grade.setName("基础");

        

         Student stu = new Student();

         stu.setName("张三疯");

         stu.setAge(22);

         Student stu1 = new Student();

         stu1.setName("老王");

         stu1.setAge(23);

         Student stu2 = new Student();

         stu2.setName("老李");

         stu2.setAge(23);

         //关联

         grade.getStudents().add(stu);

         grade.getStudents().add(stu1);

         grade.getStudents().add(stu2);

         //保存数据的顺序是根据外键的配置来决定的

         //如果外键不能为null,那么先保存一的一端

         //如果外键可以为null,则可以随意保存

         session.save(grade);

         session.save(stu);

         session.save(stu1);

         session.save(stu2);

        

         tx.commit();

        

      }catch (HibernateException e) {

         if(tx!=null)

            tx.rollback();

         e.printStackTrace();

         throw e;

      }finally{

         HibernateUtil.closeSession();

      }

   }

   @Test

   publicvoid testGet(){

      Session session = null;

      Transaction tx = null;

      try{

         session = HibernateUtil.getSession();

         tx = session.beginTransaction();

         //取数据

         Grade grade = (Grade)session.get(Grade.class, 1);

         System.out.println("gradeName="+grade.getName());

         System.out.println("grade所对应的多的一端的数据");

         List<Student> list = grade.getStudents();

         for(Student stu:list){

            System.out.println(stu.getName());

         }

         tx.commit();

      }catch (HibernateException e) {

         if(tx!=null)

            tx.rollback();

         e.printStackTrace();

         throw e;

      }finally{

         HibernateUtil.closeSession();

      }

   }

 

原创粉丝点击