Hibernate配置 junit测试 import static org.junit.Assert.*; public class HibernateTest{ SessionFactory factory=null; Session session=null; //测试之前初始化数据 @Before public void setUp() throws Eception{ System.out.println(初始化); Configur
Hibernate配置
junit测试
import static org.junit.Assert.*;
public class HibernateTest{
SessionFactory factory=null;
Session session=null;
//测试之前初始化数据
@Before
public void setUp() throws Eception{
System.out.println("初始化");
Configuration config=new Configuration
().configure();
ServiceRegistry sr=new
ServiceRegistryBuilder.applySetting(config.getProperties());
factory=config.buildSessionFactory(sr);
session=factory.openSession();
}
@After
//测试之后释放数据
public void tearDown() throws Exception{
System.out.println("结束,释放资源");
if(session.isOpen()){
session.close();
}
}
}
@Test测试方法
public void tearDown() throws Exception{
System.out.println("初始化");
} @Test
public void testAdd(){
Transaction tx=session.beginTransaction();
Person p=new Person("admin",123456,new Date
());
session.persist(p);//新增需要事务
tx.commit();
}
}
//Hibernate深入配置
access
//field(可以不需要getter/setter方法操作对象属性)
(尽量不要这样设置)
Person p=(Person)session.get(Person.class,id);//根据反射创建
对象
System.out.println(p);
Statement:The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn