hibernate单元测试的空指针

 

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"/>

posted on 2012-04-15 15:09  lovebeauty  阅读(1043)  评论(0编辑  收藏  举报

导航