2012-03-18 80 views
1

我正在构建一个使用Spring MVC,Hibernate,JBoss Tools和JSF的Web应用程序框架。我已经设法通过使用JBoss Tools来生成域类和DAO类,但是,当我尝试构建任何DAO对象时(在构建服务的那一刻,最终服务将被注入到控制器中),我收到JNDI错误。我使用Tomcat 7作为AS。我希望能够解决这个问题。JNDI与Tomcat和休眠的会话工厂错误

控制器代码:

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

DAO /服务代码:

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"); 
    } 
} 
} 

堆栈跟踪:

javax.naming.NameNotFoundException:名称SessionFactory的是不是在这方面 约束在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) 在com.webapplication.service.AuthorHome。(AuthorHome.java:26)

回答

2

您需要配置Hibernate的Session厂春天里。请参阅http://static.springsource.org/spring/docs/current/spring-framework-reference/html/orm.html#orm-session-factory-setup。另请注意,在Spring内部直接使用Hibernate需要事务上下文。一个简单的方法是使用@Transactional注释。详情在这里:http://static.springsource.org/spring/docs/current/spring-framework-reference/html/transaction.html#transaction-declarative-annotations

+0

谢谢你的参考。我没有通过使用建议的内容来解决问题,但是,它的确引导我朝着正确的方向发展,我相信它将在未来有用。这真的很傻,服务找不到我的hibernate.cfg.xml,因为它引用的路径不正确。 – unknown 2012-03-18 20:55:17