-->
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.  [ 6 posts ] 
Author Message
 Post subject: Exception of "Initial SessionFactory creation failed...
PostPosted: Mon Nov 06, 2006 10:14 pm 
Newbie

Joined: Mon Nov 06, 2006 9:39 pm
Posts: 2
Location: Kunshan, Jiangsu, China
hibernate v3.2
Mysql v5.0

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
Code:
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
Code:
<?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
Code:
### 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
Code:
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
Code:
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
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="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
Code:
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.
thanks a lot!


Best Regards,

Thomas


ps: My MSN ID: qixiangnj@gmail.com.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 08, 2006 3:55 am 
Newbie

Joined: Mon Nov 06, 2006 9:39 pm
Posts: 2
Location: Kunshan, Jiangsu, China
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?


Top
 Profile  
 
 Post subject: Environment.getProperties() from Configuration.reset()
PostPosted: Sun Nov 19, 2006 1:44 pm 
Newbie

Joined: Sun Nov 19, 2006 1:32 pm
Posts: 2
Location: California, USA
I'm seeing a similar issue. I'm trying to execute the example (e.g. Events) and can't get past the creation of the Configuration object. It seems to go into never never land when invoking the Environment.getProperties() method from within Configuration.reset().

For some reason I can't step into getProperties() using the eclipse debugger.

If anyone has a solution or has made hibernate work under eclipse WTP 3.2, I'd like to hear it.


Top
 Profile  
 
 Post subject: Problem Solved
PostPosted: Sun Nov 19, 2006 9:50 pm 
Newbie

Joined: Sun Nov 19, 2006 1:32 pm
Posts: 2
Location: California, USA
Some simple problems were returning errors that really were not clear - the hibernate developers should think about how unclear the error reporting is.

1. The first problem was the hibernate.properties file is not optionally. The documentation would lead one to this conclusion, but the code does not allow for it. Even if you don't specify any properties in the file, the file must be present on the classpath.

2. The DTD for configuration and mapping is not pulled in properly from the sourceforge website, at least that's the case when run from eclipse on my machine. It also isn't pulled in from the hibernate jar file. My solution was to put copies into the working directory and change the references in my config and mapping files; someone must have a better solution.

That's it, once I solved these simple environment issues the code works.


Top
 Profile  
 
 Post subject: One Cause
PostPosted: Sat Mar 31, 2007 5:28 pm 
Beginner
Beginner

Joined: Mon Apr 24, 2006 9:47 pm
Posts: 33
Location: Kansas City, MO
The Hibernate Environment.class.getClassLoader() is returning null for some reason.


Top
 Profile  
 
 Post subject: Same Problem - other solution
PostPosted: Fri May 11, 2007 8:37 am 
Newbie

Joined: Fri May 11, 2007 8:32 am
Posts: 1
The error
Code:
Initial SessionFactory creation failed.java.lang.ExceptionInInitializerError
Exception in thread "main" java.lang.ExceptionInInitializerError


only happens, when I use Eclipse' User Libraries for hibernate jars.
If I include them directly to the project's classpath, everything works fine...

regards

smood[/quote]


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 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.