Hibernate无法从资源解析映射文档

Hibernate无法从资源解析映射文档,hibernate,Hibernate,我知道这已经被问了十几次了。我今天一定是疯了,因为我已经尝试了一百种方法修改我的xml文件,但我想不出来 运行驱动程序时出现错误堆栈: 16 [main] INFO org.hibernate.cfg.Environment - Hibernate 3.3.2.GA 31 [main] INFO org.hibernate.cfg.Environment - hibernate.properties not found 31 [main] INFO org.hibernate.cfg.Envir

我知道这已经被问了十几次了。我今天一定是疯了,因为我已经尝试了一百种方法修改我的xml文件,但我想不出来

运行驱动程序时出现错误堆栈:

16 [main] INFO org.hibernate.cfg.Environment - Hibernate 3.3.2.GA
31 [main] INFO org.hibernate.cfg.Environment - hibernate.properties not found
31 [main] INFO org.hibernate.cfg.Environment - Bytecode provider name : javassist
31 [main] INFO org.hibernate.cfg.Environment - using JDK 1.4 java.sql.Timestamp handling
78 [main] INFO org.hibernate.cfg.Configuration - configuring from resource: test_hibernate/hibernate.cfg.xml
78 [main] INFO org.hibernate.cfg.Configuration - Configuration resource: test_hibernate/hibernate.cfg.xml
125 [main] INFO org.hibernate.cfg.Configuration - Reading mappings from resource : test_hibernate/contact.hbm.xml
Exception in thread "main" Could not parse mapping document from resource            test_hibernate/contact.hbm.xml
java.lang.NullPointerException
at test_hibernate.MyExample.main(MyExample.java:29)
下面是我如何构建表的MySQL代码:

create database HibernateTest;
drop table contact;
Create table CONTACT(
ID int(15),
FIRSTNAME varchar(50) ,
LASTNAME varchar(50),
EMAIL varchar(150)); 
describe contact;
这是我的映射文件:

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>
<class name="test_hibernate.Contact" table="CONTACT">
<id name="id" column="ID" >
<generator class="increment" />
</id>
<property name="firstName" column="FIRSTNAME" />
<property name="lastName" column="LASTNAME" />
<property name="email" column="EMAIL" />
</class>
</hibernate-mapping>
这是我的司机:

package test_hibernate;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.Transaction;

public class MyExample {

public static void main(String[] args) {
Session session = null;

try{
    // This step will read hibernate.cfg.xml and prepare hibernate for use
    SessionFactory sessionFactory = new Configuration().configure("hibernate.cfg.xml").buildSessionFactory();
    session=sessionFactory.openSession();
    Transaction tx = session.beginTransaction();

    //Create new instance of Contact and set values in it by reading them from form object
    System.out.println("Inserting Record");

    Contact contact = new Contact();
    contact.setId(3);
    contact.setFirstName("Smitha");
    contact.setLastName("Rao");
    contact.setEmail("smithaxxx@yahoo.com");
    session.save(contact);
    tx.commit();
    System.out.println("Done");
}catch(Exception e){
    System.out.println(e.getMessage());
}finally{
    // Actual contact insertion will happen at this step

    session.flush();        
    session.close();
}
}
}

contact.hbm.xml和contact POJO类是否存在于同一路径中

如果没有,请尝试提供包属性,如

<hibernate-mapping package="package name of Contact POJO class">

contact.hbm.xml和contact POJO类是否存在于同一路径中

如果没有,请尝试提供包属性,如

<hibernate-mapping package="package name of Contact POJO class">


您的映射文件看起来很好。我认为这可能是因为您使用了而不是来表示双引号。请尝试使用来表示所有双引号。

您的映射文件看起来不错。我认为这可能是因为您使用了而不是用于双引号。请尝试使用"查看所有双引号。

将MySQL连接的端口号设置为jdbc:mysql://localhost:3306/hibernatetest在配置文件中。

将MySQL连接的端口号设置为jdbc:mysql://localhost:3306/hibernatetest在您的配置文件中。

您的url应该是
jdbc:mysql://localhost:3306/hibernatetest

您的url应该是
jdbc:mysql://localhost:3306/hibernatetest

请从映射文件中删除
。这将解决您的问题

请从映射文件中删除
。这将解决您的问题

我没有为您设置包名他的测试。可能是hibernate工作所必需的?也是的,文件在同一路径中包括用于构建表格的sql脚本。我没有为此测试设置程序包名称。可能是hibernate工作所必需的?也是的,文件在同一路径中包括用于构建表格的sql脚本。哦,不,真的。让我试试。谢谢Ken。之前我在SQL代码中遇到过“vs MS Word”,如果这有任何意义的话。Will回过头来看看是的,你搞定了。更新了我问题中的上述代码。对于那些偶然发现这篇文章并需要另一个示例来尝试的人来说,Hibernate 3.3和MySQL的一切都应该是有效的。我知道在我有了som之前,我经历了很多次尝试这很接近。谢谢你,肯!哦,不,真的。让我试试。谢谢肯。如果之前SQL代码中出现过“vs MS Word”的话,如果有任何意义的话。我会回过头来看看的。是的,你搞定了。更新了我问题中的上述代码。对于那些无意中发现这篇文章并需要另一个示例的人来说,Hibernate 3.3和MySQL的一切都应该是有效的我想试试。我知道我花了很多时间才有了更接近的东西。谢谢你,肯!
<hibernate-mapping package="package name of Contact POJO class">