2012-08-29 14 views
1

我是Hibernate的新手。我创建了一个简单的程序来使用Hibernate将值插入到数据库中。failed.org.hibernate.HibernateException

我的文件结构看起来像

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

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

我用下面的代码从AddStudent.java沟通confiure文件

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> 

如果我签新的文件(文件).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) 
+0

这将有利于你张贴的实际hibernate.cfg。 xml文件。 – Strelok

+3

是否应该存在所有那些奇怪的换行符,如'“http:// hibernate.sourceforge.net /'和'hibernate-configuration-3.0.dtd”>'? –

回答

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>