线程" main"中的例外情况org.hibernate.InvalidMappingException:无法解析资源userdata.hbm.xml中的映射文档

时间:2014-11-24 04:00:35

标签: java mysql xml hibernate

这是我的目录结构:

enter image description here

这些是错误:

enter image description here

我正在尝试我的第一个hibernate程序,我收到此错误。我的程序只需在数据库中输入用户名,密码和电子邮件。我创建了一个POJO user.java,一个UserManager.java和两个xml文件,一个用于配置,另一个用于hibernate映射。我已经尝试了这个错误很长时间但仍然没有解决方案。

这是我的user.java:

    package com.csc.training.Twitter;

    public class User {

        String username;
        String password;
        String email;
        int user_id;
        Date joined;

public Date getJoined() {
        return joined;
    }
    public void setJoined(Date joined) {
        this.joined = joined;
    }

        public int getUser_id() {
            return user_id;
        }
        public void setUser_id(int user_id) {
            this.user_id = user_id;
        }


        public String getUsername() {
            return username;
        }
        public void setUsername(String username) {
            this.username = username;
        }
        public String getPassword() {
            return password;
        }
        public void setPassword(String password) {
            this.password = password;
        }
        public String getEmail() {
            return email;
        }
        public void setEmail(String email) {
            this.email = email;
        }   
    }

UserManager.java

package com.csc.training.Twitter;

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

public class UserManager {

    public void registration() {

    Configuration config=new Configuration();
    config.configure("hibernate.cfg.xml");
    SessionFactory factory=config.buildSessionFactory();

    Session session=factory.openSession();

    Transaction transaction = (Transaction) session.beginTransaction();

    User user = new User(); 

    user.setUsername("fname2");
    user.setPassword("lname2");
    user.setEmail("def@def.def");

    session.persist(user);

    transaction.commit();

    System.out.println("User Id : " + user.getUser_id());

    session.close();
    System.out.println("Employee update successful...");

}

public static void main(String[] args) {
    UserManager um=new UserManager();
    um.registration();
}
}

这些是xml文件..

userdata.hbm.xml:

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

    <hibernate-mapping>
       <class name="com.csc.training.user" table="userdata">
          <id name="user_id">
             <generator class="increment"></generator>
          </id>


          <property name="username" column="username" type="string"/>
          <property name="password" column="password" type="string"/>
          <property name="email" column="email" type="string"/>
          <property name="joined" type="timestamp" column="joined"/>

       </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.dialect">
          org.hibernate.dialect.MySQLDialect
       </property>
       <property name="hibernate.connection.driver_class">
          com.mysql.jdbc.Driver
       </property>

       <!-- Assume test is the database name -->
       <property name="hibernate.connection.url">
        jdbc:mysql://localhost/twitter
        </property>
        <property name="hibernate.connection.username">
            root
       </property>
       <property name="hibernate.connection.password">
          root
       </property>

       <!-- List of XML mapping files -->
       <mapping resource="userdata.hbm.xml"/>

    </session-factory>
    </hibernate-configuration>

我的数据库是twitter,表名是userdata。 userdata包含user_id(int,主键,自动增量),join(时间戳),用户名,电子邮件,密码。 谁能告诉我错误是什么?非常感谢你!

2 个答案:

答案 0 :(得分:0)

使用以下代码

将商家加入作为日期类型,并使用以下标记配置 HBM 文件

<property name="joined" type="timestamp" column="joinedDate_time"/>

将HBM文件的doc-type更改为beloe

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

和CFG领域

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

答案 1 :(得分:0)

hibernate配置文件中的DTD声明不正确。

user.hbm.xml文件中应该是:

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

对于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">

执行这些更改后,请确保在运行UserManager程序时它们位于类路径中。