这是什么问题啊,弄了我几天了,我老是得不到 session

时间:2021-03-29 15:01:31
为何我老是得不到 session
这是错误日志:

...

15:16:37,845  WARN Configurator:126 - No configuration found. Configuring ehcache from ehcache-failsafe.xml found in the classpath: jar:file:/E:/My%20Web/MSInfoSys/WEB-INF/lib/ehcache-1.1.jar!/ehcache-failsafe.xml
%%%% Error Creating SessionFactory %%%%

...

------------------------------------------------------------------
这是通过 Eclipse 自动生成的 StartHibernateSession.java

package unit;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.cfg.Configuration;

public class StartHibernateSession {

    private static String CONFIG_FILE_LOCATION = "/hibernate.cfg.xml";
    private static final ThreadLocal<Session> threadLocal = new ThreadLocal<Session>();
    private  static Configuration configuration = new Configuration();
    private static org.hibernate.SessionFactory sessionFactory;
    private static String configFile = CONFIG_FILE_LOCATION;

    private StartHibernateSession() {
    }

    public static Session getSession() throws HibernateException {
        Session session = (Session) threadLocal.get();
            if (session == null || !session.isOpen()) {
                if (sessionFactory == null) {
                    rebuildSessionFactory();
       }
       session = (sessionFactory != null) ? sessionFactory.openSession()
: null;
       threadLocal.set(session);
   }
        return session;
    }

    public static void rebuildSessionFactory() {
        try {
            configuration.configure(configFile);
   sessionFactory = configuration.buildSessionFactory();
        } catch (Exception e) {
            System.err.println("%%%% Error Creating SessionFactory %%%%");
   e.printStackTrace();
        }
    }

    public static void closeSession() throws HibernateException {
        Session session = (Session) threadLocal.get();
        threadLocal.set(null);

        if (session != null) {
            session.close();
        }
    }

    public static org.hibernate.SessionFactory getSessionFactory() {
        return sessionFactory;
    }

    public static void setConfigFile(String configFile) {
        StartHibernateSession.configFile = configFile;
        sessionFactory = null;
    }

    public static Configuration getConfiguration() {
        return configuration;
    }

}

19 个解决方案

#1


该回复被版主删除

#2


ehcache-failsafe.xml 是干什么的? 提示这个配置文件找不到

#3


lib里是不是没有这个包
ehcache-1.1.jar

#4


友情UP

#5


回复 dreamover

lib 里有这个包的

#6


我好像刚开始学hibernate时也是这样的问题,
你最好在发布的目录中建一个log4j.properties文件.
//log4j.properties内容如下----------
log4j.rootLogger=info,CONSOLE
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender 
log4j.appender.CONSOLE.Target=System.out 
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout 
log4j.appender.CONSOLE.layout.ConversionPattern= %4p [%t] (%F:%L) - %m%n
//--------------
当然lib中要有log4j-1.2.11.jar的
然后再运行看看,这样能看到明确的错误是什么.

#7


把hibernate里的ehcache.xml拷贝到src目录下,hibernate3用到了ehcache

#8


我曾经也出现过这个问题,把它放在应用服务器的容器中运行,把相应的jar包放进去就可以了,缺少容器是不能运行的

#9


用最新的MYECLIPS试试

#10


回复:ldg_2(绿豆糕)
按你的方法,我把那个XML文件放到了 classes 目录下,产生新的错误日志:
---------------------------------
09:24:01,833  WARN Configurator:126 - No configuration found. Configuring ehcache from ehcache-failsafe.xml found in the classpath: file:/E:/My%20Web/MSInfoSys/WEB-INF/classes/ehcache-failsafe.xml
---------------------------------
这是怎么回事,也就是这里,老是警告 configuration 找不到

#11


回复:jyh_jack() 
我的发布包中有 log4j.properties 这个包了

#12


回复:eddie404956() 
我现用的就是最新的 MyEclipse 5.0

#13


No configuration found. Configuring ehcache from ehcache-failsafe.xml found in the classpath: jar:file:/D:/workspace/HibernateText/lib/ehcache-1.1.jar!/ehcache-failsafe.xml

我这里也是这么显示的,但是运行很正常,你再看看下面有没有什么错误提示,把提示的内容全贴出来.

#14


这是应用了 struts 结构提示信息,里面还是有那个错误
-----------------------------------------------------
log4j:WARN No appenders could be found for logger (org.apache.catalina.session.ManagerBase).
log4j:WARN Please initialize the log4j system properly.
16:29:01,352  INFO Environment:464 - Hibernate 3.0.2
16:29:01,362  INFO Environment:477 - hibernate.properties not found
16:29:01,372  INFO Environment:510 - using CGLIB reflection optimizer
16:29:01,372  INFO Environment:540 - using JDK 1.4 java.sql.Timestamp handling
16:29:01,862  INFO Configuration:1160 - configuring from resource: /hibernate.cfg.xml
16:29:01,862  INFO Configuration:1131 - Configuration resource: /hibernate.cfg.xml
16:29:02,053  INFO Configuration:441 - Mapping resource: com/yourcompany/ManagerList.hbm.xml
16:29:02,263  INFO HbmBinder:258 - Mapping class: com.yourcompany.ManagerList -> ManagerList
16:29:02,313  INFO Configuration:1272 - Configured SessionFactory: null
16:29:02,313  INFO Configuration:852 - processing extends queue
16:29:02,313  INFO Configuration:856 - processing collection mappings
16:29:02,313  INFO Configuration:865 - processing association property references
16:29:02,313  INFO Configuration:894 - processing foreign key constraints
16:29:02,563  INFO DriverManagerConnectionProvider:41 - Using Hibernate built-in connection pool (not for production use!)
16:29:02,563  INFO DriverManagerConnectionProvider:42 - Hibernate connection pool size: 20
16:29:02,563  INFO DriverManagerConnectionProvider:45 - autocommit mode: false
16:29:02,573  INFO DriverManagerConnectionProvider:80 - using driver: com.microsoft.jdbc.sqlserver.SQLServerDriver at URL: jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=MsNews;SelectMethod=cursor
16:29:02,573  INFO DriverManagerConnectionProvider:86 - connection properties: {user=sa, password=****}
16:29:02,814  INFO SettingsFactory:71 - RDBMS: Microsoft SQL Server, version: Microsoft SQL Server  2000 - 8.00.2039 (Intel X86) 
May  3 2005 23:18:38 
Copyright (c) 1988-2003 Microsoft Corporation
Developer Edition on Windows NT 5.1 (Build 2600: Service Pack 2)

16:29:02,814  INFO SettingsFactory:72 - JDBC driver: SQLServer, version: 2.2.0029
16:29:02,884  INFO Dialect:92 - Using dialect: org.hibernate.dialect.SQLServerDialect
16:29:02,894  INFO SettingsFactory:130 - Scrollable result sets: enabled
16:29:02,894  INFO SettingsFactory:138 - JDBC3 getGeneratedKeys(): disabled
16:29:02,894  INFO SettingsFactory:150 - Aggressive release : disabled
16:29:02,904  INFO SettingsFactory:166 - Default batch fetch size: 1
16:29:02,904  INFO SettingsFactory:170 - Generate SQL with comments: disabled
16:29:02,904  INFO SettingsFactory:174 - Order SQL updates by primary key: disabled
16:29:02,904  INFO SettingsFactory:312 - Query translator: org.hibernate.hql.ast.ASTQueryTranslatorFactory
16:29:02,954  INFO ASTQueryTranslatorFactory:21 - Using ASTQueryTranslatorFactory
16:29:02,954  INFO SettingsFactory:182 - Query language substitutions: {}
16:29:02,964  INFO TransactionFactoryFactory:31 - Using default transaction strategy (direct JDBC transactions)
16:29:02,964  INFO TransactionManagerLookupFactory:33 - No TransactionManagerLookup configured (in JTA environment, use of read-write or transactional second-level cache is not recommended)
16:29:02,974  INFO SettingsFactory:191 - Automatic flush during beforeCompletion(): disabled
16:29:02,974  INFO SettingsFactory:195 - Automatic session close at end of transaction: disabled
16:29:02,974  INFO SettingsFactory:201 - Second-level cache: enabled
16:29:02,974  INFO SettingsFactory:205 - Query cache: disabled
16:29:02,974  INFO SettingsFactory:299 - Cache provider: org.hibernate.cache.EhCacheProvider
16:29:03,375  INFO SettingsFactory:220 - Optimize cache for minimal puts: disabled
16:29:03,375  INFO SettingsFactory:229 - Structured second-level cache entries: enabled
16:29:03,395  INFO SettingsFactory:253 - Statistics: disabled
16:29:03,395  INFO SettingsFactory:257 - Deleted entity synthetic identifier rollback: disabled
16:29:03,405  INFO SettingsFactory:271 - Default entity-mode: pojo
16:29:03,665  INFO SessionFactoryImpl:148 - building session factory
16:29:03,685  WARN Configurator:126 - No configuration found. Configuring ehcache from ehcache-failsafe.xml found in the classpath: jar:file:/E:/My%20Web/test/WebRoot/WEB-INF/lib/ehcache-1.1.jar!/ehcache-failsafe.xml
Initial SessionFactory creation failed.org.hibernate.MappingException: could not instantiate id generator
16:29:03,935 ERROR [action]:253 - Servlet.service() for servlet action threw exception
java.lang.ExceptionInInitializerError
at com.HibernateSessionFactory.<clinit>(HibernateSessionFactory.java:22)
at com.yourcompany.struts.form.AddAdminForm.validate(AddAdminForm.java:52)
at org.apache.struts.action.RequestProcessor.processValidate(RequestProcessor.java:942)
at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:255)
at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1482)
at org.apache.struts.action.ActionServlet.doGet(ActionServlet.java:507)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:689)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:252)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:178)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:126)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:105)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:107)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:148)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:869)
at org.apache.coyote.http11.Http11BaseProtocol$Http11ConnectionHandler.processConnection(Http11BaseProtocol.java:667)
at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:527)
at org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.java:80)
at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:684)
at java.lang.Thread.run(Thread.java:595)
Caused by: org.hibernate.MappingException: could not instantiate id generator
at org.hibernate.id.IdentifierGeneratorFactory.create(IdentifierGeneratorFactory.java:92)
at org.hibernate.mapping.SimpleValue.createIdentifierGenerator(SimpleValue.java:151)
at org.hibernate.impl.SessionFactoryImpl.<init>(SessionFactoryImpl.java:174)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1055)
at com.HibernateSessionFactory.<clinit>(HibernateSessionFactory.java:18)
... 21 more
Caused by: org.hibernate.MappingException: could not interpret id generator strategy: hilo.hex
at org.hibernate.id.IdentifierGeneratorFactory.getIdentifierGeneratorClass(IdentifierGeneratorFactory.java:103)
at org.hibernate.id.IdentifierGeneratorFactory.create(IdentifierGeneratorFactory.java:86)
... 25 more

#15


没有人知道吗,顶一下先

#16


Caused by: org.hibernate.MappingException: could not interpret id generator strategy: hilo.hex
at org.hibernate.id.IdentifierGeneratorFactory.getIdentifierGeneratorClass

主键生成器有问题

#17


同意楼上,你调错的时候主要看以“Caused by: ”打头的东西 。

#18


把ehcache-failsafe.xml这个文件放在scr中和hibernate-cfg-xml同级试试看!!

#19


两极缓存的路径找不到

#1


该回复被版主删除

#2


ehcache-failsafe.xml 是干什么的? 提示这个配置文件找不到

#3


lib里是不是没有这个包
ehcache-1.1.jar

#4


友情UP

#5


回复 dreamover

lib 里有这个包的

#6


我好像刚开始学hibernate时也是这样的问题,
你最好在发布的目录中建一个log4j.properties文件.
//log4j.properties内容如下----------
log4j.rootLogger=info,CONSOLE
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender 
log4j.appender.CONSOLE.Target=System.out 
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout 
log4j.appender.CONSOLE.layout.ConversionPattern= %4p [%t] (%F:%L) - %m%n
//--------------
当然lib中要有log4j-1.2.11.jar的
然后再运行看看,这样能看到明确的错误是什么.

#7


把hibernate里的ehcache.xml拷贝到src目录下,hibernate3用到了ehcache

#8


我曾经也出现过这个问题,把它放在应用服务器的容器中运行,把相应的jar包放进去就可以了,缺少容器是不能运行的

#9


用最新的MYECLIPS试试

#10


回复:ldg_2(绿豆糕)
按你的方法,我把那个XML文件放到了 classes 目录下,产生新的错误日志:
---------------------------------
09:24:01,833  WARN Configurator:126 - No configuration found. Configuring ehcache from ehcache-failsafe.xml found in the classpath: file:/E:/My%20Web/MSInfoSys/WEB-INF/classes/ehcache-failsafe.xml
---------------------------------
这是怎么回事,也就是这里,老是警告 configuration 找不到

#11


回复:jyh_jack() 
我的发布包中有 log4j.properties 这个包了

#12


回复:eddie404956() 
我现用的就是最新的 MyEclipse 5.0

#13


No configuration found. Configuring ehcache from ehcache-failsafe.xml found in the classpath: jar:file:/D:/workspace/HibernateText/lib/ehcache-1.1.jar!/ehcache-failsafe.xml

我这里也是这么显示的,但是运行很正常,你再看看下面有没有什么错误提示,把提示的内容全贴出来.

#14


这是应用了 struts 结构提示信息,里面还是有那个错误
-----------------------------------------------------
log4j:WARN No appenders could be found for logger (org.apache.catalina.session.ManagerBase).
log4j:WARN Please initialize the log4j system properly.
16:29:01,352  INFO Environment:464 - Hibernate 3.0.2
16:29:01,362  INFO Environment:477 - hibernate.properties not found
16:29:01,372  INFO Environment:510 - using CGLIB reflection optimizer
16:29:01,372  INFO Environment:540 - using JDK 1.4 java.sql.Timestamp handling
16:29:01,862  INFO Configuration:1160 - configuring from resource: /hibernate.cfg.xml
16:29:01,862  INFO Configuration:1131 - Configuration resource: /hibernate.cfg.xml
16:29:02,053  INFO Configuration:441 - Mapping resource: com/yourcompany/ManagerList.hbm.xml
16:29:02,263  INFO HbmBinder:258 - Mapping class: com.yourcompany.ManagerList -> ManagerList
16:29:02,313  INFO Configuration:1272 - Configured SessionFactory: null
16:29:02,313  INFO Configuration:852 - processing extends queue
16:29:02,313  INFO Configuration:856 - processing collection mappings
16:29:02,313  INFO Configuration:865 - processing association property references
16:29:02,313  INFO Configuration:894 - processing foreign key constraints
16:29:02,563  INFO DriverManagerConnectionProvider:41 - Using Hibernate built-in connection pool (not for production use!)
16:29:02,563  INFO DriverManagerConnectionProvider:42 - Hibernate connection pool size: 20
16:29:02,563  INFO DriverManagerConnectionProvider:45 - autocommit mode: false
16:29:02,573  INFO DriverManagerConnectionProvider:80 - using driver: com.microsoft.jdbc.sqlserver.SQLServerDriver at URL: jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=MsNews;SelectMethod=cursor
16:29:02,573  INFO DriverManagerConnectionProvider:86 - connection properties: {user=sa, password=****}
16:29:02,814  INFO SettingsFactory:71 - RDBMS: Microsoft SQL Server, version: Microsoft SQL Server  2000 - 8.00.2039 (Intel X86) 
May  3 2005 23:18:38 
Copyright (c) 1988-2003 Microsoft Corporation
Developer Edition on Windows NT 5.1 (Build 2600: Service Pack 2)

16:29:02,814  INFO SettingsFactory:72 - JDBC driver: SQLServer, version: 2.2.0029
16:29:02,884  INFO Dialect:92 - Using dialect: org.hibernate.dialect.SQLServerDialect
16:29:02,894  INFO SettingsFactory:130 - Scrollable result sets: enabled
16:29:02,894  INFO SettingsFactory:138 - JDBC3 getGeneratedKeys(): disabled
16:29:02,894  INFO SettingsFactory:150 - Aggressive release : disabled
16:29:02,904  INFO SettingsFactory:166 - Default batch fetch size: 1
16:29:02,904  INFO SettingsFactory:170 - Generate SQL with comments: disabled
16:29:02,904  INFO SettingsFactory:174 - Order SQL updates by primary key: disabled
16:29:02,904  INFO SettingsFactory:312 - Query translator: org.hibernate.hql.ast.ASTQueryTranslatorFactory
16:29:02,954  INFO ASTQueryTranslatorFactory:21 - Using ASTQueryTranslatorFactory
16:29:02,954  INFO SettingsFactory:182 - Query language substitutions: {}
16:29:02,964  INFO TransactionFactoryFactory:31 - Using default transaction strategy (direct JDBC transactions)
16:29:02,964  INFO TransactionManagerLookupFactory:33 - No TransactionManagerLookup configured (in JTA environment, use of read-write or transactional second-level cache is not recommended)
16:29:02,974  INFO SettingsFactory:191 - Automatic flush during beforeCompletion(): disabled
16:29:02,974  INFO SettingsFactory:195 - Automatic session close at end of transaction: disabled
16:29:02,974  INFO SettingsFactory:201 - Second-level cache: enabled
16:29:02,974  INFO SettingsFactory:205 - Query cache: disabled
16:29:02,974  INFO SettingsFactory:299 - Cache provider: org.hibernate.cache.EhCacheProvider
16:29:03,375  INFO SettingsFactory:220 - Optimize cache for minimal puts: disabled
16:29:03,375  INFO SettingsFactory:229 - Structured second-level cache entries: enabled
16:29:03,395  INFO SettingsFactory:253 - Statistics: disabled
16:29:03,395  INFO SettingsFactory:257 - Deleted entity synthetic identifier rollback: disabled
16:29:03,405  INFO SettingsFactory:271 - Default entity-mode: pojo
16:29:03,665  INFO SessionFactoryImpl:148 - building session factory
16:29:03,685  WARN Configurator:126 - No configuration found. Configuring ehcache from ehcache-failsafe.xml found in the classpath: jar:file:/E:/My%20Web/test/WebRoot/WEB-INF/lib/ehcache-1.1.jar!/ehcache-failsafe.xml
Initial SessionFactory creation failed.org.hibernate.MappingException: could not instantiate id generator
16:29:03,935 ERROR [action]:253 - Servlet.service() for servlet action threw exception
java.lang.ExceptionInInitializerError
at com.HibernateSessionFactory.<clinit>(HibernateSessionFactory.java:22)
at com.yourcompany.struts.form.AddAdminForm.validate(AddAdminForm.java:52)
at org.apache.struts.action.RequestProcessor.processValidate(RequestProcessor.java:942)
at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:255)
at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1482)
at org.apache.struts.action.ActionServlet.doGet(ActionServlet.java:507)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:689)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:252)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:178)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:126)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:105)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:107)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:148)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:869)
at org.apache.coyote.http11.Http11BaseProtocol$Http11ConnectionHandler.processConnection(Http11BaseProtocol.java:667)
at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:527)
at org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.java:80)
at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:684)
at java.lang.Thread.run(Thread.java:595)
Caused by: org.hibernate.MappingException: could not instantiate id generator
at org.hibernate.id.IdentifierGeneratorFactory.create(IdentifierGeneratorFactory.java:92)
at org.hibernate.mapping.SimpleValue.createIdentifierGenerator(SimpleValue.java:151)
at org.hibernate.impl.SessionFactoryImpl.<init>(SessionFactoryImpl.java:174)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1055)
at com.HibernateSessionFactory.<clinit>(HibernateSessionFactory.java:18)
... 21 more
Caused by: org.hibernate.MappingException: could not interpret id generator strategy: hilo.hex
at org.hibernate.id.IdentifierGeneratorFactory.getIdentifierGeneratorClass(IdentifierGeneratorFactory.java:103)
at org.hibernate.id.IdentifierGeneratorFactory.create(IdentifierGeneratorFactory.java:86)
... 25 more

#15


没有人知道吗,顶一下先

#16


Caused by: org.hibernate.MappingException: could not interpret id generator strategy: hilo.hex
at org.hibernate.id.IdentifierGeneratorFactory.getIdentifierGeneratorClass

主键生成器有问题

#17


同意楼上,你调错的时候主要看以“Caused by: ”打头的东西 。

#18


把ehcache-failsafe.xml这个文件放在scr中和hibernate-cfg-xml同级试试看!!

#19


两极缓存的路径找不到

#20