Exception of "Initial SessionFactory creation failed..."

Discussion in 'Java' started by Thomas Qi, Nov 9, 2006.

  1. Thomas Qi

    Thomas Qi Guest

    hibernate v3.2
    Mysql v5.0
    Eclispe v3.2

    when I run an application based on the demo of "Hibernate 3.2", I met
    some exceptions below:

    09:46:49,818 INFO Environment:500 - Hibernate 3.2.0
    Initial SessionFactory creation
    failed.java.lang.ExceptionInInitializerError
    Exception in thread "main" java.lang.ExceptionInInitializerError
    at util.HibernateUtil.<clinit>(HibernateUtil.java:18)
    at messages.MessageManager.listMessages(MessageManager.java:48)
    at messages.MessageManager.main(MessageManager.java:16)
    Caused by: java.lang.ExceptionInInitializerError
    at org.hibernate.cfg.Configuration.reset(Configuration.java:168)
    at org.hibernate.cfg.Configuration.<init>(Configuration.java:187)
    at org.hibernate.cfg.Configuration.<init>(Configuration.java:191)
    at util.HibernateUtil.<clinit>(HibernateUtil.java:13)
    .... 2 more
    Caused by: java.lang.NullPointerException
    at
    org.hibernate.util.ConfigHelper.getResourceAsStream(ConfigHelper.java:144)
    at org.hibernate.cfg.Environment.<clinit>(Environment.java:515)
    .... 6 more

    these are the codes:
    1. sql script
    drop database if exists SAMPLEDB;
    create database SAMPLEDB;
    use SAMPLEDB;

    create table messages (
    Message_ID bigint not null primary key,
    Message_TITLE varchar(15) not null,
    Message_CONTENT varchar(128) not null,
    Message_DATE timestamp
    );


    2. src/hibernate.cfg.xml
    <?xml version='1.0' encoding='utf-8'?>
    <!DOCTYPE hibernate-configuration PUBLIC
    "-//Hibernate/Hibernate Configuration DTD 3.0//EN"

    "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

    <hibernate-configuration>

    <session-factory>

    <!-- Database connection settings -->
    <property
    name="connection.driver_class">com.mysql.jdbc.Driver</property>
    <property
    name="connection.url">jdbc:mysql://localhost:3306/sampledb</property>
    <property name="connection.username">root</property>
    <property name="connection.password">123456</property>

    <!-- JDBC connection pool (use the built-in) -->
    <property name="connection.pool_size">1</property>

    <!-- SQL dialect -->
    <property
    name="dialect">org.hibernate.dialect.MySQLDialect</property>

    <!-- Enable Hibernate's automatic session context management -->
    <property name="current_session_context_class">thread</property>

    <!-- Disable the second-level cache -->
    <property name="cache.provider_class">
    org.hibernate.cache.NoCacheProvider
    </property>

    <!-- Echo all executed SQL to stdout -->
    <property name="show_sql">true</property>

    <!-- Drop and re-create the database schema on startup -->
    <property name="hbm2ddl.auto">create</property>

    <mapping resource="messages/Message.hbm.xml" />

    </session-factory>

    </hibernate-configuration>


    3. src/log4j.properties
    ### direct log messages to stdout ###
    log4j.appender.stdout=org.apache.log4j.ConsoleAppender
    log4j.appender.stdout.Target=System.out
    log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
    log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p
    %c{1}:%L - %m%n

    ### direct messages to file hibernate.log ###
    #log4j.appender.file=org.apache.log4j.FileAppender
    #log4j.appender.file.File=hibernate.log
    #log4j.appender.file.layout=org.apache.log4j.PatternLayout
    #log4j.appender.file.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L
    - %m%n

    ### set log levels - for more verbose logging change 'info' to 'debug'
    ###

    log4j.rootLogger=warn, stdout

    log4j.logger.org.hibernate=info
    #log4j.logger.org.hibernate=debug

    ### log HQL query parser activity
    #log4j.logger.org.hibernate.hql.ast.AST=debug

    ### log just the SQL
    #log4j.logger.org.hibernate.SQL=debug

    ### log JDBC bind parameters ###
    log4j.logger.org.hibernate.type=info
    #log4j.logger.org.hibernate.type=debug

    ### log schema export/update ###
    log4j.logger.org.hibernate.tool.hbm2ddl=debug

    ### log HQL parse trees
    #log4j.logger.org.hibernate.hql=debug

    ### log cache activity ###
    #log4j.logger.org.hibernate.cache=debug

    ### log transaction activity
    #log4j.logger.org.hibernate.transaction=debug

    ### log JDBC resource acquisition
    #log4j.logger.org.hibernate.jdbc=debug

    ### enable the following line if you want to track down connection ###
    ### leakages when using DriverManagerConnectionProvider ###
    #log4j.logger.org.hibernate.connection.DriverManagerConnectionProvider=trace


    4. src/util/HibernateUtil.java
    package util;

    import org.hibernate.*;
    import org.hibernate.cfg.*;

    public class HibernateUtil {

    private static final SessionFactory sessionFactory;

    static {
    try {
    // Create the SessionFactory from hibernate.cfg.xml
    sessionFactory = new Configuration().configure()
    .buildSessionFactory();
    } catch (Throwable ex) {
    // Make sure you log the exception, as it might be swallowed
    System.err.println("Initial SessionFactory creation failed." +
    ex);
    throw new ExceptionInInitializerError(ex);
    }
    }

    public static SessionFactory getSessionFactory() {
    return sessionFactory;
    }

    }


    5. src/messages/Message.java
    package messages;

    import java.util.*;

    public class Message {
    private Long id;

    private String title;

    private String content;

    private Date date;

    public Message() {
    }

    public Long getId() {
    return id;
    }

    private void setId(Long id) {
    this.id = id;
    }

    public Date getDate() {
    return date;
    }

    public void setDate(Date date) {
    this.date = date;
    }

    public String getTitle() {
    return title;
    }

    public void setTitle(String title) {
    this.title = title;
    }

    public String getContent() {
    return content;
    }

    public void setContent(String content) {
    this.content = content;
    }
    }


    6. src/messages/Message.hbm.xml
    <?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="messages.Message" table="messages">
    <id name="id" type="long" column="message_ID">
    <generator class="native" />
    </id>
    <property name="date" type="timestamp" column="message_DATE" />
    <property name="title" type="string" column="message_TITLE" />
    <property name="content" type="string" column="message_CONTENT"
    />
    </class>

    </hibernate-mapping>


    7. src/messages/MessageManager.java
    package messages;

    import org.hibernate.*;
    import java.util.*;

    import util.HibernateUtil;

    public class MessageManager {

    public static void main(String[] args) {
    MessageManager mgr = new MessageManager();

    if (args[0].equals("store")) {
    mgr.createAndStoreMessage("My Message", "My Content", new
    Date());
    } else if (args[0].equals("list")) {
    List messages = mgr.listMessages();
    for (int i = 0; i < messages.size(); i++) {
    Message theMessage = (Message) messages.get(i);
    System.out.println("Message: " + theMessage.getTitle()
    + " Content: " + theMessage.getContent() + " Time: "
    + theMessage.getDate());
    }
    }

    HibernateUtil.getSessionFactory().close();
    }

    private Long createAndStoreMessage(String title, String content,
    Date theDate) {

    Session session =
    HibernateUtil.getSessionFactory().getCurrentSession();
    session.beginTransaction();

    Message theMessage = new Message();
    theMessage.setTitle(title);
    theMessage.setContent(content);
    theMessage.setDate(theDate);

    session.save(theMessage);

    session.getTransaction().commit();

    return theMessage.getId();
    }

    private List listMessages() {

    Session session =
    HibernateUtil.getSessionFactory().getCurrentSession();
    session.beginTransaction();

    List result = session.createQuery("from Message").list();

    session.getTransaction().commit();

    return result;
    }

    }


    I had added all .jar files of Hibernate/lib directory to the classpath.
    it is so strange:
    the codes run ok on NetBeans 5.5, but can't work on Eclipse 3.2.
    who can tell me why?

    thanks a lot!


    Best Regards,

    Thomas


    ps: My MSN ID: .
     
    Thomas Qi, Nov 9, 2006
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. jeff
    Replies:
    0
    Views:
    1,555
  2. jeff
    Replies:
    3
    Views:
    1,726
    Scott
    Jun 26, 2003
  3. Kerri
    Replies:
    2
    Views:
    13,123
    Kevin Spencer
    Oct 27, 2003
  4. Ola
    Replies:
    0
    Views:
    567
  5. Selen
    Replies:
    0
    Views:
    2,721
    Selen
    May 28, 2004
Loading...

Share This Page