2

私は新しい休止状態です以下のコードを実行しようとすると、以下の例外が発生します:

org.hibernate.InvalidMappingException: リソース Employee.hbm.xml からマッピング ドキュメントを解析できませんでした

これが私のコードです:

構成:

    <hibernate-configuration> 
    <session-factory> 
    <property name="hibernate.dialect"> 
    org.hibernate.dialect.MySQLDialect 
    </property> 
    <property name="hibernate.connection.driver_class"> 
    com.mysql.jdbc.Driver 
    </property> 

    <property name="hibernate.connection.url"> 
    jdbc:mysql://localhost/hibernatetest 
    </property> 
    <property name="hibernate.connection.username"> 
    root 
    </property> 
    <property name="hibernate.connection.password"> 
    root 
    </property> 

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

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

ソース:

    package com.practice.HBtest; 

public class Employee {

    private int id;
    private String firstName;
    private String lastName;
    private Integer salary;

    public Employee() {}

    public Employee(String fname, String lname, int salary) {
        this.firstName = fname;
        this.lastName = lname;
        this.salary = salary;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String first_name) {
        this.firstName = first_name;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String last_name) {
        this.lastName = last_name;
    }

    public int getSalary() {
        return salary;
    }

    public void setSalary(int salary) {
        this.salary = salary;
    }
}

マッピング:

<hibernate-mapping> 
<class name="Employee" table="EMPLOYEE"> 
<id name="id" type="int" column="id"> 
<generator class="native"/> 
</id> 

<property name="firstname" column="first_name" type="String"/> 
<property name="lastname" column="last_name" type="String"/> 
<property name="salary" column="salary" type="int"/>    
</class> 
</hibernate-mapping> 

ソースの抜粋:

import org.hibernate.HibernateException;
import org.hibernate.SessionFactory;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;

public class EmployeeRecordManager {

    private static SessionFactory sessionFactory;
    private static ServiceRegistry serviceRegistry;
    private Session session;

    public static void main(String[] args) {

        EmployeeRecordManager erm = new EmployeeRecordManager();

        try {
            Configuration cfg = new Configuration().addResource("Employee.hbm.xml");
            SessionFactory sessionFactory = cfg.configure().buildSessionFactory(serviceRegistry);

        } catch (Exception e) {
            System.out.println(e);
        }
        Integer empID1 = erm.addEmployee("Amruta", "Ali", 1000);
        Integer empID2 = erm.addEmployee("Ruchi", "Das", 5000);
        Integer empID3 = erm.addEmployee("Mitul", "Paul", 10000);

        System.out.println(erm);

    }

    public Integer addEmployee(String fname, String lname, int salary) {
        Session session = sessionFactory.openSession();
        Transaction tx = null;
        Integer empID = null;
        try {
            tx = session.beginTransaction();
            Employee employee = new Employee(fname, lname, salary);
            empID = (Integer) session.save(employee);
            tx.commit();
        } catch (HibernateException e) {
            if (tx != null)
                tx.rollback();
            e.printStackTrace();
        } finally {
            session.close();
        }
        return empID;
    }

}







   Nov 7, 2012 5:36:15 PM org.hibernate.annotations.common.Version <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {4.0.1.Final}
Nov 7, 2012 5:36:15 PM org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {4.1.8.Final}
Nov 7, 2012 5:36:15 PM org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
Nov 7, 2012 5:36:15 PM org.hibernate.cfg.Environment buildBytecodeProvider
INFO: HHH000021: Bytecode provider name : javassist
Nov 7, 2012 5:36:15 PM org.hibernate.cfg.Configuration addResource
INFO: HHH000221: Reading mappings from resource: Employee.hbm.xml
Nov 7, 2012 5:36:15 PM org.hibernate.cfg.Configuration configure
INFO: HHH000043: Configuring from resource: /hibernate.cfg.xml
Nov 7, 2012 5:36:15 PM org.hibernate.cfg.Configuration getConfigurationInputStream
INFO: HHH000040: Configuration resource: /hibernate.cfg.xml
Nov 7, 2012 5:36:15 PM org.hibernate.cfg.Configuration addResource
INFO: HHH000221: Reading mappings from resource: Employee.hbm.xml
Nov 7, 2012 5:36:15 PM org.hibernate.cfg.Configuration doConfigure
INFO: HHH000041: Configured SessionFactory: null
Nov 7, 2012 5:36:15 PM org.hibernate.cfg.Configuration$MappingsImpl addImport
INFO: HHH000071: Duplicate import: Employee -> Employee
org.hibernate.InvalidMappingException: Could not parse mapping document from resource Employee.hbm.xml
    at org.hibernate.cfg.Configuration$MetadataSourceQueue.processHbmXml(Configuration.java:3417)
    at org.hibernate.cfg.Configuration$MetadataSourceQueue.processHbmXmlQueue(Configuration.java:3406)
    at org.hibernate.cfg.Configuration$MetadataSourceQueue.processMetadata(Configuration.java:3394)
    at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1341)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1737)
    at com.practice.HBtest.EmployeeRecordManager.main(EmployeeRecordManager.java:24)
Caused by: org.hibernate.DuplicateMappingException: Duplicate class/entity mapping Employee
    at org.hibernate.cfg.Configuration$MappingsImpl.addClass(Configuration.java:2582)
    at org.hibernate.cfg.HbmBinder.bindRoot(HbmBinder.java:174)
    at org.hibernate.cfg.Configuration$MetadataSourceQueue.processHbmXml(Configuration.java:3414)
    ... 5 more
Exception in thread "main" java.lang.NullPointerException
    at com.practice.HBtest.EmployeeRecordManager.addEmployee(EmployeeRecordManager.java:39)
    at com.practice.HBtest.EmployeeRecordManager.main(EmployeeRecordManager.java:30)
org.hibernate.InvalidMappingException: Could not parse mapping document from resource Employee.hbm.xml
After Adding the code mentioned by you was getting below Exception:


  Nov 7, 2012 6:48:24 PM org.hibernate.annotations.common.Version <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {4.0.1.Final}
Nov 7, 2012 6:48:24 PM org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {4.1.8.Final}
Nov 7, 2012 6:48:24 PM org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
Nov 7, 2012 6:48:24 PM org.hibernate.cfg.Environment buildBytecodeProvider
INFO: HHH000021: Bytecode provider name : javassist
Nov 7, 2012 6:48:24 PM org.hibernate.cfg.Configuration configure
INFO: HHH000043: Configuring from resource: hibernate.cfg.xml
Nov 7, 2012 6:48:24 PM org.hibernate.cfg.Configuration getConfigurationInputStream
INFO: HHH000040: Configuration resource: hibernate.cfg.xml
Nov 7, 2012 6:48:24 PM org.hibernate.cfg.Configuration addResource
INFO: HHH000221: Reading mappings from resource: Employee.hbm.xml
Nov 7, 2012 6:48:24 PM org.hibernate.cfg.Configuration doConfigure
INFO: HHH000041: Configured SessionFactory: null
Nov 7, 2012 6:48:24 PM org.hibernate.cfg.Configuration configure
INFO: HHH000043: Configuring from resource: /hibernate.cfg.xml
Nov 7, 2012 6:48:24 PM org.hibernate.cfg.Configuration getConfigurationInputStream
INFO: HHH000040: Configuration resource: /hibernate.cfg.xml
Nov 7, 2012 6:48:24 PM org.hibernate.cfg.Configuration addResource
INFO: HHH000221: Reading mappings from resource: Employee.hbm.xml
Nov 7, 2012 6:48:24 PM org.hibernate.cfg.Configuration doConfigure
INFO: HHH000041: Configured SessionFactory: null
Nov 7, 2012 6:48:24 PM org.hibernate.cfg.Configuration$MappingsImpl addImport
INFO: HHH000071: Duplicate import: Employee -> Employee
org.hibernate.InvalidMappingException: Could not parse mapping document from resource Employee.hbm.xml
    at org.hibernate.cfg.Configuration$MetadataSourceQueue.processHbmXml(Configuration.java:3417)
    at org.hibernate.cfg.Configuration$MetadataSourceQueue.processHbmXmlQueue(Configuration.java:3406)
    at org.hibernate.cfg.Configuration$MetadataSourceQueue.processMetadata(Configuration.java:3394)
    at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1341)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1737)
    at com.practice.HBtest.EmployeeRecordManager.main(EmployeeRecordManager.java:24)
Caused by: org.hibernate.DuplicateMappingException: Duplicate class/entity mapping Employee
    at org.hibernate.cfg.Configuration$MappingsImpl.addClass(Configuration.java:2582)
    at org.hibernate.cfg.HbmBinder.bindRoot(HbmBinder.java:174)
    at org.hibernate.cfg.Configuration$MetadataSourceQueue.processHbmXml(Configuration.java:3414)
    ... 5 more
Exception in thread "main" java.lang.NullPointerException
    at com.practice.HBtest.EmployeeRecordManager.addEmployee(EmployeeRecordManager.java:39)
    at com.practice.HBtest.EmployeeRecordManager.main(EmployeeRecordManager.java:30)
org.hibernate.InvalidMappingException: Could not parse mapping document from resource Employee.hbm.xml
4

5 に答える 5

3

hbm.xml プロパティ名の間に大文字と小文字の不一致があります:

<property name="firstname" column="first_name" type="String"/> 
<property name="lastname" column="last_name" type="String"/> 
<property name="salary" column="salary" type="int"/> 

そして、クラスの実際のメンバー フィールド:

private String first**N**ame; 
private String last**N**ame; 
private Integer salary; 

getFirstname()さらに重要なのは、クラスのゲッターとセッターにもキャメルケースがあり、休止状態が, setFirstname(), ...を探していること です。

于 2012-11-07T10:33:53.427 に答える
1

混乱した XML の代わりにJPA アノテーションを使用する

于 2012-11-07T10:35:59.470 に答える
0

問題を修正できました。1. mysql コネクタ jar を追加しました。 2. そのための正しいポートを追加しました。 3. 以下の構成を使用しました。

Configuration configuration = new Configuration();
            configuration.configure();
            serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry();        
            sessionFactory = configuration.buildSessionFactory(serviceRegistry);

これまでに貢献してくれたすべての人に感謝します

于 2012-11-08T10:24:16.713 に答える
0

スタック トレース全体を貼り付けていただけますか? 休止状態の構成をロードする時点で多くの問題が発生する可能性があるため、スタック トレース全体を使用せずにこれをデバッグするのは少し困難です。

構成を定義して接続を構築する方法に関係している可能性があります。私が通常行うことは次のとおりです。

  • メインの hibernate 構成ファイルから構成をロードします。 configuration = new Configuration().configure("hibernate.cfg.xml");

  • ロードした構成からセッション ファクトリを構築する sessionFactory = configuration.buildSessionFactory();

hbm.xml リソースを明示的に追加する理由はありません (そのリソースはメイン構成ファイルで既に定義されているため、セッション ファクトリを構築するときにサービス レジストリ パラメーターを使用する必要はありません ...

また、ID マッピングでは、次のように DB 列を別のタグに配置する必要がある場合があります。

<id name="id" type="int">
            <column name="id" scale="0" />
            <generator class="native" />
</id>
于 2012-11-07T11:37:37.587 に答える