宝慕林1479674
2017-10-21 17:24
import java.util.Date;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
public class StudentsTest {
private SessionFactory sessionFactory;
private Session session;
private Transaction transaction;
@Before
public void init(){
//创建配置对象
Configuration config = new Configuration().configure();
//创建服务注册对象
ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().
applySettings(config.getProperties()).buildServiceRegistry();
//创建会话工厂对象
sessionFactory = config.buildSessionFactory(serviceRegistry);
//开启事务
transaction =session.beginTransaction();
}
@After
public void destory(){
transaction.commit();//提交事务
session.close();//关闭回话
sessionFactory.close();//关闭会话工厂
}
@Test
public void testSaveStudents(){
//生成学生对象
Students s =new Students(1,"张三丰","男",new Date(),"武当山");
session.save(s);
}
}
你创建的session对象呢?
我也犯了这个错误 正纳闷呢 没想到有人犯了和我一样的错误。。。。
Hibernate初探之单表映射
74808 学习 · 793 问题
相似问题