生活随笔
收集整理的這篇文章主要介紹了
hibernate中many-to-one实例一
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
可以發現,添加一個教師的同時指定一個學生,這種方式相當糟糕,合理的方式應該是在添加學生的時候指定教師。即在學生類里指
定教師對象,配置文件里配置成many-to-one。
單純只使用many-to-one:
在Student類里加入Teacher teacher屬性。
注釋掉teacher.hbm.xml里的<set ...></set>內容
在student.hbm.xml里入many-to-one屬性:
[c-sharp]?view plaincopyprint?
<?xml?version="1.0"?>??<!DOCTYPE?hibernate-mapping?PUBLIC??????????"-//Hibernate/Hibernate?Mapping?DTD?3.0//EN"??????????"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">??<hibernate-mapping?package="com.sjtu.xw.pojo2">??????<class?name="Student"?table="student">??????????<id?name="id"?column="id">??????????????<generator?class="native"></generator>??????????</id>??????????<property?name="stuName"?column="studentName"?length="30"?/>??????????<many-to-one?name="teacher"?column="teacherId"?class="Teacher"??????????????fetch="join"?lazy="false"?/>??????????<!--??????????????這里的column="teacherId"是指明了自身(student表)表里的teacherId屬性,指向teacher表的外鍵??????????-->??????</class>??</hibernate-mapping>?? ?
做完上述工作即可進行測試了:
Test2.java
[c-sharp]?view plaincopyprint?
package?com.sjtu.xw.test;??import?java.util.List;??import?org.hibernate.Session;??import?org.hibernate.SessionFactory;??import?org.hibernate.Transaction;??import?com.sjtu.xw.pojo2.Student;??import?com.sjtu.xw.pojo2.Teacher;??import?com.sjtu.xw.util.HibernateUtil;??public?class?Test2?{??????public?static?void?main(String[]?args)?{??????????Test2?test?=?new?Test2();??????????????????????????????test.test();??????}??????public?void?addStudent()?{??????????Teacher?teacher?=?new?Teacher();??????????teacher.setId(1);??????????Student?student?=?new?Student();??????????student.setStuName("student8");??????????student.setTeacher(teacher);??????????SessionFactory?sf?=?HibernateUtil.getSessionFactory();??????????Session?session?=?sf.openSession();??????????Transaction?tx?=?session.beginTransaction();??????????session.save(student);??????????tx.commit();??????????session.close();??????????sf.close();??????}??????public?void?addTeacher()?{??????????Teacher?teacher?=?new?Teacher();??????????teacher.setTeaName("Teacher5");??????????SessionFactory?sf?=?HibernateUtil.getSessionFactory();??????????Session?session?=?sf.openSession();??????????Transaction?tx?=?session.beginTransaction();??????????session.save(teacher);??????????tx.commit();??????????session.close();??????????sf.close();??????}??????public?void?test()?{??????????SessionFactory?sf?=?HibernateUtil.getSessionFactory();??????????Session?session?=?sf.openSession();??????????Transaction?tx?=?session.beginTransaction();??????????List?list?=?session.createQuery("from?Student?s?where?s.id=1").list();??????????Student?s?=?(Student)?list.get(0);????????????????System.out.println(s.getTeacher());??????????System.out.println(s.getTeacher().getTeaName());??????????tx.commit();??????????session.close();??????????sf.close();??????}??} ?
本文轉自xwdreamer博客園博客,原文鏈接:http://www.cnblogs.com/xwdreamer/archive/2010/12/01/2297065.html,如需轉載請自行聯系原作者
總結
以上是生活随笔為你收集整理的hibernate中many-to-one实例一的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。