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

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

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

    blogjava's web log

    blogjava's web log
    ...

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

    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?
    寫測試類了..
    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 閱讀(25611) 評論(18)  編輯  收藏 所屬分類: java

    評論

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

    不錯啊。。

      回復  更多評論   

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

    我會常來的。。

    希望經常更新。。  回復  更多評論   

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

    :)


      回復  更多評論   

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

    如果不用java Applet 用jsp結合java怎么實現曲線圖?
    謝謝  回復  更多評論   

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

    我按照你的寫的去做了,為什么我的錯誤是這樣的呢》
    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]對象名 'Stu_cou' 無效。
    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
      回復  更多評論   

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

    我的關聯表是Stu_cou,我一直不知道為什么?請指教!謝謝!  回復  更多評論   

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

    Caused by: java.sql.SQLException: [Microsoft][SQLServer 2000 Driver for JDBC][SQLServer]對象名 'Stu_cou' 無效。






    你自己好好找找吧。。




      回復  更多評論   

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

    刪除的代碼呢?這才是關鍵  回復  更多評論   

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

    你的鍵寫的不對就會出這和情況  回復  更多評論   

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

    @yuxb
    你很可能把默認的數據庫設錯了,
    “對象名無效”一般是這個原因  回復  更多評論   

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

    Caused by: java.sql.SQLException: [Microsoft][SQLServer 2000 Driver for JDBC][SQLServer]對象名 'Stu_cou' 無效。

    在set里加上 schema="dbo" catalog="OA" //OA是數據庫名字
    就行了   回復  更多評論   

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

    @王模
    謝謝 你
    在set里加上 schema="dbo" catalog="OA" //OA是數據庫名字
    我解決了上樓的兩個異常問題  回復  更多評論   

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

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

    解決了
    Caused by: java.sql.SQLException: [Microsoft][SQLServer 2000 Driver for JDBC][SQLServer]對象名 'Stu_cou' 無效。
    異常
      回復  更多評論   

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

    真好,分享了~  回復  更多評論   

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

    不錯正在學  回復  更多評論   

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

    用sturts 怎么搞?  回復  更多評論   

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

    js+dom+jsp+servlet(struts)+ajax+hibernate;用這些技術搞? 能搞定嗎?
    用這些技術跟hibernate結合一起用你能寫個例子嗎?  回復  更多評論   

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

    @yuxb
    就是不用連接池 jdbc也要有的啊,否則怎么連到數據庫  回復  更多評論   

    導航

    常用鏈接

    留言簿(44)

    新聞檔案

    2.動態語言

    3.工具箱

    9.文檔教程

    友情鏈接

    搜索

    最新評論

    主站蜘蛛池模板: 玖玖在线免费视频| 亚洲AV电影院在线观看| 欧洲人成在线免费| 黄页网站在线视频免费| 亚洲国产精品成人精品小说| 亚洲乱码精品久久久久..| 男女啪啪永久免费观看网站| 8090在线观看免费观看| 国产免费黄色无码视频| 亚洲av成人一区二区三区观看在线| 亚洲视频在线观看一区| 国产亚洲精品福利在线无卡一| 午夜免费不卡毛片完整版| 99精品一区二区免费视频| 拍拍拍无挡免费视频网站| 男性gay黄免费网站| 亚洲熟妇无码AV不卡在线播放 | 亚洲一级黄色大片| 亚洲AV无码精品色午夜果冻不卡 | 亚洲毛片在线观看| 亚洲人色婷婷成人网站在线观看| 亚洲精品动漫人成3d在线| 日韩精品无码人妻免费视频 | 亚洲小视频在线播放| 亚洲一区二区三区电影| 亚洲va无码va在线va天堂| 久久久久久久综合日本亚洲| 黑人大战亚洲人精品一区| 亚洲一级特黄大片无码毛片| 国产网站免费观看| 国产成人免费A在线视频| 在线成人a毛片免费播放| 免费看AV毛片一区二区三区| 男女做羞羞的事视频免费观看无遮挡 | 九九精品成人免费国产片| a级精品九九九大片免费看| 少妇性饥渴无码A区免费 | 成人免费视频软件网站| 四虎影院免费在线播放| 最好免费观看韩国+日本| 日韩免费高清一级毛片在线|