【问题标题】:How to solve this hibernate runtime exception?如何解决这个休眠运行时异常?
【发布时间】:2012-11-11 11:00:32
【问题描述】:

我正在尝试在 eclipse 中运行以下代码

package com.trial;


import java.sql.*;
import java.io.*;
import org.hibernate.*;
import org.hibernate.cfg.*;

public class AddStudent {

     private static SessionFactory sessionFactory;

     public static void main(String args[]) throws Exception {

    DataInputStream d = new DataInputStream(System.in);
    System.out.println("ENTER YOUR NAME");
    String name = d.readLine();
    System.out.println("ENTER YOUR DEGREE");
    String degree = d.readLine();
    System.out.println("ENTER YOUR PHONE");
    int phone = Integer.parseInt(d.readLine());
    System.out.println("Name: " + name);
    System.out.println("Degree: " + degree);
    System.out.println("Phone: " + phone);
    if ((name.equals("") || degree.equals(""))) {
        System.out.println("Information Required");
    }
    else {

        try {
            sessionFactory = new Configuration().configure("com//xml//hibernate.cfg.xml").buildSessionFactory();
        }
        catch (Exception e) {
             System.out.println(e.getMessage());
        }

        Session s = sessionFactory.openSession();
        Student stu = new Student();
        stu.setName(name);
        stu.setDegree(degree);
        stu.setPhone(phone);
        s.save(stu);
        System.out.println("Added to Database");
        if (s != null)
            s.close();
    }
}
}

但在创建 无法读取 XML 的会话工厂对象期间出现运行时异常。

我正在使用以下 xml 文件

  1. hibernate.cfg.xml

     <!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 name="studentFactory">
    
    <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
    <property name="connection.url">jdbc:mysql://localhost:3306/ganeshdb</property>
    <property name="connection.username">****</property>
    <property name="connection.password">****</property>
    <property name="connection.pool_size">10</property>
     <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
     <property name="show_sql">true</property>
    <property name="hbm2ddl.auto">create</property>
    <mapping resource="com//xml//student.hbm.xml" />
    
     </session-factory>
    
    </hibernate-configuration>
    
  2. 映射文件

      <?xml version="1.0"? encoding='UTF-8'?>
      <!DOCTYPE hibernate-mapping PUBLIC
     "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
     "http://www.hibernate.org/hibernate-configuration-3.0.dtd">
    
      <hibernate-mapping>
    
        <class name="com.trial.Student" table="studentdemo">
      <id name="id" type="int" column="ID">
                    <generator class="increment" />
      </id>
       <property name="name" column="name" />
       <property name="degree" column="degree" />
       <property name="phone" column="phone" />
    </class>
    
        </hibernate-mapping>
    

请帮忙。

【问题讨论】:

  • 发布更多异常消息。但猜测一下,xml 文件并不是你告诉程序它应该在的地方。
  • 运行时异常:org.hibernate.InvalidMappingException:无法读取 XML

标签: java hibernate exception hibernate-mapping runtimeexception


【解决方案1】:
<mapping resource="student.hbm.xml" />

并确保它与 hibernate.cfg.xml 在同一个目录中

【讨论】:

  • 两个xml文件在同一个目录
  • 我现在已经在默认 pkg 中获取了所有 xml 文件和类文件。但是在线程“main”org.hibernate.InvalidMappingException 中仍然出现异常异常:无法在 AddStudent 读取 XML java.lang.NullPointerException。主要(AddStudent.java:37)
猜你喜欢
  • 2018-01-09
  • 2011-07-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-02-14
  • 2017-09-08
相关资源
最近更新 更多