频道直达 - 专题 - 新闻 - 技巧 - 组网 - 开发 - 安全 - web编程 - 图像 - 操作系统 - 数据库 - 教育 - 旅游 - 健康 - 时尚 - 驱动 - 软件 - 游戏 - 多媒体 - ERP - 讨论组

在MyEclipse快速搭建Hibernate应用

来源:javaresearch 作者: 出处:巧巧读书 2007-03-18 进入讨论组
上一页 1 2 

    (8)在工程的mappings(在第4步创建的目录)目录下创建一个名为User.hbm.xml,内容如下:
         <?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 >
            <class name="cn.com.test.hibernatesample.model.User" table="USER">

              <id name="id" column="id" type="integer" >
                <generator class="increment"/>
          </id>
          <property name="name" column="name" type="string" />
              <property name="sex" column="sex" type="char"/>
              <property name="age" column="age" type="byte"/>
           </class>
        </hibernate-mapping>

    (9)在HibernateSample项目的右击选项菜单中,选择New -> JUnit Test Case...,弹出新建JUnit 

Test Case的对话框:
        <9.1>package上输入:cn.com.test.hibernatesample.test;
        <9.2>type上输入UserTest;
        <9.3>在which method stubs whould you like to create中勾上setUp(),tearDown();
        整个UserTest类的代码如下:

    
        ---------------------------------------------------------------------------------
    package cn.com.test.hibernatesample.test;

    import org.hibernate.HibernateException;
    import org.hibernate.Session;
    import org.hibernate.Transaction;

    import cn.com.test.hibernatesample.factory.HibernateSessionFactory;
    import cn.com.test.hibernatesample.model.User;

    import junit.framework.Assert;
    import junit.framework.TestCase;

    public class UserTest extends TestCase {

        //hibenate的session对象
        private Session session = null;
    
        protected void setUp() throws Exception {
            super.setUp();
            session = HibernateSessionFactory.currentSession();
        }
    
        public void testUserInsert() {
            Transaction tran = null;
            try {
                tran = session.beginTransaction();
                User user = new User();
                user.setName("suhaoyuan");
                user.setSex('M');
                user.setAge(Byte.parseByte("27"));
                session.save(user);
                tran.commit();
                Assert.assertEquals(.getId().intValue() > 0, true);
            }
            catch(HibernateException ex) {
                ex.printStackTrace();
                Assert.fail(ex.getMessage());
                if(tran != null) {
                    try {
                      tran.rollback();
                    }
                    catch(HibernateException tranEx) {
                        tranEx.printStackTrace();
                    }
                }
            }
        }

        protected void tearDown() throws Exception {
            super.tearDown();
            HibernateSessionFactory.closeSession();
        }

    }
        ----------------------------------------------------------------------------

        (10)运行刚创建的UserTest测试用例即完成该应用;

                                                                         suhaoyuan
                                                                         2007.03.15

来自:http://www.qqread.com/java/2007/03/e302406.html 更多文章 更多内容请看Hibernate原理与配置Hibernate应用Hibernate相关文章专题,或进入讨论组讨论。
上一页 1 2 
收藏此文】【 】【打印】【关闭
相关图文阅读
频道图文推荐
健 康 咨 询
时 尚 咨 询
巧巧读书宗旨
相关专题
讨论组问题推荐
站内各频道最新更新文档
站内最新制作专题
热门关键字导读
Photoshop教 程照片处理 照片制作 PS快捷键 抠图
计 算 机 故 障XP系统修复
艺 术 与 设 计设计 流媒体 设计欣赏 边框
计 算 机 安 全ARP
站内频道文章精选
巧巧电脑频道编辑信箱  告诉我们您想看的专题或文章