首页 | 互联网 | IT动态 | IT培训 | Cisco | Windows | Linux | Java | .Net | Oracle | 软件测试 | C/C++ | 嵌入式开发 | 存储世界 | 服务器
网络设备 | IDC | 安全 | 求职招聘 | 数字网校 | 网页设计 | 平面设计 | 技术专题 | 电子书下载 | 教学视频 | 源码下载 | 搜索 | 博客 | 论坛
中国IT实验室Linux频道
中国IT教育
Google
首页 资讯动态 认证考试 新手入门 核心技术 高级技术 J2EE J2ME Java&XML 开源技术 其他技术 RSS订阅 论坛 专题
您现在的位置: 中国IT实验室 >> Java >> 高级技术 >> 设计模式 >> 正文

Hibernate一对多单向关系

 

 

5.   数据库操作类

BusinessService.java

package mypack;

 

import net.sf.hibernate.*;

import net.sf.hibernate.cfg.Configuration;

import java.util.*;

 

public class BusinessService{

  //session工厂类

  public static SessionFactory sessionFactory;

 

  //实始化session工厂

  static{

     try{

       //建立配置类,添加Student类和Teacher

       Configuration config = new Configuration();

       config.addClass(Student.class)

             .addClass(Teacher.class);

      //得到sessionFactory对象

      sessionFactory = config.buildSessionFactory();

    }catch(Exception e){e.printStackTrace();}

  }

 

  /**

   * 通过学生类,查找教师类

   * @param student Student

   * @throws Exception

   * @return List

   */

  public List findTeacherByStudent(Student student) throws Exception{

    Session session = sessionFactory.openSession();

    Transaction tx = null;

    try {

      tx = session.beginTransaction();

 

      List orders=(List)session.find("from Student as o where o.teacher.id="+student.getId());

      tx.commit();

      return orders;

    }catch (Exception e) {

      if (tx != null) {

        tx.rollback();

      }

      throw e;

    } finally {

      session.close();

    }

  }

 

  /**

   * 查找指定id的学生类

   * @param student_id long

   * @throws Exception

   * @return Student

   */

  public Student findStudent(long student_id) throws Exception{

    Session session = sessionFactory.openSession();

    Transaction tx = null;

    try {

      tx = session.beginTransaction();

      Student student=(Student)session.load(Student.class,new Long(student_id));

      tx.commit();

      return student;

    }catch (Exception e) {

      if (tx != null) {

        //发生错误,回滚

        tx.rollback();

      }

      throw e;

    } finally {

      //没有错误,关闭session

      session.close();

    }

  }

 

  /**

   * 级连保存Teacher对象和Student对象

   * @throws Exception

   */

  public void saveTeacherAndStudentWithCascade() throws Exception{

    Session session = sessionFactory.openSession();

    Transaction tx = null;

    try {

      tx = session.beginTransaction();

 

      Teacher teacher=new Teacher("myTeacher");

      Student student1=new Student("student1",teacher);

      Student student2=new Student("student2",teacher);

 

      session.save(student1);

      session.save(student2);

 

      tx.commit();

 

    }catch (Exception e) {

      if (tx != null) {

        //发生错误,回滚

        tx.rollback();

      }

      e.printStackTrace();

    } finally {

      // 没有错误,关闭session

      session.close();

    }

  }

 

  /**

   * 保存教师和学生对象

   * @throws Exception

   */

  public void saveTeacherAndStudent() throws Exception{

    Session session = sessionFactory.openSession();

    Transaction tx = null;

    try {

      tx = session.beginTransaction();

 

      Teacher teacher=new Teacher("teacher1");

      session.save(teacher);

 

      Student student1=new Student("student001",teacher);

      Student student2=new Student("student002",teacher);

      session.save(student1);

      session.save(student2);

      //提交事务

      tx.commit();

 

    }catch (Exception e) {

      if (tx != null) {

        //发生错误,回滚

        tx.rollback();

      }

      throw e;

    } finally {

      // 没有错误,关闭session

      session.close();

    }

  }

 

  /**

   * 输出学生对象集合

   * @param students List

   */

  public void printStudents(List students){

      for (Iterator it = students.iterator(); it.hasNext();) {

         Student student=(Student)it.next();

         System.out.println("OrderNumber of "+student.getTeacher().getTeacherName()+ " :"+student.getStudentName());

      }

  }

 

  /**

   * 测试方法

   * @throws Exception

   */

  public void test() throws Exception{

      saveTeacherAndStudent();

//      saveTeacherAndStudentWithCascade();

//      Student student=findStudent(1);

//      List students=findTeacherByStudent(student);

//      printStudents(students);

  }

 

  public static void main(String args[]) throws Exception {

    new BusinessService().test();

    sessionFactory.close();

  }

}

 

 

目录结构示意:

Classes

                  Hibernate.property

       /mypack

                  Teacher.java

                              Student.java

                  BusinessService.java

                  Teacher.hbm.xml

                              Student.hbm.xml      

上一页  [1] [2] 

【责编:wayen】

中国IT教育

相关产品和培训
文章评论
 友情推荐链接
 认证培训
 专题推荐

 ·关于Java框架技术专题
 ·XML全攻略技术专题
 ·JAVA开源技术介绍专题
 ·Java嵌入式开发之J2ME技术专题
 ·超前体验 Oracle 11g的5个新特性…
 ·揭密使用VB.NET的五个实用技巧
 ·Oracle和SQL Server常用函数对比专题…
 ·展现C#世界 C#程序设计专题…
 ·Java入门 Tomcat的配置技巧精华专题…
 ·Oracle RMAN物理备份技术详解…
 今日更新
 社区讨论
 博客论点
 频道精选
 Java 频道导航