jboss 4.0.5中的hibernate配置

时间:2014-09-29 08:57:42

标签: hibernate jboss-4.0.x

我正在尝试使用jdeveloper postgres 9.3和jboss 4.0.5创建一个hibernate项目。 虽然当我调用一个简单的servlet时,我可以通过jdeveloper连接连接数据库

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:1329) at org.hibernate.cfg.Configuration.configure(Configuration.java:1351) at testingmodel2.Example1Servlet.$init$(Example1Servlet.java:18) at testingmodel2.Example1Servlet.<init>(Example1Servlet.java:17) at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27) at java.lang.reflect.Constructor.newInstance(Constructor.java:494) at java.lang.Class.newInstance0(Class.java:350) at java.lang.Class.newInstance(Class.java:303) at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1055) at org.apache.catalina.core.StandardWrapper.allocate(StandardWrapper.java:757) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:130) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:178) at org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(SecurityAssociationValve.java:175) at org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValve.java:74) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:126) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:105) at org.jboss.web.tomcat.tc5.jca.CachedConnectionValve.invoke(CachedConnectionValve.java:156) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:107) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:148) at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:869) at org.apache.coyote.http11.Http11BaseProtocol$Http11ConnectionHandler.processConnection(Http11BaseProtocol.java:664) at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:527) at org.apache.tomcat.util.net.MasterSlaveWorkerThread.run(MasterSlaveWorkerThread.java:112) at java.lang.Thread.run(Thread.java:595)

我的hibernate.cfg.xml是

<?xml version="1.0" encoding="windows-1253" ?>

<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory>
        <!--<property name="hibernate.connection.datasource">
             jdbc/DBConnection3DS
        </property>
        <property name="show_sql">
             false
        </property>
        <property name="query.factory_class">
         org.hibernate.hql.classic.ClassicQueryTranslatorFactory
        </property>-->

        <property name="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</property>
        <property name="hibernate.connection.driver_class">org.postgresql.Driver</property>
        <property name="hibernate.connection.username">postgres</property>
        <property name="hibernate.connection.password">seaco712</property>
        <property name="hibernate.connection.url">jdbc:postgresql://localhost:5432/dvdrentals</property>



        <property name="connection_pool_size">1</property>

        <property name="hbm2ddl.auto">create</property>

        <property name="show_sql">true</property>





        <mapping resource="Employee.hbm.xml"/>
    </session-factory>
</hibernate-configuration>

并且web.xml是

<?xml version = '1.0' encoding = 'windows-1253'?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"> <description>Empty web.xml file for Web Application</description> <servlet> <servlet-name>Servlet1</servlet-name> <servlet-class>testingmodel2.Servlet1</servlet-class> </servlet> <servlet> <servlet-name>Servlet2</servlet-name> <servlet-class>testingmodel2.Servlet2</servlet-class> </servlet> <servlet> <servlet-name>action</servlet-name> <servlet-class>org.apache.struts.action.ActionServlet</servlet-class> <init-param> <param-name>config</param-name> <param-value>/WEB-INF/struts-config.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet> <servlet-name>Example1Servlet</servlet-name> <servlet-class>testingmodel2.Example1Servlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>Servlet1</servlet-name> <url-pattern>/servlet1</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>Servlet2</servlet-name> <url-pattern>/servlet2</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>action</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>Example1Servlet</servlet-name> <url-pattern>/example1servlet</url-pattern> </servlet-mapping> <session-config> <session-timeout>35</session-timeout> </session-config> <mime-mapping> <extension>html</extension> <mime-type>text/html</mime-type> </mime-mapping> <mime-mapping> <extension>txt</extension> <mime-type>text/plain</mime-type> </mime-mapping> <!--<resource-ref> <res-ref-name>jdbc/DBConnection3DS</res-ref-name> <res-type>javax.sql.DataSource</res-type> <res-auth>Container</res-auth> </resource-ref>--> </web-app>

,servlet代码是

    package testingmodel2;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.*;
import javax.servlet.http.*;

import java.util.Iterator;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

public class Example1Servlet extends HttpServlet {
    SessionFactory sessionFactory =
         new Configuration().configure("hibernate.cfg.xml").buildSessionFactory();

    public void init(ServletConfig config) throws ServletException {
        super.init(config);
    }

    protected void processRequest(HttpServletRequest request,
                                  HttpServletResponse response)
      throws ServletException, IOException {
        Session session = sessionFactory.openSession();

        Transaction tx = session.beginTransaction();
        Query query = session.createQuery("from Employee");
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        out.println("");
        out.println("");       
        out.println("");       
        out.println("");       
        out.println("");       
        out.println("");       
        out.println("<table>");
        out.println("<tr><th>ID</th><th>First Name</th>" +
                    "<TH>Last Name</th></tr>");
        for (Iterator it = query.iterate(); it.hasNext();) {
            Employee e = (Employee)it.next();
            out.println("<tr><td>" + e.getEmployeeID() +
                        "</td><td>" + e.getFirstName() +
                        "</td><td>" + e.getLastName() +
                        "</td></tr>");
        }
        tx.commit();
        session.close();

        out.println("</table>");
        out.close();
    }

    public void doGet(HttpServletRequest request,
                      HttpServletResponse response)
      throws ServletException, IOException {
        processRequest(request, response);
    }

    public void doPost(HttpServletRequest request,
                       HttpServletResponse response)
      throws ServletException, IOException {
        processRequest(request, response);
    }
}

请多多帮助!

1 个答案:

答案 0 :(得分:0)

您的错误消息说明了一切:它无法在您指定的位置找到hibernate.cfg.xml。尝试将其设置为jar / war / ear-file中的正确路径。