facebook

[Closed] Hibernate Error At Startup

  1. MyEclipse IDE
  2.  > 
  3. General Development
Viewing 11 posts - 1 through 11 (of 11 total)
  • Author
    Posts
  • #262440 Reply

    I’m using MyEclipse 5.1GA and created a project with Hibernate 3.1, JSF, and Web capabilities. I configured hibernate for one table to use a Tomcat 5.5 JNDI data source and have a simple .jsp page that accesses that table using a backing bean. The first time I launch the page Hibernate errors when loading the configuration (see error stack below). I submit the page again and then the configuration loads and everything is well. It is like the process is not waiting for the Hibernate configuration to load…. I appreciate any help you can offer 🙂

    org.hibernate.MappingException: Could not read mappings from resource: com/nomad/db/Userinfo.hbm.xml
    at org.hibernate.cfg.Configuration.addResource(Configuration.java:485)
    at org.hibernate.cfg.Configuration.parseMappingElement(Configuration.java:1465)
    at org.hibernate.cfg.Configuration.parseSessionFactory(Configuration.java:1433)
    at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1414)
    at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1390)
    at org.hibernate.cfg.Configuration.configure(Configuration.java:1310)
    at com.nomad.hibernate.HibernateSessionFactory.rebuildSessionFactory(HibernateSessionFactory.java:59)
    at com.nomad.hibernate.HibernateSessionFactory.getSession(HibernateSessionFactory.java:43)
    at com.nomad.db.BaseHibernateDAO.getSession(BaseHibernateDAO.java:14)
    at com.nomad.db.UserinfoDAO.findByExample(UserinfoDAO.java:81)
    at com.nomad.db.UserinfoDAO.findByLogin(UserinfoDAO.java:97)
    at com.nomad.bb.Visit.logOn(Visit.java:59)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.apache.myfaces.el.MethodBindingImpl.invoke(MethodBindingImpl.java:129)
    at org.apache.myfaces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:63)
    at javax.faces.component.UICommand.broadcast(UICommand.java:106)
    at javax.faces.component.UIViewRoot._broadcastForPhase(UIViewRoot.java:90)
    at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:164)
    at org.apache.myfaces.lifecycle.LifecycleImpl.invokeApplication(LifecycleImpl.java:316)
    at org.apache.myfaces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:86)
    at javax.faces.webapp.FacesServlet.service(FacesServlet.java:106)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:252)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
    at org.apache.myfaces.trinidadinternal.webapp.TrinidadFilterImpl._invokeDoFilter(TrinidadFilterImpl.java:327)
    at org.apache.myfaces.trinidadinternal.webapp.TrinidadFilterImpl._doFilterImpl(TrinidadFilterImpl.java:291)
    at org.apache.myfaces.trinidadinternal.webapp.TrinidadFilterImpl.doFilter(TrinidadFilterImpl.java:214)
    at org.apache.myfaces.trinidad.webapp.TrinidadFilter.doFilter(TrinidadFilter.java:90)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:202)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
    at org.apache.myfaces.component.html.util.ExtensionsFilter.doFilter(ExtensionsFilter.java:122)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:202)
    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:664)
    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(Unknown Source)
    Caused by: org.hibernate.MappingException: Could not parse mapping document in input stream
    at org.hibernate.cfg.Configuration.addInputStream(Configuration.java:431)
    at org.hibernate.cfg.Configuration.addResource(Configuration.java:482)
    … 46 more
    Caused by: org.dom4j.DocumentException: FWK005 parse may not be called while parsing. Nested exception: FWK005 parse may not be called while parsing.
    at org.dom4j.io.SAXReader.read(SAXReader.java:484)
    at org.hibernate.cfg.Configuration.addInputStream(Configuration.java:422)
    … 47 more

    #262550 Reply

    Haris Peco
    Member

    [email protected],

    Could you send example of project (if it is possible) which throw this log on [email protected] with subject ATTN: snpe (you haven’t to send libraries).You have some threading problem and I can’t reproduce it ?

    Thanks,

    #262608 Reply

    Sent the zip file without libraries. I really appreciate you looking into this! Thank you 🙂

    #262609 Reply

    Note: I have a few more tables condifugred now in the zip provided but still gets the provided error because the Userinfo table is the first thing used at login… Once I get Hibernate to load the other tables and Userinfo work fine… Weird…

    #262653 Reply

    Haris Peco
    Member

    [email protected],

    I can reproduce your case.
    Please, try change HibernateSessionfactory#rebuildSessionFactory to :

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

    Sorry for the inconvenience caused .
    I will try update hibernate templates

    #262657 Reply

    Thank you for your quick responses!!! The .jsp page still comes back with an error and the “cannot read Userinfo.xbm.xml” is on the console but I can see the console continue on and load the Hibernate configuration. It seems the create session is not waiting until it is done? Is there a way to make it wait until it is loaded?

    #262658 Reply

    Haris Peco
    Member

    [email protected] ,

    Please add this on start rebuildSessionFactory (copy this method from my previous post)

    if (sessionFactory != null)
             return; 

    Regards,

    #262752 Reply

    I did put this in?

    /**
    * Rebuild hibernate session factory
    *
    */
    public static void rebuildSessionFactory() {
    if (sessionFactory != null)
    return;
    try {
    configuration.configure(configFile);
    sessionFactory = configuration.buildSessionFactory();
    } catch (Exception e) {
    System.err
    .println(“%%%% Error Creating SessionFactory %%%%”);
    e.printStackTrace();
    }
    }

    I still get ther error…
    %%%% Error Creating SessionFactory %%%%
    org.hibernate.MappingException: Could not read mappings from resource: com/nomad/db/Userinfo.hbm.xml
    at org.hibernate.cfg.Configuration.addResource(Configuration.java:485)
    at org.hibernate.cfg.Configuration.parseMappingElement(Configuration.java:1465)

    #262755 Reply

    Haris Peco
    Member

    Please try add static initializer.Sorry for inconvvenience caused, but I don’t like static initializer, because I sometime want use this class for configration only (without building session factory).

    Please, send us if it helps.
    Thank you

    This is complete class with static initializer.

    
    import org.hibernate.HibernateException;
    import org.hibernate.Session;
    import org.hibernate.cfg.Configuration;
    
    /**
     * Configures and provides access to Hibernate sessions, tied to the
     * current thread of execution.  Follows the Thread Local Session
     * pattern, see {@link http://hibernate.org/42.html }.
     */
    public class HibernateSessionFactory {
    
        /** 
         * Location of hibernate.cfg.xml file.
         * Location should be on the classpath as Hibernate uses  
         * #resourceAsStream style lookup for its configuration file. 
         * The default classpath location of the hibernate config file is 
         * in the default package. Use #setConfigFile() to update 
         * the location of the configuration file for the current session.   
         */
        private static String CONFIG_FILE_LOCATION = "/hibernate.cfg.xml";
        private static final ThreadLocal threadLocal = new ThreadLocal();
        private  static Configuration configuration = new Configuration();
        private static org.hibernate.SessionFactory sessionFactory;
        private static String configFile = CONFIG_FILE_LOCATION;
    
        static {
            try {
                configuration.configure(configFile);
                sessionFactory = configuration.buildSessionFactory();
            } catch (Exception e) {
                System.err
                        .println("%%%% Error Creating SessionFactory %%%%");
                e.printStackTrace();
            }
        }
        private HibernateSessionFactory() {
        }
        
        /**
         * Returns the ThreadLocal Session instance.  Lazy initialize
         * the <code>SessionFactory</code> if needed.
         *
         *  @return Session
         *  @throws HibernateException
         */
        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;
        }
    
        /**
         *  Rebuild hibernate session factory
         *
         */
        public synchronized static void rebuildSessionFactory() {
            try {
                configuration.configure(configFile);
                sessionFactory = configuration.buildSessionFactory();
            } catch (Exception e) {
                System.err
                        .println("%%%% Error Creating SessionFactory %%%%");
                e.printStackTrace();
            }
        }
    
        /**
         *  Close the single hibernate session instance.
         *
         *  @throws HibernateException
         */
        public static void closeSession() throws HibernateException {
            Session session = (Session) threadLocal.get();
            threadLocal.set(null);
    
            if (session != null) {
                session.close();
            }
        }
    
        /**
         *  return session factory
         *
         */
        public static org.hibernate.SessionFactory getSessionFactory() {
            return sessionFactory;
        }
    
        /**
         *  return session factory
         *
         *    session factory will be rebuilded in the next call
         */
        public static void setConfigFile(String configFile) {
            HibernateSessionFactory.configFile = configFile;
            sessionFactory = null;
        }
    
        /**
         *  return hibernate configuration
         *
         */
        public static Configuration getConfiguration() {
            return configuration;
        }
    
    

    and this is change from standard MyEclipse session factory :

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

    That seems to have fixed my issue! I really appreciate the time and effort you put into helping me on this.

    Thank you very much!!! 🙂

    #262880 Reply

    Haris Peco
    Member

    [email protected],

    You are welcome.Thank you for your patience and sorry for the inconvenience caused.
    I will try update templates in shortly

    Best regards,

Viewing 11 posts - 1 through 11 (of 11 total)
Reply To: [Closed] Hibernate Error At Startup

You must be logged in to post in the forum log in