<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 一對多測試-----筆記

    最近看那本深入淺出hibernate 真是很不錯啊。。講的也很細。。
    剛剛小試了一把,真的很過隱。。
    我用的是MYSQL數據庫

    表結構。
    1:文章表

    CREATE ? TABLE ?`t_article`?(
    ??`a_id`?
    int ( 11 )? NOT ? NULL ?auto_increment,
    ??`a_sort`?
    int ( 11 )? NOT ? NULL ? default ? ' 0 ' ,
    ??`a_title`?
    varchar ( 50 )? default ? NULL ,
    ??`a_body`?
    text ,
    ??`a_author`?
    varchar ( 11 )? default ? '' ,
    ??`a_hit`?
    int ( 11 )? NOT ? NULL ? default ? ' 0 ' ,
    ??`c_id`?
    int ( 11 )? default ? ' 0 ' ,
    ??`a_date`?
    varchar ( 20 )? default ? NULL ,
    ??
    PRIMARY ? KEY ??(`a_id`)
    )?

    2:評論表

    CREATE ? TABLE ?`t_remark`?(
    ??`r_id`?
    int ( 11 )? NOT ? NULL ?auto_increment,
    ??`a_id`?
    int ( 11 )? NOT ? NULL ? default ? ' 0 ' ,
    ??`r_name`?
    varchar ( 20 )? NOT ? NULL ? default ? '' ,
    ??`r_title`?
    varchar ( 50 )? default ? '' ,
    ??`r_body`?
    varchar ( 100 )? default ? NULL ,
    ??`r_email`?
    varchar ( 30 )? default ? NULL ,
    ??`r_date`?
    varchar ( 30 )? default ? NULL ,
    ??
    PRIMARY ? KEY ??(`r_id`),
    ??
    KEY ?`a_id`?(`a_id`)
    )

    表結構我直接導出來的。。
    表建好了。接下來寫vo 類了..

    這是文章表的VO

    package ?wjjcms.vo;
    import ?java.util. * ;

    public ? class ?articleVO? {
    ????
    private ? int ?a_id;
    ????
    private ? int ?a_sort;
    ????
    private ? int ?a_hit;
    ????
    private ? int ?c_id;
    ????
    private ?String?a_title;
    ????
    private ?String?a_body;
    ????
    private ?String?a_author;
    ????
    private ?String?a_date;
    ????
    private ?Set?a_remark;
    ????
    ????
    public ?articleVO()? {
    ????}


    ???
    // 自己寫上get?set?方法。。我就不貼上來了

    評論表的。

    package ?wjjcms.vo;

    public ? class ?remarkVO? {
    ????
    private ? int ?a_id;
    ????
    private ? int ?r_id;
    ????
    private ?String?r_name;
    ????
    private ?String?r_title;
    ????
    private ?String?r_body;
    ????
    private ?String?r_email;
    ????
    private ?String?r_date;
    ????
    public ?remarkVO()? {
    ????}
    ?????? //get set 方法自己加上。。

    接下來 寫映射文件了..
    我用的是hibernate.properties 文件連接數據庫。


    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

    該文件記的放在classes目錄下面。。

    <? xml?version="1.0"?encoding="UTF-8" ?>

    <! DOCTYPE?hibernate-mapping?PUBLIC
    ????"-//Hibernate/Hibernate?Mapping?DTD?2.0//EN"
    ????"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd"
    >
    < hibernate-mapping >

    ????
    < class? name ="wjjcms.vo.articleVO" ?table ="t_article" ? >


    ????
    < id? name ="a_id" ?column ="a_id" ?unsaved-value ="0" ? >
    ??????
    < generator? class ="native" />
    ?
    </ id >
    ?????
    < property? name ="c_id" ????column ="c_id" />
    ?????
    < property? name ="a_title" ?column ="a_title" />
    ?????
    < property? name ="a_sort" ??column ="a_sort" />
    ?????
    < property? name ="a_date" ??column ="a_date" />
    ?????
    < property? name ="a_body" ??column ="a_body" />
    ?????
    < property? name ="a_hit" ???column ="a_hit" />
    ?????
    < property? name ="a_author" ?column ="a_author" />
    ????????
    < set? name ="a_remark" ?cascade ="all" ?outer-join ="true" >
    ????????????
    < key? column ="a_id" />
    ????????????
    < one-to-many? class ="wjjcms.vo.remarkVO" ? />
    ????????
    </ set >
    ????
    ??
    </ class >

    </ hibernate-mapping >

    配置文件 那些字段 屬性是什么意思。。你到首頁搜索一下,很多的 。

    <? 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 ="wjjcms.vo.remarkVO" ?table ="t_remark" ? >

    ????
    < id? name ="r_id" ?column ="r_id" ?unsaved-value ="0" ? >
    ??????
    < generator? class ="native" /> ?
    ???
    </ id >

    ??????
    < property? name ="r_name" ?column ="r_name" ? />
    ????
    < property? name ="r_email" ?column ="r_email" ? />
    ????
    < property? name ="r_title" ?column ="r_title" ? />
    ????
    < property? name ="r_body" ?column ="r_body" ? />
    ????
    < property? name ="r_date" ?column ="r_date" ? />
    ?????
    < property? name ="a_id" ?column ="a_id" ? />
    ????
    </ class >

    </ hibernate-mapping >


    其實這些都是可以自動生成的。。你去看看http://blog.csdn.net/javamxj/category/111072.aspx
    他講的很詳細。。

    一切都準備好了。。該寫個類來小試一下了。。

    package ?wjjcms.test;

    import ?junit.framework. * ;
    import ?net.sf.hibernate.cfg. * ;
    import ?net.sf.hibernate. * ;
    import ?wjjcms.vo.remarkVO;
    import ?wjjcms.vo.articleVO;
    import ?java.sql.SQLException;
    import ?java.util. * ;

    public ? class ?TestText? extends ?TestCase? {

    ????
    private ?SessionFactory?sessionFactory;
    ????
    private ?Session?ss? = ? null ;
    ????
    public ?TestText(String?name)? {
    ????????
    super (name);
    ????}


    ????
    /*
    ?????junit中setUp方法在TestCase初試化的時候會自動調用
    ?????一般用來初試化公共資源。。
    ?????這里用來初試化Hibernate?Session
    ????
    */

    ????
    protected ? void ?setUp()? throws ?Exception? {
    ????????Configuration?config?
    = ? new ?Configuration();
    ????????config.addClass(articleVO.
    class ).addClass(remarkVO. class );
    ????????sessionFactory?
    = ?config.buildSessionFactory();
    ????????ss?
    = ?sessionFactory.openSession();
    ????}

    ????
    /*
    ????*?這個方法junit?TestCase執行完畢時,會自動調用tearDown方法。
    ????*?一般用于釋放資源,我這里是關閉在setUp()方法里打開的Session
    ????
    */

    ????
    protected ? void ?tearDown()? throws ?Exception? {
    ????????
    try ? {
    ????????????ss.close();
    ????????}
    ? catch ?(HibernateException?ex)? {
    ????????????ex.printStackTrace();
    ????????}

    ????}

    ????
    // 測試添加一篇文章
    ???? public ? void ?testAddArticle()? throws ?Exception? {
    ????????
    try ? {
    ????????????wjjcms.vo.articleVO?vo?
    = ? new ?articleVO();
    ????????????vo.setA_author(
    " wujunjun " );
    ????????????vo.setA_body(
    " 熱愛祖國,堅決抗日! " );
    ????????????vo.setA_date(
    " 2006-3-30 " );
    ????????????vo.setA_hit(
    33 );
    ????????????vo.setA_sort(
    1 );
    ????????????vo.setA_title(
    " 小日本鬼子 " );
    ????????????vo.setC_id(
    1 );
    ????????????ss.save(vo);
    ????????????ss.flush();
    ????????????ss.connection().commit();
    ????????????ss.close();
    ????????}
    ? catch ?(HibernateException?ex)? {
    ????????????
    // junit.framework.Assert.
    ????????????System.out.print(ex.getMessage());
    ????????}

    ????}


    ????
    // 測試添加一篇評論
    ???? public ? void ?testAddRemark()? throws ?Exception? {
    ????????
    try ? {
    ????????????wjjcms.vo.remarkVO?vo?
    = ? new ?remarkVO();
    ????????????vo.setR_body(
    " 有是你個小日本。。。。 " );
    ????????????vo.setR_date(
    " 2006-1-1 " );
    ????????????vo.setA_id(
    1 );
    ????????????vo.setR_email(
    " wujun1866@gmail.com " );
    ????????????vo.setR_name(
    " wujunjun " );
    ????????????vo.setR_title(
    " re:小日本,打的好 " );
    ????????????ss.save(vo);
    ????????????ss.flush();
    ????????????ss.connection().commit();
    ??????????
    ????????}
    ? catch ?(HibernateException?ex)? {
    ????????????System.out.print(ex.getMessage());
    ????????}

    ????}

    ????
    // 測試同時添加一騙文章和5篇評論
    ???? public ? void ?testAddAll()
    ????
    {
    ????????wjjcms.vo.articleVO?vo?
    = ? new ?articleVO();
    ????????vo.setA_author(
    " wujunjun " );
    ????????vo.setA_body(
    " 熱愛祖國,堅決抗日! " );
    ????????vo.setA_date(
    " 2006-3-30 " );
    ????????vo.setA_hit(
    33 );
    ????????vo.setA_sort(
    1 );
    ????????vo.setA_title(
    " 小日本鬼子 " );
    ????????vo.setC_id(
    1 );

    ?????????????Set?remarkSet
    = new ?HashSet();
    ?????????????
    for ( int ?i = 0 ;i < 5 ;i ++ )
    ?????????????
    {
    ?????????????????wjjcms.vo.remarkVO?reVO?
    = ? new ?remarkVO();
    ?????????????????reVO.setR_body(
    " 有是你個小日本。。。。 " );
    ?????????????????reVO.setR_date(
    " 2006-1-1 " );
    ?????????????????reVO.setA_id(
    1 );
    ?????????????????reVO.setR_email(
    " wujun1866@gmail.com " );
    ?????????????????reVO.setR_name(
    " wujunjun " );
    ?????????????????reVO.setR_title(
    " re:小日本,打的好 " );
    ?????????????????remarkSet.add(reVO);
    ?????????????}

    ???????vo.setA_remark(remarkSet);
    ???????
    try
    ???????
    {
    ???????????ss.save(vo);
    ???????????ss.flush();
    ???????????ss.connection().commit();
    ???????}

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

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

    ????
    // 測試顯示文章。。和評論。。
    ???? public ? void ?testShowArticle()? throws ?SQLException,?HibernateException? {
    ????????Query?q?
    = ?ss.createQuery( " from?articleVO?where?c_id=? " );
    ????????q.setInteger(
    0 ,? 1 );
    ????????List?l?
    = ?q.list();
    ????????
    for ?( int ?i? = ? 0 ;?i? < ?l.size();?i ++ )? {
    ????????????articleVO?showVO?
    = ?(articleVO)?l.get(i);
    ????????????System.out.print(showVO.getA_author());
    ????????????System.out.print(showVO.getA_title());
    ????????????java.util.Iterator?it?
    = ?showVO.getA_remark().iterator();
    ????????????
    while ?(it.hasNext())? {
    ????????????????remarkVO?reVO?
    = ?(remarkVO)?it.next();
    ????????????????System.out.print(reVO.getR_email());
    ????????????????System.out.print(reVO.getR_title());
    ????????????}

    ????????}

    ????}

    }



    運行一下看看。

    ?
    OK,,成功了。數據也已經進數據庫了。。

    哈。。我是菜鳥。專家多指點啊。。

    posted on 2006-03-30 22:41 record java and net 閱讀(842) 評論(0)  編輯  收藏 所屬分類: java

    導航

    常用鏈接

    留言簿(44)

    新聞檔案

    2.動態語言

    3.工具箱

    9.文檔教程

    友情鏈接

    搜索

    最新評論

    主站蜘蛛池模板: 最近中文字幕mv免费高清视频8 | 美女隐私免费视频看| 免费精品国产自产拍观看| 男人免费视频一区二区在线观看| 中文字幕亚洲日韩无线码| 精品视频在线免费观看| 亚洲一级毛片免费看| 又黄又爽一线毛片免费观看| 国产无遮挡无码视频免费软件| 亚洲午夜电影一区二区三区| 亚洲第一成人影院| 7m凹凸精品分类大全免费| 亚洲av无码一区二区三区四区 | 精品国产免费观看久久久| 91国内免费在线视频| 67194在线午夜亚洲| 最新亚洲成av人免费看| 中国在线观看免费高清完整版| 五级黄18以上免费看| 亚洲美女视频一区| 亚洲一区二区精品视频| 18女人水真多免费高清毛片| 免费无码国产在线观国内自拍中文字幕| 亚洲三级电影网址| 亚洲欧洲精品成人久久曰影片 | 亚洲AV无码专区亚洲AV伊甸园| 日本免费电影一区| 久久99国产乱子伦精品免费 | 国国内清清草原免费视频99| a级毛片视频免费观看| 亚洲国产精品日韩av不卡在线| 久久精品国产亚洲av高清漫画| 亚洲区小说区图片区| 美女被免费视频网站a国产| 84pao国产成视频免费播放| 久久九九免费高清视频| 亚洲AV无码国产剧情| 亚洲最大在线观看| 亚洲av片劲爆在线观看| 亚洲午夜福利精品无码| 国产免费啪嗒啪嗒视频看看|