package com.wangyi.hibernate.demo.test;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import com.wangyi.hibernate.demo.User;
import junit.framework.TestCase;
/**
* @author WangYi
*
*/
public class TestHibernate extends TestCase {
Session session = null;
/**
* @param name
*/
public TestHibernate(String name) {
super(name);
}
/*
* (非 Javadoc)
*
* @see junit.framework.TestCase#setUp()
*/
protected void setUp() throws Exception {
super.setUp();
Configuration config = new Configuration().configure();
SessionFactory factory = config.buildSessionFactory();
session = factory.openSession();
}
/*
* (非 Javadoc)
*
* @see junit.framework.TestCase#tearDown()
*/
protected void tearDown() throws Exception {
if (session != null) {
session.close();
}
super.tearDown();
}
public void testDemo() {
int n = 10;
for (int i = 0; i < n; i++) {
User user = new User();
user.setUsername("username_" + i);
user.setPassword("password_" + i);
user.setEmail("user_" + i + "@yahoo.com");
session.save(user);
session.beginTransaction().commit();
}
}
}
评论13
最新资源