cn.fql.template.hibernate.HibernateTest.java Source code

Java tutorial

Introduction

Here is the source code for cn.fql.template.hibernate.HibernateTest.java

Source

/*
 * File: $RCSfile$
 *
 * Copyright (c) 2005 Wincor Nixdorf International GmbH,
 * Heinz-Nixdorf-Ring 1, 33106 Paderborn, Germany
 * All Rights Reserved.
 *
 * This software is the confidential and proprietary information
 * of Wincor Nixdorf ("Confidential Information"). You shall not
 * disclose such Confidential Information and shall use it only in
 * accordance with the terms of the license agreement you entered
 * into with Wincor Nixdorf.
 */
package cn.fql.template.hibernate;

import junit.framework.TestCase;
import junit.framework.Assert;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.HibernateException;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

import java.util.List;

import cn.fql.template.hibernate.Tuser;

/**
 * The class <code>cn.fql.template.hibernate.HibernateTest</code>
 *
 * @author User, WN ASP SSD
 * @version $Revision$
 */
public class HibernateTest extends TestCase {

    Session session = null;

    protected void setUp() throws Exception {
        Configuration config = new Configuration().configure();
        SessionFactory sessionFactory = config.buildSessionFactory();
        session = sessionFactory.openSession();
    }

    protected void tearDown() throws Exception {
        try {
            session.close();
        } catch (HibernateException e) {
            e.printStackTrace();
        }
    }

    public void testInsert() {
        Transaction tran = null;
        try {
            tran = session.beginTransaction();
            Tuser user = new Tuser();
            user.setName("Mary");
            session.save(user);
            tran.commit();
            Assert.assertEquals(user.getId().intValue() > 0, true);
        } catch (HibernateException e) {
            e.printStackTrace();
            Assert.fail(e.getMessage());
            if (tran != null) {
                try {
                    tran.rollback();
                } catch (HibernateException e1) {
                    e1.printStackTrace();
                }
            }
        }
    }

    public void testSelect() {
        String hql = " from Tuser where name='Terry'";
        try {
            List userList = session.createQuery(hql).list();
            Tuser user = (Tuser) userList.get(0);
            Assert.assertEquals(user.getName(), "Terry");
        } catch (HibernateException e) {
            e.printStackTrace();
            Assert.fail(e.getMessage());
        }
    }
}
/**
 * History:
 *
 * $Log$
 */