1

私は休止状態が初めてです。Hibernate を使用して db に値を挿入する簡単なプログラムを作成します。

私のファイル構造は次のようになります

src -> com.visolve -> AddStudent.java and student.java

src -> com.xml -> hibernate.cfg.xml and student.hbm.xml

次のコードを使用して、AddStudent.java から構成ファイルを通信します。

 String file = "/src/com/xml/hibernate.cfg.xml";
 sessionFactory = new Configuration().configure(new File(file)).buildSessionFactory();

私の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="hibernate.connection.driver_class">
       com.mysql.jdbc.Driver</property>
       <property name="hibernate.connection.url">
       jdbc:mysql://localhost:3306/hibernateExamples</property>
      <property name="hibernate.connection.username">
       root</property>
      <property name="hibernate.connection.password">
      </property>
      <property name="hibernate.connection.pool_size">
      10</property>
       <property name="show_sql">true</property>
       <property name="dialect">
       org.hibernate.dialect.MySQLDialect</property>
       <property name="hibernate.hbm2ddl.auto">
         update</property>

           <!-- Mapping files -->

     <mapping resource="com/xml/student.hbm.xml"/>
     </session-factory>
   </hibernate-configuration>

new File(file).exists() をチェックすると、true が返されることを意味します..しかし、ここでは次の例外が返されます

Initial SessionFactory creation failed.org.hibernate.HibernateException: Could not parse configuration: src/com/xml/hibernate.cfg.xml
Exception in thread "main" java.lang.NullPointerException
at com.visolve.AddStudent.main(AddStudent.java:44)
4

1 に答える 1

2
 <?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="hibernate.connection.driver_class">
         com.mysql.jdbc.Driver</property>
       <property name="hibernate.connection.url">
            jdbc:mysql://localhost:3306/hibernateexamples</property>
       <property name="hibernate.connection.username">
          root</property>
       <property name="hibernate.connection.password">
           admin</property>
       <property name="hibernate.connection.pool_size">
            10</property>
       <property name="show_sql">true</property>
       <property name="dialect">
              org.hibernate.dialect.MySQLDialect</property>
       <property name="hibernate.hbm2ddl.auto">
            update</property>

       <!-- Mapping files -->

       <mapping resource="com/xml/student.hbm.xml" />
   </session-factory>
于 2012-09-12T05:21:33.433 に答える