facebook

Hibernate DAO without Spring

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

    jim5359
    Member

    Can anyone provide an example of using the generated DAOs using JNDI rather than using Spring. I can’t figure out how or where I am supposed to instantiate the SessionFactory. I have set the session_factory_name property in my hibernate.cfg.xml file to the desired JNDI name. When I generated the DAO I used the same JNDI name. But when I try to execute a function for the DAO I get the error:
    “Could not locate SessionFactory in JNDI”

    I am using Tomcat. Is there something I have to setup in Tomcat? Is there some place I need to instantiate the sessionfactory so it gets added to the JNDI? Any help would be appreciated.

    #249051 Reply

    Haris Peco
    Member

    Jim,

    Have you example (cfg.xml, mappings, POJO, example application code)
    You have to set JNDI datasource in tomcat and you can use tomcat’s JNIDI in tomcat’s JVM only (web application)

    Best

    #249062 Reply

    jim5359
    Member

    Just to be clear, I’m not talking about a JNDI datasource. I’m talking about a JNDI DAO. When you create a hibernate mapping for a table within the database explorer, one of the options is to create a Java Data Access Object (DAO). There are two options for the DAO type, “Spring DAO” and “JNDI DAO”. If you choose “JNDI DAO” then you must enter the JNDI path.

    Are there any examples on how to use this option?

    Here is some of the code it generates for the DAO:

    protected SessionFactory getSessionFactory() {
    try {
    return (SessionFactory) new InitialContext().lookup(“java:comp/env/hibernate/SessionFactory”);
    } catch (Exception e) {
    log.error(“Could not locate SessionFactory in JNDI”, e);
    return cfg.buildSessionFactory();
    throw new IllegalStateException(“Could not locate SessionFactory in JNDI”);
    }
    }

    Whenever I call one of the DAO functions, it calls getSessionFactory(), which generates the error:
    “Could not locate SessionFactory in JNDI”

    #249063 Reply

    Haris Peco
    Member

    Jim,

    JNDI Dao je datasource in application server.You have to configure datasource in server and use name in DAO .If you use tomcat you have to execute application in tomcat context (tomcat JVM)
    see tomcat JNDI documentation and tomcat JNDI datasource http://tomcat.apache.org/tomcat-5.5-doc/jndi-datasource-examples-howto.html

    If you have problem stiil, i will try make concrete example for you

    Best

    #253556 Reply

    lambo82669
    Member

    you must call “new Configuration().configure().buildSessionFactory()” in order to have the sessionfactory register itself with the weblogic jndi context.

Viewing 5 posts - 1 through 5 (of 5 total)
Reply To: Hibernate DAO without Spring

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