1

I am building a web application framework by making use of Spring MVC, Hibernate, JBoss Tools and JSFs. I have managed to generate domain classes and DAO classes by making use of JBoss Tools, however, when I try to construct the any DAO object(At the moment I am constructing the service but ultimately the service will be injected into the controller), I receive a JNDI error. I am using Tomcat 7 as the AS. I would appreciate a simple solution to this problem.

Controller Code:

AuthorHome ah = new AuthorHome();
Author a = ah.findById(1);

DAO/Service Code:

public class AuthorHome {

private static final Log log = LogFactory.getLog(AuthorHome.class);

private final SessionFactory sessionFactory = getSessionFactory();

protected SessionFactory getSessionFactory() {
    try {
        return (SessionFactory) new InitialContext().lookup("SessionFactory");
    } catch (Exception e) {
        log.error("Could not locate SessionFactory in JNDI", e);
        throw new IllegalStateException(
                "Could not locate SessionFactory in JNDI");
    }
}
}

Stack Trace:

javax.naming.NameNotFoundException: Name SessionFactory is not bound in this Context at org.apache.naming.NamingContext.lookup(NamingContext.java:803) at org.apache.naming.NamingContext.lookup(NamingContext.java:159) at org.apache.naming.SelectorContext.lookup(SelectorContext.java:158) at javax.naming.InitialContext.lookup(Unknown Source) at com.webapplication.service.AuthorHome.getSessionFactory(AuthorHome.java:31) at com.webapplication.service.AuthorHome.(AuthorHome.java:26)

unknown
  • 13
  • 5

1 Answers1

2

You need to configure the Hibernate Session Factory inside of Spring. See http://static.springsource.org/spring/docs/current/spring-framework-reference/html/orm.html#orm-session-factory-setup. Also note that direct use of Hibernate inside of Spring requires a transactional context. A simple way to do so is to use the @Transactional annotation. Details here: http://static.springsource.org/spring/docs/current/spring-framework-reference/html/transaction.html#transaction-declarative-annotations.

atrain
  • 9,139
  • 1
  • 36
  • 40
  • Thank you for the references. I didn't solve the problem by making use of the suggested content, however, it did guide me in the right direction and I am sure it will be useful in the future. It was something silly really, the services couldn't find my hibernate.cfg.xml as the path it was referencing was incorrect. – unknown Mar 18 '12 at 20:55