java.lang.NullPointerException Hibernate与Ehc​​ache一起使用

我将Hibernate 4.1.2与Ehcache 2.4.3一起使用(在下载hibernate时与hibernate一起发布)。

我的hibernate.cfg.xml:

     com.microsoft.sqlserver.jdbc.SQLServerDriver   jdbc:sqlserver://localhost:1433;databaseName=Stock_indices xxx xxx true org.hibernate.dialect.SQLServerDialect update 5 20 300 50 30 true true org.hibernate.cache.ehcache.SingletonEhCacheRegionFactory  ehcache-entity.xml    

ehcache-entity.xml:

     

mapping.xml

                            

但是,我得到了这个例外:

  java.lang.NullPointerException at org.hibernate.cache.ehcache.internal.util.HibernateUtil.loadAndCorrectConfiguration(HibernateUtil.java:64) at org.hibernate.cache.ehcache.SingletonEhCacheRegionFactory.start(SingletonEhCacheRegionFactory.java:91) at org.hibernate.internal.SessionFactoryImpl.(SessionFactoryImpl.java:281) at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1741) ............ 

看来hibernate无法加载配置? 我该如何解决这个问题? (serlvet在没有使用Ehcache的情况下工作正常)。

我的servlet类:

 package stockdataservlet; import java.io.IOException; import java.math.BigDecimal; import java.util.Iterator; import java.util.List; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.hibernate.Query; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.hibernate.cfg.Configuration; import org.hibernate.service.ServiceRegistry; import org.hibernate.service.ServiceRegistryBuilder; import com.google.gson.Gson; import com.microsoft.sqlserver.jdbc.*; public class DataServlet extends HttpServlet { private static final long serialVersionUID = 1L; public static SessionFactory sessionfactory = null; public void init() { try { Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); Configuration conf = new Configuration(); conf.configure(); ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings(conf.getProperties()).buildServiceRegistry(); sessionfactory = conf.buildSessionFactory(serviceRegistry); } catch(Exception e){ e.printStackTrace(); } } protected void doGet(HttpServletRequest request, HttpServletResponse response) { Session session = null; try{ session = sessionfactory.openSession(); Transaction transaction = session.beginTransaction(); Query query = session.createSQLQuery("SELECT * FROM STOCKINDEX WHERE TICKER = :index ").addScalar("CLOSEPRICE"); query.setParameter("index", "AAA"); List list = query.list(); transaction.commit(); String json = new Gson().toJson(list); /*response.setContentType("application/json"); response.setCharacterEncoding("UTF-8"); response.getWriter().write(json);*/ } catch(Exception e){ e.printStackTrace(); } finally{ session.close(); } } 

} 在此处输入图像描述

没关系。 如果资源名为ehcache.xml并放在类路径中,则不必为属性“net.sf.ehcache.configurationResourceName”显式指定值。 如果有多个缓存管理器,则可以选择此选项。

或者您可以将所有资源文件从lib移动到WEB-INF / resources之类的目录,以便在类路径下更容易引用它。

Ehcache在类路径的顶层查找名为ehcache.xml的文件。 没有在类路径中查找ehcache-failsafe.xml。 ehcache-failsafe.xml包装在Ehcache jar中,应该始终可以找到。

最后我发现了我的代码不起作用的原因。 感谢Vinodn的建议。

 config.getDefaultCacheConfiguration().isTerracottaClustered() 

实际上,我没有在我的ehcache-entity.xml中配置defaultCache标签

 config.getDefaultCacheConfiguration() 

返回null。 因此添加defaultCache解决了这个问题。

ehcache-entity.xml放在要由hibernate配置加载的类路径中。

在ehcache.xml中添加defaultCache配置对我也有帮助。