Go Back   Velocity Reviews > Newsgroups > Java
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

Reply

Java - Exception of "Initial SessionFactory creation failed..."

 
Thread Tools Search this Thread
Old 11-09-2006, 01:01 PM   #1
Default Exception of "Initial SessionFactory creation failed..."


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:1
at messages.MessageManager.listMessages(MessageManage r.java:4
at messages.MessageManager.main(MessageManager.java:1 6)
Caused by: java.lang.ExceptionInInitializerError
at org.hibernate.cfg.Configuration.reset(Configuratio n.java:16
at org.hibernate.cfg.Configuration.<init>(Configurati on.java:187)
at org.hibernate.cfg.Configuration.<init>(Configurati on.java:191)
at util.HibernateUtil.<clinit>(HibernateUtil.java:13)
.... 2 more
Caused by: java.lang.NullPointerException
at
org.hibernate.util.ConfigHelper.getResourceAsStrea m(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(12 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.Driv er</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.ConsoleAppe nder
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.Patt ernLayout
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.Patte rnLayout
#log4j.appender.file.layout.ConversionPattern=%d{A BSOLUTE} %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.DriverManag erConnectionProvider=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().getCurrentSessio n();
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().getCurrentSessio n();
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
  Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off




SEO by vBSEO 3.3.2 ©2009, Crawlability, Inc.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46