Hibernate 使用 Annotation 1(测试)
Hello World 程序
?
hibernate.cfg.xml配置文件:
?
?
?
LoginTest.java 代码:
package com.jlee01.helloworld;import junit.framework.TestCase;import org.hibernate.Session;import org.hibernate.SessionFactory;import org.hibernate.cfg.AnnotationConfiguration;import org.hibernate.tool.hbm2ddl.SchemaExport;import org.junit.AfterClass;import org.junit.BeforeClass;public class LoginTest extends TestCase {@BeforeClasspublic void begin(){}@AfterClasspublic void end(){}public void testLogin(){AnnotationConfiguration acfg = new AnnotationConfiguration() ;SessionFactory sf = acfg.configure().buildSessionFactory() ;Session session = sf.getCurrentSession() ;session.beginTransaction() ;Login login = new Login() ;login.setUsername("JLee") ;login.setPassword("198791") ;login.setAddress("北京") ;login.setEmail("444823046@qq.com") ;session.save(login) ;session.getTransaction().commit() ;}public void testExport(){new SchemaExport(new AnnotationConfiguration().configure()).create(false, true) ;}}?