现在的位置: 首页 > 综合 > 正文

hibernate单元测试的空指针

2012年11月29日 ⁄ 综合 ⁄ 共 830字 ⁄ 字号 评论关闭

 

View Code

 1 public class HibernateCoreAPITest {
 2 private static SessionFactory sessionFactory;
 3     
 4     @BeforeClass
 5     public static void beforeClass() {
 6             sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory();
 7     }
 8     @AfterClass
 9     public static void afterClass() {
10         sessionFactory.close();
11     }
12     
13     @Test
14     public void testGetStudent() {
15         Session session = sessionFactory.getCurrentSession();
16         session.beginTransaction();
17         Student s = (Student)session.get(Student.class, 1);
18         s.setName("xml");
19         session.getTransaction().commit();
20     }
21 }

此时总是行不通,在main里面

public static void main(String[] args) {
		beforeClass();
		afterClass();
	}

 这才查出来

之前这样写的

<mapping class="com/test/hibernate/model/Student.hbm.xml" />
<mapping class="com.test.hibernate.model.Teacher" />

正确写法

xml :<mapping resource="com/test/hibernate/model/Student.hbm.xml"/>
注解:<mapping class="com.test.hibernate.model.Teacher"/>

抱歉!评论已关闭.