문제

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)

도움이 되었습니까?

해결책

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top