1

こんにちは、休止状態のフレームワークを初めて使用します。休止状態のサンプル コードを実行しているときに、インターネット接続が利用できる場合は正常に動作します。インターネット接続が利用できない場合、動作せず、次のようなエラーが発生します。

log4j:WARN No appenders could be found for logger (org.hibernate.cfg.Environment).
log4j:WARN Please initialize the log4j system properly.
Failed to create sessionFactory object.org.hibernate.HibernateException: Could not parse configuration: /hibernate.cfg.xml
Exception in thread "main" java.lang.ExceptionInInitializerError
    at com.hb.example.ManageEmployee.main(ManageEmployee.java:20)
Caused by: org.hibernate.HibernateException: Could not parse configuration: /hibernate.cfg.xml
    at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1491)
    at org.hibernate.cfg.Configuration.configure(Configuration.java:1425)
    at org.hibernate.cfg.Configuration.configure(Configuration.java:1411)
    at com.hb.example.ManageEmployee.main(ManageEmployee.java:17)
Caused by: org.dom4j.DocumentException: www.hibernate.org Nested exception: www.hibernate.org
    at org.dom4j.io.SAXReader.read(SAXReader.java:484)
    at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1481)
    ... 3 more

私のコード例は以下のようになります: Employee.java:

package com.hb.example;

public class Employee {
       private int id;
       private String firstName; 
       private String lastName;   
       private int 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;
       }
    }

ManageEmployee.java:

package com.hb.example;

import java.util.List; 
import java.util.Date;
import java.util.Iterator; 

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

public class ManageEmployee {
   private static SessionFactory factory; 
   public static void main(String[] args) {
      try{
         factory = new Configuration().configure().buildSessionFactory();
      }catch (Throwable ex) { 
         System.err.println("Failed to create sessionFactory object." + ex);
         throw new ExceptionInInitializerError(ex); 
      }
      ManageEmployee ME = new ManageEmployee();

      /* Add few employee records in database */
      Integer empID1 = ME.addEmployee("Zara", "Ali", 1000);
      Integer empID2 = ME.addEmployee("Daisy", "Das", 5000);
      Integer empID3 = ME.addEmployee("John", "Paul", 10000);

      /* List down all the employees */
      ME.listEmployees();

      /* Update employee's records */
      ME.updateEmployee(empID1, 5000);

      /* Delete an employee from the database */
      ME.deleteEmployee(empID2);

      /* List down new list of the employees */
      ME.listEmployees();
   }
   /* Method to CREATE an employee in the database */
   public Integer addEmployee(String fname, String lname, int salary){
      Session session = factory.openSession();
      Transaction tx = null;
      Integer employeeID = null;
      try{
         tx = session.beginTransaction();
         Employee employee = new Employee(fname, lname, salary);
         employeeID = (Integer) session.save(employee); 
         tx.commit();
      }catch (HibernateException e) {
         if (tx!=null) tx.rollback();
         e.printStackTrace(); 
      }finally {
         session.close(); 
      }
      return employeeID;
   }
   /* Method to  READ all the employees */
   public void listEmployees( ){
      Session session = factory.openSession();
      Transaction tx = null;
      try{
         tx = session.beginTransaction();
         List employees = session.createQuery("FROM Employee").list(); 
         for (Iterator iterator = 
                           employees.iterator(); iterator.hasNext();){
            Employee employee = (Employee) iterator.next(); 
            System.out.print("First Name: " + employee.getFirstName()); 
            System.out.print("  Last Name: " + employee.getLastName()); 
            System.out.println("  Salary: " + employee.getSalary()); 
         }
         tx.commit();
      }catch (HibernateException e) {
         if (tx!=null) tx.rollback();
         e.printStackTrace(); 
      }finally {
         session.close(); 
      }
   }
   /* Method to UPDATE salary for an employee */
   public void updateEmployee(Integer EmployeeID, int salary ){
      Session session = factory.openSession();
      Transaction tx = null;
      try{
         tx = session.beginTransaction();
         Employee employee = 
                    (Employee)session.get(Employee.class, EmployeeID); 
         employee.setSalary( salary );
         session.update(employee); 
         tx.commit();
      }catch (HibernateException e) {
         if (tx!=null) tx.rollback();
         e.printStackTrace(); 
      }finally {
         session.close(); 
      }
   }
   /* Method to DELETE an employee from the records */
   public void deleteEmployee(Integer EmployeeID){
      Session session = factory.openSession();
      Transaction tx = null;
      try{
         tx = session.beginTransaction();
         Employee employee = 
                   (Employee)session.get(Employee.class, EmployeeID); 
         session.delete(employee); 
         tx.commit();
      }catch (HibernateException e) {
         if (tx!=null) tx.rollback();
         e.printStackTrace(); 
      }finally {
         session.close(); 
      }
   }
}

hibernate.cfg.xml:

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

<----->

<-- --->

<hibernate-configuration>
   <session-factory>
   <property name="hbm2ddl.auto">update</property>
   <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/test
   </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>

従業員.hbm.xml:

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

ここで私は変更を行いましたが、機能していません

<--- --->

<hibernate-mapping>
   <class name="com.hb.example.Employee" table="EMPLOYEE">
      <meta attribute="class-description">
         This class contains the employee detail. 
      </meta>
      <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>

誰でも助けてください。

4

3 に答える 3

1

休止状態の構成とマッピング ファイルの 3 行目と 4 行目をそれぞれ参照してください。

DTD のドキュメントには次のように書かれています。

Hibernate は Web から DTD ファイルをロードしませんが、最初にアプリケーションのクラスパスから検索します。DTD ファイルは hibernate-core.jar に含まれています (配布バンドルを使用している場合は、hibernate3.jar にも含まれています)。

ほとんどの場合、アプリケーションでインターネットから休止状態の構成ファイルとマッピング ファイルの両方の DTD ファイルをロードしようとするため、インターネットがない場合にこの例外が発生します。

アプリケーションが開始され、最初に hibernate 構成ファイルにアクセスすると、 www.hibernate.orgからダウンロードされた DTD ファイルを使用して構成ファイルを解析しようとします。

Hiberate DTD の詳細については、以下をご覧ください。

http://forum.spring.io/forum/spring-projects/data/73208-how-to-configure-hibernate-cfg-xml-to-work-offline

http://docs.jboss.org/hibernate/orm/3.3/reference/en/html/tutorial.html#tutorial-firstapp-mapping

アップデート :-

Hibernate Offline を使用するには?

Hibernate 構成ファイルの場合:

  1. DTD ファイルの宣言を次のように変更します。

    <!DOCTYPE hibernate-configuration SYSTEM "hibernate-configuration-3.0.dtd">

  2. http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtdから hibernate 構成 DTD ファイルをダウンロードし、クラスパスに設定します。

ハイバネート マッピング ファイルの場合:

  1. DTD ファイルの宣言を次のように変更します。

    <!DOCTYPE hibernate-configuration SYSTEM "hibernate-mapping-3.0.dtd">

  2. http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtdから hibernate 構成 DTD ファイルをダウンロードし、クラスパスに設定します。

これは正しく動作するはずです。

于 2013-11-11T12:29:35.440 に答える
0

今日、同様の質問が 1 つあり、回答しました。hbm ファイルにも DOCTYPE の不一致があるようです。一度確認してください。それはうまくいくはずです。

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

以下の doctype を使用します。

于 2013-11-11T09:48:41.743 に答える