hibernate为什么初始化失败???
我用eclipse建一个web项目来做hibernate练习。
下面这个servlet测试类运行没问题。
- Java code
package zgl.test.client;import java.io.IOException;import java.util.Date;//import java.sql.Date;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import zgl.test.bean.Cat;/** * Servlet implementation class HelloWorld */public class HelloWorld extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public HelloWorld() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse * response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request, response); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse * response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=UTF-8"); java.io.PrintWriter out = response.getWriter(); out.println("--------------你好吗?--------------"); Cat cat=new Cat(); cat.setBirth(new Date()); cat.setCatname("迷人的天空"); zgl.test.util.HibernateUtil.addCat(cat); if (out != null) out.close(); }}
但是我写的一个普通类不能运行,高手给说一下吧。
- Java code
package zgl.test.client;import java.util.Date;import javax.servlet.ServletException;import zgl.test.bean.Cat;public class Test { /** * @param args * @throws ServletException */ public static void main(String[] args) throws ServletException { //为什么会初始化失败呢,为什么获取配置为空指针呢? Cat cat=new Cat(); cat.setBirth(new Date()); cat.setCatname("迷人的天空"); zgl.test.util.HibernateUtil.addCat(cat); }}
异常如下:
Exception in thread "main" java.lang.ExceptionInInitializerError
at org.hibernate.cfg.Configuration.reset(Configuration.java:201)
at org.hibernate.cfg.Configuration.<init>(Configuration.java:220)
at org.hibernate.cfg.Configuration.<init>(Configuration.java:224)
at zgl.test.util.HibernateUtil.<clinit>(HibernateUtil.java:15)
at zgl.test.client.Test.main(Test.java:19)
Caused by: java.lang.NullPointerException
at org.hibernate.util.ConfigHelper.getResourceAsStream(ConfigHelper.java:167)
at org.hibernate.cfg.Environment.<clinit>(Environment.java:558)
... 5 more
[解决办法]
[解决办法]
是不是初始化文件中有在TOMCAT启动时自动加载的 ??或者必须通过web程序才能正常初始化?
例如proxool中配置数据库,就必须通过web程序运行。
[解决办法]
是不是hinernate中的配置文件有问题啊!没找到映射文件