熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Java編程 >> Java開源技術 >> 正文

Hibernate一對多單向關系

2022-06-13   來源: Java開源技術 

     數據庫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);

     Teacherjava和Studentjava

  Teacherjava

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

  thisid = id;

  }

  /**

  * 得到教師名稱

  * @return String    教師名稱

  */

  public String getTeacherName() {

  return teacherName;

  }

  /**

  * 設置教師名稱

  * @param teacherName String    教師名稱

  */

  public void setTeacherName(String teacherName) {

  thisteacherName = teacherName;

  }

  /**

  * 構造函數

  * @param teacherName String

  */

  public Teacher(String teacherName) {

  thisteacherName = teacherName;

  }

  }

  Studentjava

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

  thisid = id;

  }

  /**

  * 得到學生名稱

  * @return String    學生名稱

  */

  public String getStudentName() {

  return studentName;

  }

  /**

  * 設置學生名稱

  * @param studentName String    學生名稱

  */

  public void setStudentName(String studentName) {

  thisstudentName = studentName;

  }

  /**

  * 得到教師對象

  * @return Teacher    教師對象

  */

  public Teacher getTeacher() {

  return teacher;

  }

  /**

  * 設置教師對象

  * @param teacher Teacher    教師對象

  */

  public void setTeacher(Teacher teacher) {

  thisteacher = teacher;

  }

  /**

  * 構造函數

  * @param string String

  * @param teacher Teacher

  */

  public Student(String studentName Teacher teacher) {

  thisstudentName = studentName;

  thisteacher = teacher;

  }

  }

     hibernateproperties

  ## Oracle

  hibernatedialect netsfhibernatedialectOracleDialect

  hibernatedialect netsfhibernatedialectOracleDialect

  nnectiondriver_class oraclejdbcdriverOracleDriver

  nnectionusername jbcm

  nnectionpassword jbcm

  nnectionurl jdbc:oracle:thin:@localhost::wsy

     Teacherhbmxml和Studenthbmxml

  Teacherhbmxml

  <?xml version=?>

  <!DOCTYPE hibernatemapping

  PUBLIC //Hibernate/Hibernate Mapping DTD //EN

  mappingdtd>

  <hibernatemapping >

  <class name=mypackTeacher table=teachers >

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

  <generator class=increment/>

  </id>

  <property name=teacherName type=string >

  <column name=teacherName length= />

  </property>

  </class>

  </hibernatemapping>

  Studenthbmxml

  <?xml version=?>

  <!DOCTYPE hibernatemapping

  PUBLIC //Hibernate/Hibernate Mapping DTD //EN

  mappingdtd>

  <hibernatemapping >

  <class name=mypackStudent table=students >

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

  <generator class=increment/>

  </id>

  <property name=studentName type=string >

  <column name=studentName length= />

  </property>

  <manytoone

  name=teacher

  column=teacher_id

  class=mypackTeacher

  cascade=saveupdate

  />

  </class>

  </hibernatemapping>

  

     數據庫操作類

  BusinessServicejava

  package mypack;

  import netsfhibernate*;

  import netsfhibernatecfgConfiguration;

  import javautil*;

  public class BusinessService{

  //session工廠類

  public static SessionFactory sessionFactory;

  //實始化session工廠

  static{

  try{

  //建立配置類添加Student類和Teacher類

  Configuration config = new Configuration();

  configaddClass(Studentclass)

  addClass(Teacherclass);

  //得到sessionFactory對象

  sessionFactory = configbuildSessionFactory();

  }catch(Exception e){eprintStackTrace();}

  }

  /**

  * 通過學生類查找教師類

  * @param student Student

  * @throws Exception

  * @return List

  */

  public List findTeacherByStudent(Student student) throws Exception{

  Session session = sessionFactoryopenSession();

  Transaction tx = null;

  try {

  tx = sessionbeginTransaction();

  List orders=(List)sessionfind(from Student as o where oteacherid=+studentgetId());

  mit();

  return orders;

  }catch (Exception e) {

  if (tx != null) {

  txrollback();

  }

  throw e;

  } finally {

  sessionclose();

  }

  }

  /**

  * 查找指定id的學生類

  * @param student_id long

  * @throws Exception

  * @return Student

  */

  public Student findStudent(long student_id) throws Exception{

  Session session = sessionFactoryopenSession();

  Transaction tx = null;

  try {

  tx = sessionbeginTransaction();

  Student student=(Student)sessionload(Studentclassnew Long(student_id));

  mit();

  return student;

  }catch (Exception e) {

  if (tx != null) {

  //發生錯誤回滾

  txrollback();

  }

  throw e;

  } finally {

  //沒有錯誤關閉session

  sessionclose();

  }

  }

  /**

  * 級連保存Teacher對象和Student對象

  * @throws Exception

  */

  public void saveTeacherAndStudentWithCascade() throws Exception{

  Session session = sessionFactoryopenSession();

  Transaction tx = null;

  try {

  tx = sessionbeginTransaction();

  Teacher teacher=new Teacher(myTeacher);

  Student student=new Student(studentteacher);

  Student student=new Student(studentteacher);

  sessionsave(student);

  sessionsave(student);

  mit();

  }catch (Exception e) {

  if (tx != null) {

  //發生錯誤回滾

  txrollback();

  }

  eprintStackTrace();

  } finally {

  // 沒有錯誤關閉session

  sessionclose();

  }

  }

  /**

  * 保存教師和學生對象

  * @throws Exception

  */

  public void saveTeacherAndStudent() throws Exception{

  Session session = sessionFactoryopenSession();

  Transaction tx = null;

  try {

  tx = sessionbeginTransaction();

  Teacher teacher=new Teacher(teacher);

  sessionsave(teacher);

  Student student=new Student(studentteacher);

  Student student=new Student(studentteacher);

  sessionsave(student);

  sessionsave(student);

  //提交事務

  mit();

  }catch (Exception e) {

  if (tx != null) {

  //發生錯誤回滾

  txrollback();

  }

  throw e;

  } finally {

  // 沒有錯誤關閉session

  sessionclose();

  }

  }

  /**

  * 輸出學生對象集合

  * @param students List

  */

  public void printStudents(List students){

  for (Iterator it = erator(); ithasNext();) {

  Student student=(Student)itnext();

  Systemoutprintln(OrderNumber of +studentgetTeacher()getTeacherName()+ :+studentgetStudentName());

  }

  }

  /**

  * 測試方法

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

  sessionFactoryclose();

  }

  }

  目錄結構示意

  Classes

  Hibernateproperty

  /mypack

  Teacherjava

  Studentjava

  BusinessServicejava

  Teacherhbmxml

  Studenthbmxml


From:http://tw.wingwit.com/Article/program/Java/ky/201311/28579.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.