配置Hibernate.cfg.xml

时间:2011-01-04 06:28:07

标签: java hibernate spring

如何告诉我的Spring,这是我的hibernate.cf.xml ...我收到此错误。我没有在application-context.xml中配置任何东西

Initial SessionFactory creation failed.org.hibernate.HibernateException: /hibernate.cfg.xml not found
com.vaadin.event.ListenerMethod$MethodException
Cause: java.lang.ExceptionInInitializerError
        at com.vaadin.event.ListenerMethod.receiveEvent(ListenerMethod.java:507)
        at com.vaadin.event.EventRouter.fireEvent(EventRouter.java:161)
        at com.vaadin.ui.AbstractComponent.fireEvent(AbstractComponent.java:1154)
        at com.vaadin.ui.Button.fireClick(Button.java:371)
        at com.vaadin.ui.Button.changeVariables(Button.java:193)
        at com.vaadin.terminal.gwt.server.AbstractCommunicationManager.handleVariables(AbstractCommunicationManager.java:1
        at com.vaadin.terminal.gwt.server.AbstractCommunicationManager.doHandleUidlRequest(AbstractCommunicationManager.ja
        at com.vaadin.terminal.gwt.server.CommunicationManager.handleUidlRequest(CommunicationManager.java:266)
        at com.vaadin.terminal.gwt.server.AbstractApplicationServlet.service(AbstractApplicationServlet.java:476)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:820)
        at org.mortbay.jetty.servlet.ServletHolder.handle(ServletHolder.java:511)
        at org.mortbay.jetty.servlet.ServletHandler.handle(ServletHandler.java:390)
        at org.mortbay.jetty.security.SecurityHandler.handle(SecurityHandler.java:216)
        at org.mortbay.jetty.servlet.SessionHandler.handle(SessionHandler.java:182)
        at org.mortbay.jetty.handler.ContextHandler.handle(ContextHandler.java:765)
        at org.mortbay.jetty.webapp.WebAppContext.handle(WebAppContext.java:418)
        at org.mortbay.jetty.handler.ContextHandlerCollection.handle(ContextHandlerCollection.java:230)
        at org.mortbay.jetty.handler.HandlerCollection.handle(HandlerCollection.java:114)
        at org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:152)
        at org.mortbay.jetty.Server.handle(Server.java:326)
        at org.mortbay.jetty.HttpConnection.handleRequest(HttpConnection.java:542)
        at org.mortbay.jetty.HttpConnection$RequestHandler.content(HttpConnection.java:943)
        at org.mortbay.jetty.HttpParser.parseNext(HttpParser.java:756)
        at org.mortbay.jetty.HttpParser.parseAvailable(HttpParser.java:218)
        at org.mortbay.jetty.HttpConnection.handle(HttpConnection.java:404)
        at org.mortbay.io.nio.SelectChannelEndPoint.run(SelectChannelEndPoint.java:410)
        at org.mortbay.thread.QueuedThreadPool$PoolThread.run(QueuedThreadPool.java:582)
Caused by: java.lang.ExceptionInInitializerError
        at your.intermedix.persistence.HibernateUtil.buildSessionFactory(HibernateUtil.java:18)
        at your.intermedix.persistence.HibernateUtil.<clinit>(HibernateUtil.java:8)
        at your.intermedix.services.ContactSerImpl.saveContact(ContactSerImpl.java:17)
        at your.intermedix.MyVaadinApplication.buttonClick(MyVaadinApplication.java:80)
        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 com.vaadin.event.ListenerMethod.receiveEvent(ListenerMethod.java:487)
        ... 26 more
Caused by: org.hibernate.HibernateException: /hibernate.cfg.xml not found
        at org.hibernate.util.ConfigHelper.getResourceAsStream(ConfigHelper.java:147)
        at org.hibernate.cfg.Configuration.getConfigurationInputStream(Configuration.java:1411)
        at org.hibernate.cfg.Configuration.configure(Configuration.java:1433)
        at org.hibernate.cfg.Configuration.configure(Configuration.java:1420)
        at your.intermedix.persistence.HibernateUtil.buildSessionFactory(HibernateUtil.java:13)

2 个答案:

答案 0 :(得分:2)

如果您正在使用Spring配置Hibernate,那么在会话工厂中,您可以通过这种方式将位置固定到您的文件中,如果不是这种情况,请使用足够的信息更新您的问题

<bean id="mySessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation">
<value>file:src/hibernate.cfg.xml</value>
</property>

答案 1 :(得分:0)

如果您使用Spring with Hibernate,那么您可以执行类似的操作。

<beans>
    <bean id="YourClass"
        class="com.YourClass.
        <property name="sessionFactory">
            <ref bean="DbSessionFactory" />
        </property>     
    </bean>


    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
        <property name="driverClassName">
            <value>org.postgresql.Driver</value>
        </property>
        <property name="url">
            <value>jdbc:postgresql://localhost/yourdb</value>
        </property>
        <property name="username">
            <value>postgres</value>
        </property>
        <property name="password">
            <value>postgres</value>
        </property>     
    </bean>

    <bean id="DbSessionFactory"
        class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
        <property name="dataSource">
            <ref local="dataSource"/>
        </property>     
        <property name="mappingResources">
            <list>
                <value>conf/hibernate/UserMapping.hbm.xml</value>               
            </list>
        </property>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect"> org.hibernate.dialect.PostgreSQLDialect </prop>      
                <prop key="hibernate.hbm2ddl.auto">update</prop>
                <prop key="hibernate.cache.use_second_level_cache"> true </prop>
                <prop key="hibernate.cache.use_query_cache">true</prop>
            </props>
        </property>
    </bean>
</beans>