1

I am new to Hibernate. I create a simple program to insert values to db using Hibernate.

My File structure looks like

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

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

I use the following code to communicate the confiure file from AddStudent.java

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

My hibernate.cfg.xml file is

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

If i check new File(file).exists() means it returns true.. But here it returns the following exception

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)
2
  • It would be beneficial to you to post the actual hibernate.cfg.xml file. Commented Aug 29, 2012 at 6:10
  • 3
    Are there supposed to be all those weird line breaks like between "http://hibernate.sourceforge.net/ and hibernate-configuration-3.0.dtd">? Commented Aug 29, 2012 at 6:25

1 Answer 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>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.