blogjava's web log

          blogjava's web log
          ...

          hibernate多對(duì)多例子-方便以后查看(三)

          1.建表

          create ? table ?student
          (sid?
          varchar ( 32 )? not ? null ? primary ? key ,
          ?sname?
          varchar ( 16 ),
          ?sage?
          varchar ( 16 ),
          )

          create ? table ?course
          (cid?
          varchar ( 32 )? not ? null ? primary ? key ,
          cname?
          varchar ( 16 )
          )

          create ? table ?student_course_link
          (sid?
          varchar ( 32 )? not ? null ,
          cid?
          varchar ( 32 )? not ? null ,
          primary ? key (sid,cid)
          )
          2.寫VO
          StudentVO
          package?com.test;
          import?java.util.Set;
          public?class?Student
          {
          ????
          private?String?sid;
          ????
          private?String?sname;
          ????
          private?String?sage;

          ????
          private?Set?course;
          ????
          public?Student()
          ????
          {
          ????}

          ???
          //寫上get?set
          Course vo
          package?com.test;

          import?java.util.Set;

          public?class?Course
          {
          ????
          private?String?cid;
          ????
          private?String?cname;
          ????
          private?Set?student;
          ???
          //寫上get?set

          寫配置文件
          Student.hbm.xml
          <?xml?version="1.0"?>
          <!DOCTYPE?hibernate-mapping
          ????PUBLIC?"-//Hibernate/Hibernate?Mapping?DTD//EN"
          ????"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd"
          >

          <hibernate-mapping>

          ????
          <class?name="com.test.Student"?table="student"?>

          ????????
          <id?name="sid"?type="string"?unsaved-value="null"?>
          ????????????
          <column?name="sid"?sql-type="char(32)"?not-null="true"/>
          ????????????
          <generator?class="uuid.hex"/>
          ????????
          </id>

          ????????
          <property?name="sname">
          ????????????
          <column?name="sname"?sql-type="varchar(16)"?not-null="true"/>
          ????????
          </property>

          ????????
          <property?name="sage">
          ????????????
          <column?name="sage"?sql-type="varchar(16)"?not-null="true"/>
          ????????
          </property>

          ????????
          <set?name="course"?table="student_course_link"?cascade="all"?outer-join="false">
          ????????????
          <key?column="sid"/>
          ????????????
          <many-to-many?class="com.test.Course"?column="cid"/>
          ????????
          </set>
          ???
          ????
          </class>

          </hibernate-mapping>

          Course.hbm.xml
          <?xml?version="1.0"?>
          <!DOCTYPE?hibernate-mapping
          ????PUBLIC?"-//Hibernate/Hibernate?Mapping?DTD//EN"
          ????"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd"
          >

          <hibernate-mapping>

          ????
          <class?name="com.test.Course"?table="course"?>

          ????????
          <id?name="cid"?type="string"?unsaved-value="null"?>
          ????????????
          <column?name="cid"?sql-type="char(32)"?not-null="true"/>
          ????????????
          <generator?class="uuid.hex"/>
          ????????
          </id>

          ????????
          <property?name="cname">
          ????????????
          <column?name="cname"?sql-type="varchar(16)"?not-null="true"/>
          ????????
          </property>

          ????????
          <set?name="student"?table="student_course_link"?lazy="false"?cascade="all">
          ????????????
          <key?column="cid"/>
          ????????????
          <many-to-many?class="com.test.Student"?column="sid"/>
          ????????
          </set>
          ???
          ????
          </class>

          </hibernate-mapping>

          接著把下面的hibernate.properties文件拷到classes目錄下。。這里用的是mysql
          hibernate.query.substitutions?true?1,?false?0,?yes?'Y',?no?'N'
          ##?MySQL
          hibernate.dialect?net.sf.hibernate.dialect.MySQLDialect
          hibernate.connection.driver_class?org.gjt.mm.mysql.Driver
          hibernate.connection.url?jdbc:mysql://localhost:3306/wjcms
          hibernate.connection.username?root
          hibernate.connection.password?wujun
          hibernate.connection.pool_size?1
          hibernate.proxool.pool_alias?pool1
          hibernate.show_sql?true
          hibernate.jdbc.batch_size?0
          hibernate.max_fetch_depth?1
          hibernate.cache.use_query_cache?true?
          寫測(cè)試類了..
          package?com.test;

          import?net.sf.hibernate.Session;
          import?net.sf.hibernate.SessionFactory;
          import?net.sf.hibernate.cfg.Configuration;
          import?net.sf.hibernate.*;
          import?java.util.Set;
          import?java.util.HashSet;
          import?java.sql.*;
          import?java.util.List;
          import?java.util.Iterator;

          public?class?TestManyToMany
          {
          ????SessionFactory?sf;
          ????Session?session;
          ????
          public?TestManyToMany()
          ????
          {
          ????????
          try
          ????????
          {
          ????????????Configuration?cfg?
          =?new?Configuration();
          ????????????sf?
          =?cfg.addClass(Student.class).addClass(Course.class).buildSessionFactory();
          ????????}

          ????????
          catch(HibernateException?ex)
          ????????
          {
          ????????????ex.printStackTrace();
          ????????}

          ????}

          ????
          public?void?doCreate()
          ????
          {
          ????????
          try
          ????????
          {
          ????????????session?
          =?sf.openSession();

          ????????????Student?student?
          =?new?Student();
          ????????????student.setSname(
          "小王");
          ????????????student.setSage(
          "22");

          ????????????Set?courseSet?
          =?new?HashSet();
          ????????????Course?course?
          =?null;
          ????????????
          for(int?i=0;i<2;i++)
          ????????????
          {
          ????????????????course?
          =?new?Course();
          ????????????????
          if(i==0)
          ????????????????????course.setCname(
          "c++");
          ????????????????
          else?if(i==1)
          ????????????????????course.setCname(
          "java");
          ????????????????courseSet.add(course);
          ????????????}

          ????????????student.setCourse(courseSet);
          ????????????
          ????????????session.save(student);
          ????????????session.flush();
          ????????????session.connection().commit();

          ????????}

          ????????
          catch(HibernateException?ex)
          ????????
          {
          ????????????ex.printStackTrace();
          ????????}

          ????????
          catch(SQLException?ex1)
          ????????
          {
          ????????????ex1.printStackTrace();
          ????????}

          ????????
          finally
          ????????
          {
          ????????????????
          try{
          ????????????????????session.close();
          ????????????????}

          ????????????????
          catch(HibernateException?ex2){
          ????????????????}

          ????????}


          ????}

          ????
          public?void?doQuery()
          ????
          {
          ????????
          try{
          ????????????session?
          =?sf.openSession();
          ????????????Query?q?
          =?session.createQuery("select?s?from?Student?as?s");
          ????????????List?l?
          =?q.list();
          ????????????Student?s?
          =?null;
          ????????????Course?course?
          =?null;
          ????????????
          for(int?i=0;i<l.size();i++)
          ????????????
          {
          ????????????????s?
          =?(Student)l.get(i);
          ????????????????System.out.println(
          "姓名:?"+s.getSname());
          ????????????????System.out.println(
          "年齡:?"+s.getSage());
          ????????????????System.out.println(
          "所選的課程:");
          ????????????????Iterator?it?
          =?s.getCourse().iterator();
          ????????????????
          while(it.hasNext())
          ????????????????
          {
          ????????????????????course?
          =?(Course)it.next();
          ????????????????????System.out.println(
          "課程名:?"+course.getCname());
          ????????????????}



          ????????????}


          ????????}

          ????????
          catch(HibernateException?ex){
          ????????????ex.printStackTrace();
          ????????}

          ????????
          finally{
          ????????????
          try{
          ????????????????session.close();
          ????????????}

          ????????????
          catch(HibernateException?ex2){
          ????????????}

          ????????}

          ????}

          ????
          public?static?void?main(String[]?args)
          ????
          {
          ????????TestManyToMany?t?
          =?new?TestManyToMany();
          ????????
          //t.doCreate();
          ????????t.doQuery();
          ????}

          }

          ?

          好。。可以了。。

          posted on 2006-04-08 11:11 record java and net 閱讀(25613) 評(píng)論(18)  編輯  收藏 所屬分類: java

          評(píng)論

          # re: hibernate多對(duì)多例子-方便以后查看(三) 2006-04-08 11:49 lin

          不錯(cuò)啊。。

            回復(fù)  更多評(píng)論   

          # re: hibernate多對(duì)多例子-方便以后查看(三) 2006-04-08 11:50 lin

          我會(huì)常來(lái)的。。

          希望經(jīng)常更新。。  回復(fù)  更多評(píng)論   

          # re: hibernate多對(duì)多例子-方便以后查看(三) 2006-04-08 15:20 吳某人-不斷地學(xué)習(xí)

          :)


            回復(fù)  更多評(píng)論   

          # re: hibernate多對(duì)多例子-方便以后查看(三) 2006-04-10 20:30 joyschen

          如果不用java Applet 用jsp結(jié)合java怎么實(shí)現(xiàn)曲線圖?
          謝謝  回復(fù)  更多評(píng)論   

          # re: hibernate多對(duì)多例子-方便以后查看(三) 2006-11-20 21:21 yuxb

          我按照你的寫的去做了,為什么我的錯(cuò)誤是這樣的呢》
          Exception in thread "main" org.hibernate.exception.SQLGrammarException: could not insert collection: [many_to_many.Course.student#31]
          at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:67)
          at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
          at org.hibernate.persister.collection.AbstractCollectionPersister.recreate(AbstractCollectionPersister.java:1058)
          at org.hibernate.action.CollectionRecreateAction.execute(CollectionRecreateAction.java:26)
          at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:248)
          at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:232)
          at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:143)
          at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:297)
          at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
          at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:985)
          at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:333)
          at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:106)
          at many_to_many.Test_Many_To_Many.main(Test_Many_To_Many.java:73)
          Caused by: java.sql.SQLException: [Microsoft][SQLServer 2000 Driver for JDBC][SQLServer]對(duì)象名 'Stu_cou' 無(wú)效。
          at com.microsoft.jdbc.base.BaseExceptions.createException(Unknown Source)
          at com.microsoft.jdbc.base.BaseExceptions.getException(Unknown Source)
          at com.microsoft.jdbc.sqlserver.tds.TDSRequest.processErrorToken(Unknown Source)
          at com.microsoft.jdbc.sqlserver.tds.TDSRequest.processReplyToken(Unknown Source)
          at com.microsoft.jdbc.sqlserver.tds.TDSRPCRequest.processReplyToken(Unknown Source)
          at com.microsoft.jdbc.sqlserver.tds.TDSRequest.processReply(Unknown Source)
          at com.microsoft.jdbc.sqlserver.SQLServerImplStatement.getNextResultType(Unknown Source)
          at com.microsoft.jdbc.base.BaseStatement.commonTransitionToState(Unknown Source)
          at com.microsoft.jdbc.base.BaseStatement.postImplExecute(Unknown Source)
          at com.microsoft.jdbc.base.BasePreparedStatement.postImplExecute(Unknown Source)
          at com.microsoft.jdbc.base.BaseStatement.commonExecute(Unknown Source)
          at com.microsoft.jdbc.base.BaseStatement.executeUpdateInternal(Unknown Source)
          at com.microsoft.jdbc.base.BasePreparedStatement.executeUpdate(Unknown Source)
          at org.hibernate.jdbc.NonBatchingBatcher.addToBatch(NonBatchingBatcher.java:23)
          at org.hibernate.persister.collection.AbstractCollectionPersister.recreate(AbstractCollectionPersister.java:1039)
          ... 10 more
            回復(fù)  更多評(píng)論   

          # re: hibernate多對(duì)多例子-方便以后查看(三) 2006-11-20 21:23 yuxb

          我的關(guān)聯(lián)表是Stu_cou,我一直不知道為什么?請(qǐng)指教!謝謝!  回復(fù)  更多評(píng)論   

          # re: hibernate多對(duì)多例子-方便以后查看(三) 2006-11-21 08:28 junmy

          Caused by: java.sql.SQLException: [Microsoft][SQLServer 2000 Driver for JDBC][SQLServer]對(duì)象名 'Stu_cou' 無(wú)效。






          你自己好好找找吧。。




            回復(fù)  更多評(píng)論   

          # re: hibernate多對(duì)多例子-方便以后查看(三) 2008-03-23 13:52 xx

          刪除的代碼呢?這才是關(guān)鍵  回復(fù)  更多評(píng)論   

          # re: hibernate多對(duì)多例子-方便以后查看(三) 2008-05-14 14:03

          你的鍵寫的不對(duì)就會(huì)出這和情況  回復(fù)  更多評(píng)論   

          # re: hibernate多對(duì)多例子-方便以后查看(三) 2008-08-06 00:03 stonegreen

          @yuxb
          你很可能把默認(rèn)的數(shù)據(jù)庫(kù)設(shè)錯(cuò)了,
          “對(duì)象名無(wú)效”一般是這個(gè)原因  回復(fù)  更多評(píng)論   

          # re: hibernate多對(duì)多例子-方便以后查看(三) 2008-10-14 19:11 王模

          Caused by: java.sql.SQLException: [Microsoft][SQLServer 2000 Driver for JDBC][SQLServer]對(duì)象名 'Stu_cou' 無(wú)效。

          在set里加上 schema="dbo" catalog="OA" //OA是數(shù)據(jù)庫(kù)名字
          就行了   回復(fù)  更多評(píng)論   

          # re: hibernate多對(duì)多例子-方便以后查看(三) 2008-11-01 10:45 woyuanxiaodan

          @王模
          謝謝 你
          在set里加上 schema="dbo" catalog="OA" //OA是數(shù)據(jù)庫(kù)名字
          我解決了上樓的兩個(gè)異常問(wèn)題  回復(fù)  更多評(píng)論   

          # re: hibernate多對(duì)多例子-方便以后查看(三) 2011-05-11 21:21 及時(shí)回頭

          @王模
          真的挺感謝你的 我加上了
          在set里加上 schema="dbo" catalog="OA" //OA是數(shù)據(jù)庫(kù)名字

          解決了
          Caused by: java.sql.SQLException: [Microsoft][SQLServer 2000 Driver for JDBC][SQLServer]對(duì)象名 'Stu_cou' 無(wú)效。
          異常
            回復(fù)  更多評(píng)論   

          # re: hibernate多對(duì)多例子-方便以后查看(三) [未登錄](méi) 2011-09-15 08:15 abc

          真好,分享了~  回復(fù)  更多評(píng)論   

          # re: hibernate多對(duì)多例子-方便以后查看(三) 2011-11-03 17:00 程序員之家

          不錯(cuò)正在學(xué)  回復(fù)  更多評(píng)論   

          # re: hibernate多對(duì)多例子-方便以后查看(三) 2012-07-13 17:14 勿戀勿忘

          用sturts 怎么搞?  回復(fù)  更多評(píng)論   

          # re: hibernate多對(duì)多例子-方便以后查看(三) 2012-07-13 17:16 勿戀勿忘

          js+dom+jsp+servlet(struts)+ajax+hibernate;用這些技術(shù)搞? 能搞定嗎?
          用這些技術(shù)跟hibernate結(jié)合一起用你能寫個(gè)例子嗎?  回復(fù)  更多評(píng)論   

          # re: hibernate多對(duì)多例子-方便以后查看(三) 2013-01-10 14:26 xuan ge

          @yuxb
          就是不用連接池 jdbc也要有的啊,否則怎么連到數(shù)據(jù)庫(kù)  回復(fù)  更多評(píng)論   

          導(dǎo)航

          常用鏈接

          留言簿(44)

          新聞檔案

          2.動(dòng)態(tài)語(yǔ)言

          3.工具箱

          9.文檔教程

          友情鏈接

          搜索

          最新評(píng)論

          主站蜘蛛池模板: 遵义县| 青阳县| 鄱阳县| 重庆市| 乌鲁木齐市| 开封县| 华安县| 慈利县| 太保市| 陵水| 锡林郭勒盟| 平乡县| 古丈县| 梧州市| 崇文区| 温泉县| 宜兰县| 威宁| 新源县| 莱芜市| 昌邑市| 页游| 宜宾县| 安福县| 都安| 拉萨市| 大悟县| 扎兰屯市| 武隆县| 巴彦淖尔市| 淳安县| 富顺县| 斗六市| 寻乌县| 高陵县| 库车县| 苏尼特左旗| 商丘市| 湘乡市| 云阳县| 老河口市|