hbm.xml文件出错

时间:2016-06-03 07:30:58

标签: hibernate

当我执行我的代码时,我收到错误

  

无法解析资源EmployeeBean.hbm.xml

中的映射文档

我已经多次检查过,我无法解决错误。请求我帮忙。我附上了我的代码供参考。

TestFile.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
<script src="js/jquery-1.10.2.js"></script>
<script src="js/jquery-ui.js"></script>
<script src="js/jquery.jqGrid.min.js"></script>
<script>
$(document).ready(function() {
    $("#btn").click(function(){ 
            $.ajax({
            type: "Post",
            url: "hibernateTest",
            success: function(resp)
            {           
            }
            });
    });
 });

</script>
</head>
<body>
    <input type="button" id="btn" />
</body>
</html>

的web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
 <servlet>
 <servlet-name>Controller</servlet-name>
 <servlet-class>org.hibernate.test.EmployeeController</servlet-class>
 </servlet>
 <servlet-mapping>
 <servlet-name>Controller</servlet-name>
 <url-pattern>/hibernateTest</url-pattern>
 </servlet-mapping>
</web-app>

EmployeeController.java

package org.hibernate.test;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
public class EmployeeController extends HttpServlet{
protected void doPost(HttpServletRequest request, HttpServletResponse       response) throws ServletException, IOException
{
    EmployeeDAO employeeDAO = new EmployeeDAO();
    employeeDAO.addUserDetails("test");
}

}

EmployeeBean.hbm.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="org.hibernate.test.EmployeeBean" table="EmployeeMaster">
    <id column="EmpId" name="id" >
        <generator class="increment"/>
    </id>
    <property column="EmployeeName" name="EmployeeName" type="java.lang.String" />
    <property column="Age" name="Age" type="java.lang.Short" />
    <property column="Sex" name="sex" type="java.lang.String" />
    <property column="Address" name="address" type="java.lang.String" />
    <property column="City" name="city" type="java.lang.String" />
</class>
</hibernate-mapping>

的hibernate.cfg.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC 
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
<session-factory>
    <property name="hibernate.connection.driver_class">com.microsoft.sqlserver.jdbc.SQLServerDriver</property>
    <property name="hibernate.connection.url">jdbc:sqlserver://localhost:1433;databaseName=JAVADB</property>
    <property name="hibernate.connection.username">sa</property>
    <property name="hibernate.connection.password">dbadmin</property>
    <property name="hibernate.dialect">org.hibernate.dialect.SQLServer2008Dialect</property>
    <property name="show_sql">true</property>
    <property name="format_sql">true</property>
    <property name="hbm2ddl.auto">create </property>
    <mapping resource="EmployeeBean.hbm.xml" />
</session-factory>
</hibernate-configuration>

EmployeeBean.java

package org.hibernate.test;

public class EmployeeBean {

private String EmployeeName;
private short Age;
private String sex;
private String address;
private String city;
public String getEmployeeName() {
    return EmployeeName;
}
public void setEmployeeName(String employeeName) {
    EmployeeName = employeeName;
}
public short getAge() {
    return Age;
}
public void setAge(short age) {
    Age = age;
}
public String getSex() {
    return sex;
}
public void setSex(String sex) {
    this.sex = sex;
}
public String getAddress() {
    return address;
}
public void setAddress(String address) {
    this.address = address;
}
public String getCity() {
    return city;
}
public void setCity(String city) {
    this.city = city;
}

}

EmployeeDAO.java

package org.hibernate.test;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

public class EmployeeDAO {
public void addUserDetails(String employeeName) {
    try {

        System.out.println("test 1");
        Configuration configuration = new Configuration().configure();
        System.out.println("test 1.5");
        SessionFactory sessionFactory = configuration.buildSessionFactory();
        System.out.println("test 2");
        Session session = sessionFactory.openSession();
        Transaction transaction = session.beginTransaction();
        System.out.println("test 3");
        EmployeeBean user = new EmployeeBean();
        user.setEmployeeName(employeeName);
        System.out.println("test 4");
        session.save(user);
        transaction.commit();
        System.out.println("\n\n Details Added \n");

    } catch (HibernateException e) {
        System.out.println(e.getMessage());
        System.out.println("error");
    }

}
}

堆栈跟踪:

Jun 03, 2016 4:08:29 PM org.apache.catalina.core.AprLifecycleListener init
INFO: The APR based Apache Tomcat Native library which allows optimal         performance in production environments was not found on the java.library.path: C:\Program Files   (x86)\Java\jre7\bin;C:\Windows\Sun\Java\bin;C:\Windows\system32;C:\Windows;C:/Pr ogram Files (x86)/Java/jre7/bin/client;C:/Program Files  (x86)/Java/jre7/bin;C:/Program Files  (x86)/Java/jre7/lib/i386;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem ;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Program Files (x86)\Microsoft   SQL Server\100\Tools\Binn\;C:\Program Files (x86)\Microsoft SQL  Server\100\DTS\Binn\;C:\Program Files (x86)\Microsoft SQL  Server\100\Tools\Binn\VSShell\Common7\IDE\;D:\Ashwin\Java\Eclipse -  Juno\eclipse;;.
Jun 03, 2016 4:08:30 PM org.apache.tomcat.util.digester.SetPropertiesRule  begin
WARNING: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting   property 'source' to 'org.eclipse.jst.jee.server:HibernateProject1' did not find  a matching property.
Jun 03, 2016 4:08:30 PM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["http-bio-8080"]
Jun 03, 2016 4:08:30 PM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["ajp-bio-8009"]
Jun 03, 2016 4:08:30 PM org.apache.catalina.startup.Catalina load
INFO: Initialization processed in 791 ms
Jun 03, 2016 4:08:30 PM org.apache.catalina.core.StandardService   startInternal
INFO: Starting service Catalina
Jun 03, 2016 4:08:30 PM org.apache.catalina.core.StandardEngine     startInternal
INFO: Starting Servlet Engine: Apache Tomcat/7.0.42
Jun 03, 2016 4:08:30 PM org.apache.catalina.loader.WebappClassLoader     validateJarFile
INFO: validateJarFile(D:\Ashwin\Java\Workspace_22_06_2015\.metadata\.plugins\org.eclip    se.wst.server.core\tmp1\wtpwebapps\HibernateProject1\WEB-  INF\lib\javax.servlet.jar) - jar not loaded. See Servlet Spec 2.3, section   9.7.2. Offending class: javax/servlet/Servlet.class
Jun 03, 2016 4:08:31 PM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["http-bio-8080"]
Jun 03, 2016 4:08:31 PM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["ajp-bio-8009"]
Jun 03, 2016 4:08:31 PM org.apache.catalina.startup.Catalina start
INFO: Server startup in 861 ms
test 1
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further  details.
Jun 03, 2016 4:08:35 PM org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {4.2.4.Final}
Jun 03, 2016 4:08:35 PM org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
Jun 03, 2016 4:08:35 PM org.hibernate.cfg.Environment buildBytecodeProvider
INFO: HHH000021: Bytecode provider name : javassist
Jun 03, 2016 4:08:35 PM org.hibernate.cfg.Configuration configure
INFO: HHH000043: Configuring from resource: /hibernate.cfg.xml
Jun 03, 2016 4:08:35 PM org.hibernate.cfg.Configuration  getConfigurationInputStream
INFO: HHH000040: Configuration resource: /hibernate.cfg.xml
Jun 03, 2016 4:08:35 PM org.hibernate.cfg.Configuration addResource
INFO: HHH000221: Reading mappings from resource: EmployeeBean.hbm.xml
Jun 03, 2016 4:08:35 PM org.hibernate.internal.util.xml.DTDEntityResolver  resolveEntity
WARN: HHH000223: Recognized obsolete hibernate namespace   http://hibernate.sourceforge.net/. Use namespace http://www.hibernate.org/dtd/   instead. Refer to Hibernate 3.6 Migration Guide!
Jun 03, 2016 4:08:35 PM org.hibernate.cfg.Configuration doConfigure
INFO: HHH000041: Configured SessionFactory: null
test 1.5
Jun 03, 2016 4:08:35 PM  org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderI    mpl configure
INFO: HHH000402: Using Hibernate built-in connection pool (not for   production use!)
Jun 03, 2016 4:08:35 PM org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
INFO: HHH000115: Hibernate connection pool size: 20
Jun 03, 2016 4:08:35 PM   org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderI    mpl configure
INFO: HHH000006: Autocommit mode: false
Jun 03, 2016 4:08:35 PM  org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderI    mpl configure
INFO: HHH000401: using driver [com.microsoft.sqlserver.jdbc.SQLServerDriver]   at URL [jdbc:sqlserver://localhost:1433;databaseName=JAVADB]
Jun 03, 2016 4:08:35 PM  org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderI    mpl configure
INFO: HHH000046: Connection properties: {user=sa, password=****}
Jun 03, 2016 4:08:36 PM org.hibernate.dialect.Dialect <init>
INFO: HHH000400: Using dialect: org.hibernate.dialect.SQLServer2008Dialect
Jun 03, 2016 4:08:36 PM org.hibernate.engine.jdbc.internal.LobCreatorBuilder     useContextualLobCreation
INFO: HHH000423: Disabling contextual LOB creation as JDBC driver reported   JDBC version [3] less than 4
Could not parse mapping document from resource EmployeeBean.hbm.xml
error

1 个答案:

答案 0 :(得分:0)

这里你忘了把type =&#34; int&#34;在id列中,这就是它无法正常工作的原因。它应该如下所示。

<id column="EmpId" name="id" type="int">
    <generator class="increment"/>
</id>

如果需要,也可以给它外键。

   <id name="EmpId" type="java.lang.Long">
        <column name="id" />
        <generator class="foreign">
            <param name="property">employee</param>
        </generator>
    </id>