java - 失败.org.hibernate.HibernateException

标签 java database hibernate

我是 Hibernate 的新手。我创建了一个简单的程序来使用 Hibernate 将值插入数据库。

我的文件结构是这样的

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

src -> com.xml -> hibernate.cfg.xml 和学生.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)

最佳答案

 <?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>

关于java - 失败.org.hibernate.HibernateException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12171794/

相关文章:

java - 阻止 EDT 直到 executorservice 完成所有任务

java - 在 Play Framework 中通过 HTTP 服务后删除文件 - java

java - java中模拟OS进程/抢占式线程停止/classloader+线程进程模拟

java - Hibernate SQLQuery 列表方法返回对象而不是对象数组

hibernate - 如何在没有注释的字段中使用Hibernate验证程序?

java - android-maven-plugin debug 永远等待调试器

sql - 根据最后日期选择行

mysql - 列出收入高于经理的员工姓名

mysql - 从今天开始按顺序从数据库中提取事件

javax.servlet.ServletException : Error creating bean - Invocation of init method failed; nested exception is javax. 持久性.PersistenceException