facebook

HibernateSessionFactory.currentSession exception

  1. MyEclipse Archived
  2.  > 
  3. Database Tools (DB Explorer, Hibernate, etc.)
Viewing 15 posts - 1 through 15 (of 23 total)
  • Author
    Posts
  • #229308 Reply

    brlmcguire
    Member

    Hello. I am getting the following exception in a test app. Any help would be appreciated.

    WinXP Home, XP1
    Eclipse 3.02 Build 200503110845
    Eclipse fresh install for MyEclipse
    No other plugins installed.
    8 plugins start with org.eclipse.pde
    MyEclipse version 3.8.4
    JDK 1.4.2_08
    Tomcat 5.0.28 installed using JDK 1.4.2_08

    C:\eclipse\workspace\.metadata\.log is empty.

    Localhost exception (C:\Program Files\Apache Software Foundation\Tomcat 5.0\logs)
    java.lang.NullPointerException
    at com.tkm.db.HibernateSessionFactory.currentSession(HibernateSessionFactory.java:56)
    at thisIsATest.doGet(thisIsATest.java:67)
    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:237)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:157)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:214)
    at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:104)
    at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:520)
    at org.apache.catalina.core.StandardContextValve.invokeInternal(StandardContextValve.java:198)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:152)
    at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:104)
    at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:520)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:137)
    at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:104)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:118)
    at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:102)
    at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:520)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
    at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:104)
    at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:520)
    at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:929)
    at org.apache.coyote.tomcat5.CoyoteAdapter.service(CoyoteAdapter.java:160)
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:799)
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.processConnection(Http11Protocol.java:705)
    at org.apache.tomcat.util.net.TcpWorkerThread.runIt(PoolTcpEndpoint.java:577)
    at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:683)
    at java.lang.Thread.run(Thread.java:534)

    Console Exception (within Eclipse)
    %%%% Error Creating SessionFactory %%%%
    net.sf.hibernate.MappingException: Error reading resource: com/tkm/db/Person.hbm.xml
    at net.sf.hibernate.cfg.Configuration.addResource(Configuration.java:339)
    at net.sf.hibernate.cfg.Configuration.doConfigure(Configuration.java:1018)
    at net.sf.hibernate.cfg.Configuration.doConfigure(Configuration.java:974)
    at net.sf.hibernate.cfg.Configuration.configure(Configuration.java:902)
    at com.tkm.db.HibernateSessionFactory.currentSession(HibernateSessionFactory.java:48)
    at thisIsATest.doGet(thisIsATest.java:67)
    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:237)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:157)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:214)
    at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:104)
    at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:520)
    at org.apache.catalina.core.StandardContextValve.invokeInternal(StandardContextValve.java:198)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:152)
    at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:104)
    at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:520)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:137)
    at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:104)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:118)
    at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:102)
    at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:520)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
    at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:104)
    at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:520)
    at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:929)
    at org.apache.coyote.tomcat5.CoyoteAdapter.service(CoyoteAdapter.java:160)
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:799)
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.processConnection(Http11Protocol.java:705)
    at org.apache.tomcat.util.net.TcpWorkerThread.runIt(PoolTcpEndpoint.java:577)
    at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:683)
    at java.lang.Thread.run(Thread.java:534)
    Caused by: net.sf.hibernate.MappingException: duplicate import: Person
    at net.sf.hibernate.cfg.Mappings.addImport(Mappings.java:85)
    at net.sf.hibernate.cfg.Binder.bindClass(Binder.java:127)
    at net.sf.hibernate.cfg.Binder.bindRootClass(Binder.java:222)
    at net.sf.hibernate.cfg.Binder.bindRoot(Binder.java:1257)
    at net.sf.hibernate.cfg.Configuration.add(Configuration.java:252)
    at net.sf.hibernate.cfg.Configuration.addInputStream(Configuration.java:288)
    at net.sf.hibernate.cfg.Configuration.addResource(Configuration.java:336)
    … 31 more

    ———————–
    Code in servlet is quite small. Just testing at this point, trying to get past this error.

    List people = null ;
    Configuration config = new Configuration() ;

    try
    {
    config.addClass(Person.class);
    Session session = HibernateSessionFactory.currentSession();
    }
    catch (MappingException ex)
    {
    ex.printStackTrace() ;
    }
    catch (HibernateException ex)
    {
    ex.printStackTrace() ;
    }

    Thanks very much,

    Bruce.

    #229345 Reply

    Riyad Kalla
    Member

    Console Exception (within Eclipse)
    %%%% Error Creating SessionFactory %%%%
    net.sf.hibernate.MappingException: Error reading resource: com/tkm/db/Person.hbm.xml

    Hmm let’s fix this first, if you navigate into your source folder, and down into com/tkm/db package, do you have a Person.hbm.xml file? Does it have errors?

    #229396 Reply

    brlmcguire
    Member

    @support-rkalla wrote:

    Console Exception (within Eclipse)
    %%%% Error Creating SessionFactory %%%%
    net.sf.hibernate.MappingException: Error reading resource: com/tkm/db/Person.hbm.xml

    Hmm let’s fix this first, if you navigate into your source folder, and down into com/tkm/db package, do you have a Person.hbm.xml file? Does it have errors?

    Yes, the Person.hbm.xml file exists, but there are no errors listed for it.

    #229401 Reply

    Riyad Kalla
    Member

    If you open up your Navigator View and go into your output dir and drill down to com/tkm/db do you see the Person.hbm.xml file? I want to know why hibernate is complaining about loading this mapping file…

    #229437 Reply

    support-jeff
    Member

    Look farther down the original stacktrace and you will see:

    Caused by: net.sf.hibernate.MappingException: duplicate import: Person

    Does your hibernate.cfg.xml file contain two entries for Person.hbm.xml?

    #230570 Reply

    brlmcguire
    Member

    Hello All.

    I appreciate your patience and persistance.

    I finally got so frustrated that I uninstalled the works, myEclipse, Eclipse, JDK, darn near the OS, then re-installed the whole thing.

    Of course, the problem went away. Sigh.

    Thanks again,

    Bruce.

    #236520 Reply

    telly
    Member

    Hi, I am receiving the exact error brlmcguire received. Since he didn’t stay long enough to receive a response, I would like to continue the help you all started.

    Yes, my *.hbm.xml file does exist (Applicant.hbm.xml to be exact) and it does not contain any errors in it. The only difference between my project and brlmcguire’s project is my classes and map files are all in the default package (/src directory).

    I did received the “… duplicate import: Applicant…” exception as well. I check the hibernate.cfg.xml file and I only have one entry for my Applicant.hbm.xml file.

    Thank you in advance for any assistance you can provide. =(

    #236536 Reply

    Riyad Kalla
    Member

    The only difference between my project and brlmcguire’s project is my classes and map files are all in the default package (/src directory).

    Ahhh don’t do that, put them in a package. God will strike you down if you leave them in the default package… I’ve seen it happen.

    #237644 Reply

    shadoweucom
    Member

    This message has not been recovered.

    #239943 Reply

    Marc Siramy
    Member

    I’m new to Hibernate and I have to face exactly to the same problem.
    I’ve just resolved a bug, found in another thread in the forum, This bug was in the ‘SessionFactory’ class in method execute: I’ve changed the code in the tutorial by this code:

    
    // write instead
            if (session == null || !session.isOpen() )  {
                    try {
                        cfg.configure(CONFIG_FILE_LOCATION);
                        sessionFactory = cfg.buildSessionFactory();
                    }
                    catch (Exception e) {
                        System.err.println("%%%% Error Creating SessionFactory %%%%");
                        e.printStackTrace();
                    }
                session = sessionFactory.openSession();
                threadLocal.set(session);
            }
    

    and since then hopefully, I can read the correct page in the browser. But even if the display in the browser is correct ,
    an error still occurs:

    
    18:58:52,052 INFO  [Configuration] configuring from resource: /hibernate.cfg.xml
    18:58:52,095 INFO  [Configuration] Configuration resource: /hibernate.cfg.xml
    18:58:52,110 INFO  [Configuration] Mapping resource: com/nscorp/hibernate/Vipdata.hbm.xml
    18:58:52,116 ERROR [Configuration] Could not compile the mapping document
    net.sf.hibernate.MappingException: duplicate import: Vipdata
            at net.sf.hibernate.cfg.Mappings.addImport(Mappings.java:85)
    ...
    Caused by: net.sf.hibernate.MappingException: duplicate import: Vipdata
            at net.sf.hibernate.cfg.Mappings.addImport(Mappings.java:85)
            at net.sf.hibernate.cfg.Binder.bindClass(Binder.java:126)
            at net.sf.hibernate.cfg.Binder.bindRootClass(Binder.java:221)
    

    Even if this problem exists, data are written correctly in MySQL database and the browser displays them correctly.

    So since last posts on this subject, did someone found a solution to resolve this bug ?

    Cordially ,

    – Marc

    #239958 Reply

    I’ve the same problem than msiramy
    but my code is

    
    public static Session currentSession() throws HibernateException {
    Session session = (Session) threadLocal.get();
    if (session != null && !session.isOpen())
    session = null;
    if (session == null) {
    if (sessionFactory == null) {
            try {
                cfg.configure(CONFIG_FILE_LOCATION);
                sessionFactory = cfg.buildSessionFactory();
            } catch (Exception e) {
    System.err.println("%%%% Error Creating SessionFactory %%%%");
            e.printStackTrace();
                    }
                }
                session = sessionFactory.openSession();
                threadLocal.set(session);
            }
    
            return session;
        }
    

    Hope that somebody can help us

    Regards
    Rinaldo

    #239971 Reply

    Riyad Kalla
    Member

    Marc & Rinaldo you are both getting duplicate importa exceptions? If that is the case why are you showing code for the SessionFactory class? Are you having other troubles not shown here with it?

    #240008 Reply

    Marc Siramy
    Member

    Personnally, I had a bug with SessionFactory class as it is defined in the tutorial and in Rinaldo message. That’s why I’ve changed the SessionFactory code, as I’ve mentioned above. Without this modification, nothing can be display in my browser. This error, as I can remember, was explained and resolved in another thread by someone else.

    So, after having “resolved” this first bug, the sample example works as shown in the demo tutorial exept the fact I always obtain a message error:

    
     net.sf.hibernate.MappingException: duplicate import: Vipdata
            at net.sf.hibernate.cfg.Mappings.addImport(Mappings.java:85) 
    

    Bests Regards,

    Marc

    #240014 Reply

    Riyad Kalla
    Member

    Can you past your addImport method here?

    #240022 Reply

    Marc Siramy
    Member

    I’m not sure to understand. I think you talk about getVipdataList() and addVipdata(Vipdata data) in VipService .
    This is my code for these methods:

    
     public void addVipdata(Vipdata data){
            Session session = null;
            try{
                session = SessionFactory.currentSession();
                session.save(data);
                session.flush();
            }
            catch (HibernateException e){
                System.err.println("Hibernate Exception" + e.getMessage());
                throw new RuntimeException(e);
            }
            finally
            {
                if (session != null){
                    try{
                        session.close();
                    }
                    catch (HibernateException e){
                        System.err.println("Hibernate Exception" + e.getMessage());
                        throw new RuntimeException(e);
                    }
                }
            }
        }
    
    
     public List getVipdataList(){
    Session session = null;
            try{
                session = SessionFactory.currentSession();
                Query query =    session.createQuery(
                        "select Vipdata from com.nscorp.hibernate.Vipdata Vipdata order by Vipdata.vipName");
                return query.list();
            }
            catch (HibernateException e){
                System.err.println("Hibernate Exception" + e.getMessage());
                throw new RuntimeException(e);
            }
            finally    {
                if (session != null){
                    try{
                        session.close();
                    }
                    catch (HibernateException e){
                        System.err.println("Hibernate Exception" + e.getMessage());
                        throw new RuntimeException(e);
                    }
                }
            }
        }
    

    .
    Marc

Viewing 15 posts - 1 through 15 (of 23 total)
Reply To: HibernateSessionFactory.currentSession exception

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