1

我試圖在Eclipse如何解決這個hibernate運行時異常?

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> 
    

plz幫助。

+0

發佈更多異常信息。但有人猜測,xml文件不是你告訴程序應該在的地方。 – NimChimpsky

+0

運行時異常: org.hibernate.InvalidMappingException:無法讀取XML – Ganesh

回答

0
<mapping resource="student.hbm.xml" /> 

,並確保它是在同一direcotry作爲hibernate.cfg.xml中

+0

這兩個xml文件都在同一個目錄中 – Ganesh

+0

我已將所有xml文件和類文件放在默認pkg中。但仍在線程「main」org中創建異常Exception。 hibernate.InvalidMappingException:無法讀取XML java.lang.NullPointerException \t at AddStudent.main(AddStudent.java:37) – Ganesh

相關問題