1

我有一个简单的应用程序可以从 oracle DB 中检索数据:

public class userExportImpl extends HibernateDaoSupport {

    public User getUser (final String username) throws OssObjectNotFoundException, OssIllegalArgumentException
    {
       Session session = null;
       SessionFactory sessionFactory = new
            Configuration().configure().buildSessionFactory();
       session = sessionFactory.openSession();
       User user= null;
       createHibernateTemplate(sessionFactory);
       user = (User)getHibernateTemplate().execute( new HibernateCallback()
       {
          public Object doInHibernate(Session session) throws HibernateException, SQLException
          {
            User user = (User) session.get(User.class, username);

            return user;
          }
       });
    session.close();
    return user;
}}

这是我调用getUser的main():

public class userExportTool {
public static void main(String args[]){
   userExportImpl userExport = new userExportImpl();
   User user = userExport.getUser("test");
}}

这就是我尝试运行它时遇到的异常:

Exception in thread "main" java.lang.NullPointerException
    at com.tektronix.userExport.userExportImpl.getUser(userExportImpl.java:40)
    at com.tektronix.userExport.uacnExportTool.main(uacnExportTool.java:17)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)

起初我认为 getHibernateTemplate 需要一个会话工厂并打开会话,所以我添加了这些,但我仍然收到一个 nullPointerException 并且我不知道为什么会发生这种情况。hibernate.cfg.xml 和 User.hbm.xml 都存在,我已经尝试通过 session.get() 等更简单的方法使用 db,所以连接必须没问题。你能给我一些想法吗?

4

0 回答 0