-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 
Author Message
 Post subject: New User:SessionFactory is not getting created
PostPosted: Tue Aug 01, 2006 5:05 am 
Beginner
Beginner

Joined: Tue Aug 01, 2006 4:57 am
Posts: 25
Hi
I am a new user of Hibernate and I need some help.
This is the code:
Code:
package com.rosenet.pojo;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;


public class FirstExample {
   
  public static void main(String[] args) {
    
    Session session = null;
    try{
       
       // This step will read hibernate.cfg.xml and prepare hibernate for use

        System.out.println("I am here");
       SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
       session = sessionFactory.openSession();
       
       //Create new instance of Contact and set values in it by reading them from form object

        System.out.println("Inserting Record");
        Contact contact = new Contact();
        contact.setId(3);
        contact.setFirstName("Riaz");
        contact.setLastName("Uddin");
        contact.setEmail("ruuddin@yahoo.com");
       
        session.save(contact);
       
       
        System.out.println("Done");
       
    }catch(Exception e){
      System.out.println(e.getMessage());
    }finally{
      // Actual contact insertion will happen at this step
      session.flush();
      session.close();

      }
  }
}

When i run this program in eclipse it shows Nullpointer exception at session.flush();
When i tried to debug I found that the session factory is not getting created.
the hibernate.cfg.xml file is as follows:
Code:
<?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/samepageinfosol</property>
      <property name="hibernate.connection.username">localhost</property>
      <property name="hibernate.connection.password"></property>
      <property name="hibernate.connection.pool_size">10</property>
      <property name="show_sql">true</property>
      <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
   <property name="hibernate.hbm2ddl.auto">update</property>
      <!-- Mapping files -->
      <mapping resource="contact.hbm.xml"/>
</session-factory>
</hibernate-configuration>


I have placed this file in WEB-INF/ folder and my contact.hbm.xml file is as follows:
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>
  <class name="com.rosenet.pojo.Contact" table="contact">
   <id name="id" type="long" column="id" >
   <generator class="assigned"/>
  </id>

  <property name="firstName">
     <column name="first_name" />
  </property>
  <property name="lastName">
    <column name="last_name"/>
  </property>
  <property name="email">
    <column name="email"/>
  </property>
</class>
</hibernate-mapping>


What is wrong with this code... ? can someone help..


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 01, 2006 1:54 pm 
Beginner
Beginner

Joined: Wed Jun 28, 2006 2:24 pm
Posts: 30
Location: Brazil
If you are running FirstExample as standalone application, put your hibernate.cfg.xml file in root source folder.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 01, 2006 3:36 pm 
Beginner
Beginner

Joined: Tue Aug 01, 2006 4:57 am
Posts: 25
my directory structure is like this...

Code:
WEB-INF/hibernate.cfg.xml
WEB-INF/classes/com/rosenet/pojo/FirstExample.class{Contact.class}
WEB-INF/com/rosenet/pojo/FirstExample.java{Contact.java}



Where actually shall i keep my hibernate.cfg.xml. I have tried moving the file to each and every directory but it did not work.[/list]


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 01, 2006 9:30 pm 
Beginner
Beginner

Joined: Wed Jun 28, 2006 2:24 pm
Posts: 30
Location: Brazil
Is your application web or standalone?

Are you trying to run FirstExample class inside a web directory structure? If so, try to put hibernate.cfg.xml in /WEB-INF/classes.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.