org.hibernate.InvalidMappingException:无法从资源User.hbm.xml解析映射文档

时间:2016-12-21 07:44:45

标签: java xml hibernate maven

作为输出我收到错误:

  

org.hibernate.InvalidMappingException:无法从资源User.hbm.xml解析映射文档。

我尝试使用链接解决它:

Could not parse mapping document from resource Books.hbm.xml

Hibernate and maven: Could not parse mapping document from resource

我正在尝试使用pojo类

在数据库中输入值

User.hbm.xml

               <hibernate-mapping>
    <class name="com.program.User" table="user">
        <id name="ID" type="int" column="id">
            <generator class="increment" />
        </id>
        <property name="NAME" type="java.lang.String" column="name">
            <column name="name" />
        </property>
        <property name="AGE" type="int" column="age">
            <column name="age" />
        </property>
    </class>
</hibernate-mapping>

Program.java

            package com.program;

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

            public class Program {

                public static void main(String[] args) {
                    try{
                        System.out.println("hello world");
                        SessionFactory factory = new Configuration().configure("hibernate.cfg.xml").buildSessionFactory();;
                        Session session = factory.openSession();
                        session.beginTransaction();
                        User user = new User();
                        user.setID(1);
                        user.setNAME("Toral");
                        user.setAGE(21);
                        session.save(user);
                        session.getTransaction().commit();
                        session.close();
                    }catch(Exception e){
                        System.out.println(e);
                    }
                }

            }

的hibernate.cfg.xml

        <?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">
        <hibernate-configuration>
            <session-factory>
                <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
                <property name="hibernate.connection.username">root</property>
                <property name="hibernate.connection.password">root</property>
                <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/student</property>
                <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
                <property name="show_sql">true</property>
                <property name="format_sql">true</property>
                <property name="hbm2ddl.auto">update </property>
                <mapping resource="User.hbm.xml" />
            </session-factory>
        </hibernate-configuration>

1 个答案:

答案 0 :(得分:0)

我认为你错放了&#34; name&#34;的实际价值。属性&#34;列&#34;属性。

&#34;名称&#34; attribute表示POJO类的属性。

&#34;柱&#34; attribute表示表的列名。

试试这个:

<hibernate-mapping>
<class name="com.program.User" table="user">
    <id name="id" type="int" column="ID">
        <generator class="increment" />
    </id>
    <property name="name" type="java.lang.String" column="NAME"></property>
    <property name="age" type="int" column="AGE"></property>
</class>