首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >failed.org.hibernate.HibernateException

failed.org.hibernate.HibernateException
EN

Stack Overflow用户
提问于 2012-08-29 14:07:01
回答 1查看 2.5K关注 0票数 1

我对Hibernate还不熟悉。我创建了一个简单的程序,使用Hibernate将值插入到数据库中。

我的文件结构看起来像

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

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

我使用以下代码来传递来自AddStudent.java的配置文件

代码语言:javascript
复制
 String file = "/src/com/xml/hibernate.cfg.xml";
 sessionFactory = new Configuration().configure(new File(file)).buildSessionFactory();

我的hibernate.cfg.xml文件是

代码语言:javascript
复制
<?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。但在这里,它返回以下异常

代码语言:javascript
复制
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)
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-09-12 13:21:33

代码语言:javascript
复制
 <?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>
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12171794

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档