- This topic has 4 replies, 3 voices, and was last updated 18 years, 7 months ago by lambo82669.
-
AuthorPosts
-
jim5359MemberCan 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.
Haris PecoMemberJim,
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
jim5359MemberJust 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”
Haris PecoMemberJim,
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.htmlIf you have problem stiil, i will try make concrete example for you
Best
lambo82669Memberyou must call “new Configuration().configure().buildSessionFactory()” in order to have the sessionfactory register itself with the weblogic jndi context.
-
AuthorPosts