<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    內蒙古java團隊

    j2se,j2ee開發組
    posts - 139, comments - 212, trackbacks - 0, articles - 65
      BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理

    Hibernate一對多單向關系

    Posted on 2007-01-09 14:41 帥子 閱讀(529) 評論(2)  編輯  收藏 所屬分類: j2se技術專區

    1.?? 數據庫 schema

    teachers :

    create table teachers

    (

    ? id????????? number(10) not null,

    ? teachername varchar2(15)

    )

    alter table teachers

    ? add constraint dere primary key (id)

    ?

    students 表:

    create table students

    (

    ? id????????? number(10) not null,

    ? studentname varchar2(15),

    ? teacher_id? number(10)

    )

    alter table students

    ? add constraint rere primary key (id)

    alter table students

    ? add constraint fff foreign key (teacher_id)

    ? references teachers (id);

    ?

    2.?? teacher.java student.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;

    ? }

    }

    ?

    ?

    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;

    ? }

    }

    3.?? hibernate.properties

    ## oracle

    ?

    hibernate.dialect net.sf.hibernate.dialect.oracle9dialect

    hibernate.dialect net.sf.hibernate.dialect.oracledialect

    hibernate.connection.driver_class oracle.jdbc.driver.oracledriver

    hibernate.connection.username jbcm

    hibernate.connection.password jbcm

    hibernate.connection.url jdbc:oracle:thin:@localhost:1521:wsy

    ?

    4.?? teacher.hbm.xml student.hbm.xml

    ?

    <?xml version="1.0"?>

    <!doctype hibernate-mapping

    public "-//hibernate/hibernate mapping dtd 2.0//en"

    "http://hibernate.sourceforge.net/hibernate-mapping-2.0.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="15" />

    ??? </property>

    ?

    ? </class>

    ?

    </hibernate-mapping>

    ?

    ?

    <?xml version="1.0"?>

    <!doctype hibernate-mapping

    public "-//hibernate/hibernate mapping dtd 2.0//en"

    "http://hibernate.sourceforge.net/hibernate-mapping-2.0.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="15" />

    ??? </property>

    ?

    ?? <many-to-one

    ??????? name="teacher"

    ?????? ?column="teacher_id"

    ??????? class="mypack.teacher"

    ??????? cascade="save-update"

    ?????? />

    ?

    ? </class>

    ?

    </hibernate-mapping>

    5.?? 數據庫操作類

    ?

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

    ? }

    }


    評論

    # re: Hibernate一對多單向關系  回復  更多評論   

    2007-01-16 17:35 by ss
    很不錯,值的餓學習。
    很想認識你,看來你對hibernate已經很熟悉了,想與你交流,交流。
    謝謝

    # re: Hibernate一對多單向關系[未登錄]  回復  更多評論   

    2007-09-07 14:36 by swordman
    有沒有人可以搞點hibernate深層次的東西出來!!!小學水平!!
    主站蜘蛛池模板: 久久久亚洲裙底偷窥综合| 日韩午夜免费视频| 国产卡一卡二卡三免费入口 | 午夜宅男在线永久免费观看网| 亚洲av色福利天堂| 真人无码作爱免费视频| 无码成A毛片免费| 香蕉蕉亚亚洲aav综合| 精品无码AV无码免费专区| 亚洲综合网美国十次| 精品多毛少妇人妻AV免费久久| 久久夜色精品国产亚洲av| 亚洲国产精品免费在线观看| 皇色在线免费视频| 久久久久久久综合日本亚洲 | 少妇高潮太爽了在线观看免费| 亚洲一区在线视频观看| 久久久久国色AV免费观看性色| 亚洲国产AV一区二区三区四区| 最近免费中文字幕大全免费版视频 | 久久青草免费91线频观看不卡 | 国产在线98福利播放视频免费| 猫咪免费人成网站在线观看入口| 国产av无码专区亚洲国产精品| 波多野结衣免费一区视频 | 国内精品免费视频精选在线观看| 四虎免费久久影院| 亚洲成a人片在线观看精品| 久久99国产乱子伦精品免费| 亚洲精品综合在线影院| 男人的天堂亚洲一区二区三区 | 久久精品国产免费一区| 亚洲av无码一区二区三区天堂古代 | 杨幂最新免费特级毛片| 欧洲亚洲国产清在高| 国产在线a免费观看| 九九免费精品视频在这里| 久久丫精品国产亚洲av| 国产成人免费a在线视频色戒| 国产偷伦视频免费观看| 亚洲国产成人久久综合|