import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.junit.Test;
import com.opensymphony.xwork2.interceptor.annotations.After;
import com.opensymphony.xwork2.interceptor.annotations.Before;
public class HibernateUtil {
private SessionFactory sessionFactory;
private Session session;
private Transaction transaction;
@Before
public void init() {
// 创建配置对象
Configuration config = new Configuration().configure();
// 创建回话工厂对象
sessionFactory = config.buildSessionFactory();
// 会话对象
session = sessionFactory.openSession();
// 开启事务
transaction = session.beginTransaction();
}
@After
public void destory() {
transaction.commit();// 提交事务
session.close();// 关闭会话
sessionFactory.close();// 关闭会话工厂
}
@Test
public void testSaveUser(){
User u=(User)session.get(User.class, 100000);
System.out.println(u.getUsername());
}
}
大佬们,求解为什么会出现java.lang.NullPointerException,提示是User u=(User)session.get(User.class, 100000);这个地方出错,但是我排查不出来。。
相关分类