知识大全 Hibernate一对多单向关系

Posted 教师

篇首语:金鞍玉勒寻芳客,未信我庐别有春。本文由小常识网(cha138.com)小编为大家整理,主要介绍了知识大全 Hibernate一对多单向关系相关的知识,希望对你有一定的参考价值。

Hibernate一对多单向关系  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!

      数据库schema

  Teachers表:

  create table TEACHERS

  (

  ID          NUMBER( ) not null

  TEACHERNAME VARCHAR ( )

  )

  alter table TEACHERS

  add constraint DERE primary key (ID)

  Students表

  create table STUDENTS

  (

  ID          NUMBER( ) not null

  STUDENTNAME VARCHAR ( )

  TEACHER_ID  NUMBER( )

  )

  alter table STUDENTS

  add constraint RERE primary key (ID)

  alter table STUDENTS

  add constraint FFF foreign key (TEACHER_ID)

  references TEACHERS (ID);

      Teacher java和Student java

  Teacher java

  package mypack;

  public class Teacher

  //教师id

  private Long id;

  //教师名称

  private String teacherName;

  /**

  * 缺省构造函数

  */

  public Teacher()

  

  /**

  * 得到教师id

  * @return Long    教师id

  */

  public Long getId()

  return id;

  

  /**

  * 设置教师id

  * @param id Long    教师id

  */

  public void setId(Long id)

  this id = id;

  

  /**

  * 得到教师名称

  * @return String    教师名称

  */

  public String getTeacherName()

  return teacherName;

  

  /**

  * 设置教师名称

  * @param teacherName String    教师名称

  */

  public void setTeacherName(String teacherName)

  this teacherName = teacherName;

  

  /**

  * 构造函数

  * @param teacherName String

  */

  public Teacher(String teacherName)

  this teacherName = teacherName;

  

  

  Student java

  package mypack;

  public class Student

  //学生id

  private Long id;

  //学生名称

  private String studentName;

  //教师类

  private Teacher teacher;

  /**

  * 缺省构造函数

  */

  public Student()

  

  /**

  * 得到学生id

  * @return Long    学生id

  */

  public Long getId()

  return id;

  

  /**

  * 设置学生id

  * @param id Long    学生id

  */

  public void setId(Long id)

  this id = id;

  

  /**

  * 得到学生名称

  * @return String    学生名称

  */

  public String getStudentName()

  return studentName;

  

  /**

  * 设置学生名称

  * @param studentName String    学生名称

  */

  public void setStudentName(String studentName)

  this studentName = studentName;

  

  /**

  * 得到教师对象

  * @return Teacher    教师对象

  */

  public Teacher getTeacher()

  return teacher;

  

  /**

  * 设置教师对象

  * @param teacher Teacher    教师对象

  */

  public void setTeacher(Teacher teacher)

  this teacher = teacher;

  

  /**

  * 构造函数

  * @param string String

  * @param teacher Teacher

  */

  public Student(String studentName Teacher teacher)

  this studentName = studentName;

  this teacher = teacher;

  

  

      hibernate properties

  ## Oracle

  hibernate dialect net sf hibernate dialect Oracle Dialect

  hibernate dialect net sf hibernate dialect OracleDialect

  nnection driver_class oracle jdbc driver OracleDriver

  nnection username jbcm

  nnection password jbcm

  nnection url jdbc:oracle:thin:@localhost: :wsy

      Teacher hbm xml和Student hbm xml

  Teacher hbm xml

  <?xml version= ?>

  <!DOCTYPE hibernate mapping

  PUBLIC //Hibernate/Hibernate Mapping DTD //EN

   mapping dtd >

  <hibernate mapping >

  <class name= mypack Teacher table= teachers >

  <id name= id type= long column= ID >

  <generator class= increment />

  </id>

  <property name= teacherName type= string >

  <column name= teacherName length= />

  </property>

  </class>

  </hibernate mapping>

  Student hbm xml

  <?xml version= ?>

  <!DOCTYPE hibernate mapping

  PUBLIC //Hibernate/Hibernate Mapping DTD //EN

   mapping dtd >

  <hibernate mapping >

  <class name= mypack Student table= students >

  <id name= id type= long column= ID >

  <generator class= increment />

  </id>

  <property name= studentName type= string >

  <column name= studentName length= />

  </property>

  <many to one

  name= teacher

  column= teacher_id

  class= mypack Teacher

  cascade= save update

  />

  </class>

  </hibernate mapping>

  

      数据库操作类

  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());

  mit();

  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));

  mit();

  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 student =new Student( student teacher);

  Student student =new Student( student teacher);

  session save(student );

  session save(student );

  mit();

  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( teacher );

  session save(teacher);

  Student student =new Student( student teacher);

  Student student =new Student( student teacher);

  session save(student );

  session save(student );

  //提交事务

  mit();

  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 = erator(); 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( );

  //      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

cha138/Article/program/Java/ky/201311/28579

相关参考

知识大全 hibernate一对多关联关系

  想了几天终于知道sql语句的发出问题查了很多书感觉都没有说清楚有的还是错的请看下面  [java]  <?xmlversion=?>  <!DOCTYPEhibernatemap

知识大全 hibernate关联关系-一对一

  Java代码  packageDomain;  publicclassPerson  publicintgetId()  returnid;    publicvoidsetId(intid)  

知识大全 Hibernate一对多双向映射及乐观锁使用

Hibernate一对多双向映射及乐观锁使用  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!  在H

知识大全 Hibernate初学之一对多、多对一关系模型

Hibernate初学之一对多、多对一关系模型  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!  举

知识大全 Hibernate的多对一和一对多操作实例

Hibernate的多对一和一对多操作实例  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!  Hib

知识大全 Hibernate中多对多关系的常见问题

Hibernate中多对多关系的常见问题  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!  1到底在

知识大全 Hibernate一对一 主键关联映射

Hibernate一对一主键关联映射  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧! &n

知识大全 Hibernate持久化技术中多对多关系应用

Hibernate持久化技术中多对多关系应用  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!  Hi

知识大全 hibernate关系映射

  一对象关系映射基础  对象间的基本关系  首先我简要阐明一下对象之间的基本关系在这以后UML的课程中也会深入的学习对象具有的四种基本关系  关联关系关联关系在设计模式中是被提倡优先使用于继承关系的

知识大全 Hibernate的继承关系

Hibernate的继承关系  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!一每个子类对应一个数据表