在项目中用到了hibernate,是通过在 jboss里设置一个jndi数据源去访问的,web环境下运行每问题 ,但是在用junit对其做单元测试时,启动hibernate报错
2982 [main] INFO org.hibernate.util.NamingHelper - JNDI InitialContext properties:{}2982 [main] WARN org.hibernate.impl.SessionFactoryObjectFactory - Could not bind factory to JNDI
javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file: java.naming.factory.initial
at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:645)
at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:288)
at javax.naming.InitialContext.getURLOrDefaultInitCtx(InitialContext.java:325)
at javax.naming.InitialContext.getNameParser(InitialContext.java:480)
at org.hibernate.util.NamingHelper.bind(NamingHelper.java:75)
at org.hibernate.impl.SessionFactoryObjectFactory.addInstance(SessionFactoryObjectFactory.java:113)
at org.hibernate.impl.SessionFactoryImpl.<init>(SessionFactoryImpl.java:338)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1327)
...
查了一下,原来是hibernate.cfg.xml文件中有个机关
<hibernate-configuration>
<session-factory name="sample">
....
</session-factory>
</hibernate-configuration>
当设置了session-factory元素的name属性时,hibernate会把session-factory注册到jndi中,因为我们单元测试是脱离web容器的,因此这个时候就报错了,只要把