代码之家  ›  专栏  ›  技术社区  ›  Stan Kurilin

用弹簧配置休眠

  •  2
  • Stan Kurilin  · 技术社区  · 14 年前

    我正在尝试将Spring与Hibernate集成,但捕获NamingException:Context是只读的

    堆栈跟踪

    WARNING: Could not bind factory to JNDI
    javax.naming.NamingException: Context is read only
        at org.apache.naming.NamingContext.checkWritable(NamingContext.java:903)
        at org.apache.naming.NamingContext.createSubcontext(NamingContext.java:517)
        at org.apache.naming.NamingContext.createSubcontext(NamingContext.java:538)
        at org.apache.naming.SelectorContext.createSubcontext(SelectorContext.java:422)
        at javax.naming.InitialContext.createSubcontext(InitialContext.java:464)
        at org.hibernate.util.NamingHelper.bind(NamingHelper.java:69)
        at org.hibernate.impl.SessionFactoryObjectFactory.addInstance(SessionFactoryObjectFactory.java:90)
        at org.hibernate.impl.SessionFactoryImpl.<init>(SessionFactoryImpl.java:306)
        at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1300)
        at org.springframework.orm.hibernate3.LocalSessionFactoryBean.newSessionFactory(LocalSessionFactoryBean.java:855)
        at org.springframework.orm.hibernate3.LocalSessionFactoryBean.buildSessionFactory(LocalSessionFactoryBean.java:774)
        at org.springframework.orm.hibernate3.AbstractSessionFactoryBean.afterPropertiesSet(AbstractSessionFactoryBean.java:211)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1460)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1398)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:512)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:450)
        at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:290)
        at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
        at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:287)
        at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:189)
        at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:540)
        at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:842)
        at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:416)
        at org.springframework.web.context.ContextLoader.createWebApplicationContext(ContextLoader.java:261)
        at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:192)
        at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:47)
        at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:3843)
        at org.apache.catalina.core.StandardContext.start(StandardContext.java:4350)
        at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:791)
        at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:771)
        at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:525)
        at org.apache.catalina.startup.HostConfig.deployDescriptor(HostConfig.java:626)
        at org.apache.catalina.startup.HostConfig.deployDescriptors(HostConfig.java:553)
        at org.apache.catalina.startup.HostConfig.deployApps(HostConfig.java:488)
        at org.apache.catalina.startup.HostConfig.start(HostConfig.java:1147)
        at org.apache.catalina.startup.HostConfig.lifecycleEvent(HostConfig.java:311)
        at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:117)
        at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1053)
        at org.apache.catalina.core.StandardHost.start(StandardHost.java:719)
        at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1045)
        at org.apache.catalina.core.StandardEngine.start(StandardEngine.java:443)
        at org.apache.catalina.core.StandardService.start(StandardService.java:516)
        at org.apache.catalina.core.StandardServer.start(StandardServer.java:710)
        at org.apache.catalina.startup.Catalina.start(Catalina.java:578)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
        at java.lang.reflect.Method.invoke(Method.java:597)
        at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:288)
        at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:413)
    

    组态

    <bean id="myDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
        <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
        <property name="url" value="jdbc:mysql://localhost/school"/>
        <property name="username" value="root"/>
        <property name="password" value="toor"/>
    </bean>
    
    <bean id="hibernateSessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
        <property name="dataSource" ref="myDataSource"/>
        <property name="configLocation" value="/WEB-INF/hib.cfg.xml"/>
    </bean>
    

    H.CF.G.XML

    <hibernate-configuration>
        <session-factory name="java:hibernate/SessionFactory">
            <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
            <property name="show_sql">false</property>
            <property name="transaction.factory_class">
                org.hibernate.transaction.JTATransactionFactory
            </property>
            <property name="jta.UserTransaction">java:comp/UserTransaction</property>
            <property name="configClass">org.hibernate.cfg.AnnotationConfiguration</property>
    
            <mapping class="com.domain.Worker"/>
        </session-factory>
    </hibernate-configuration>
    

    我如何解决这个问题?

    谢谢。

    1 回复  |  直到 14 年前
        1
  •  6
  •   Ross    14 年前

    尝试从hib.cfg.xml文件中删除会话工厂名,使其看起来像:

    <hibernate-configuration>
        <session-factory>
            <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
            <property name="show_sql">false</property>
            <property name="transaction.factory_class">
                org.hibernate.transaction.JTATransactionFactory
            </property>
            <property name="jta.UserTransaction">java:comp/UserTransaction</property>
            <property name="configClass">org.hibernate.cfg.AnnotationConfiguration</property>
    
            <mapping class="com.domain.Worker"/>
        </session-factory>
    </hibernate-configuration>