-->
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.  [ 10 posts ] 
Author Message
 Post subject: Initial SessionFactory creation failed
PostPosted: Thu Nov 26, 2009 8:42 am 
Beginner
Beginner

Joined: Thu Nov 26, 2009 8:20 am
Posts: 23
Hi everyone,

first of all, I´m new to Hibernate. These are the steps I made so far:

I used HibernateTool to create all Beans plus mapping xml and the hibernate.cfg.xml for a existing mysql 5.0 catalog. I created a HibernateUtil class similiar to what I´ve seen in one of your tutorials.

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

public class HibernateUtil
{

private static final SessionFactory sessionFactory;

static {
try {
// Create the SessionFactory from hibernate.cfg.xml
sessionFactory = new Configuration().configure("src/hibernate.cfg.xml").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;
}
}


Now I want to simply create a session and print it like this:
(This method is called if the user pushes a button within the plugin)

public void go(){

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

System.out.println(session.toString());


setup();
run();
tearDown();
}


If I start the plugin and getCurrentSession is reached, the following message in the console is shown:

Initial SessionFactory creation failed.java.lang.NoClassDefFoundError: org/hibernate/cfg/Configuration

There is a lib folder in the plugin project with the following components:

antrl-2.7.6
asm-attrs
asm
cglib-2.1.3
commons-collections-2.1.1
commons-logging-1.0.4
dom4j-1.6.1
hibernate3
jta
log4j-1.2.11
mysql-connector-java-5.0.4-bin

All these components are added to the plugin project build path, so there should be no problem.


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>
<property name="hibernate.bytecode.use_reflection_optimizer">false</property>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.password">...</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/atomspro</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property>
<mapping resource=".../generator/beans/StructureddialogelementEvents.hbm.xml" />
<mapping resource=".../generator/beans/EannotationContents.hbm.xml" />
<mapping resource=".../generator/beans/Guide.hbm.xml" />
<mapping resource=".../generator/beans/Details.hbm.xml" />
<mapping resource=".../generator/beans/EoperationEexceptions.hbm.xml" />
<mapping resource=".../generator/beans/AnytypeAnyattribute.hbm.xml" />
....for all beans....
</session-factory>
</hibernate-configuration>


Thanks for the help.

Best regards.
StefanKNeedsHelp


Top
 Profile  
 
 Post subject: Re: Initial SessionFactory creation failed
PostPosted: Thu Nov 26, 2009 9:07 am 
Newbie

Joined: Thu Nov 26, 2009 2:05 am
Posts: 5
check your build path whether hibernate.jar is exist or not.


Top
 Profile  
 
 Post subject: Re: Initial SessionFactory creation failed
PostPosted: Thu Nov 26, 2009 9:16 am 
Beginner
Beginner

Joined: Thu Nov 26, 2009 8:20 am
Posts: 23
Hi there,

as I said, hibernate3.jar is correctly added to the build path. Is it possible that there are modifications needed to be done in the plugin.xml?


Top
 Profile  
 
 Post subject: Re: Initial SessionFactory creation failed
PostPosted: Thu Nov 26, 2009 9:25 am 
Newbie

Joined: Thu Nov 26, 2009 2:05 am
Posts: 5
If it possible send your project to me.
snrpriyadarshana@gmail.com


Top
 Profile  
 
 Post subject: Re: Initial SessionFactory creation failed
PostPosted: Thu Nov 26, 2009 10:10 am 
Beginner
Beginner

Joined: Thu Nov 26, 2009 8:20 am
Posts: 23
I don´t think that this is possible. If I add the jars to the classpath of the plugin, there is no exception shown anymore. But now I´ve got a new problem.

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

session.beginTransaction();

session.getTransaction().commit();

HibernateUtil.getSessionFactory().close();

As soon as the program reaches getCurrentSession, the main thread keeps stepping Thread [main] (Stepping) and I have to terminate the eclipse application


Top
 Profile  
 
 Post subject: Re: Initial SessionFactory creation failed
PostPosted: Thu Nov 26, 2009 10:36 am 
Beginner
Beginner

Joined: Thu Nov 26, 2009 8:20 am
Posts: 23
Update:

I copied the beans package and the HibernateUtil in a JavaProject and tried to run it. Now I get the following exception:

log4j:WARN No appenders could be found for logger (org.hibernate.cfg.Environment).
log4j:WARN Please initialize the log4j system properly.
Initial SessionFactory creation failed.org.hibernate.HibernateException: /hibernate.cfg.xml not found
Exception in thread "main" java.lang.ExceptionInInitializerError
at eu.emundo.atomspro.generator.util.HibernateUtil.<clinit>(HibernateUtil.java:18)
at com.alexwestconsulting.hibernateexample.Main.<init>(Main.java:21)
at com.alexwestconsulting.hibernateexample.Main.main(Main.java:15)
Caused by: org.hibernate.HibernateException: /hibernate.cfg.xml not found
at org.hibernate.util.ConfigHelper.getResourceAsStream(ConfigHelper.java:147)
at org.hibernate.cfg.Configuration.getConfigurationInputStream(Configuration.java:1405)
at org.hibernate.cfg.Configuration.configure(Configuration.java:1427)
at org.hibernate.cfg.Configuration.configure(Configuration.java:1414)
at eu.emundo.atomspro.generator.util.HibernateUtil.<clinit>(HibernateUtil.java:14)
... 2 more


The hibernate.cfg.xml can be found at the project root

I´m pretty much clueless now..


Top
 Profile  
 
 Post subject: Re: Initial SessionFactory creation failed
PostPosted: Thu Nov 26, 2009 10:38 am 
Regular
Regular

Joined: Mon Aug 07, 2006 5:07 am
Posts: 56
Can you try adding the property "hibernate.current_session_context_class" with the value "thread" to your Hibernate configuration (hibernate.cfg.xml or something)?
Possible values are: jta | thread | managed | custom.Class

http://docs.jboss.org/hibernate/core/3.3/reference/en/html/session-configuration.html#configuration-optional


Top
 Profile  
 
 Post subject: Re: Initial SessionFactory creation failed
PostPosted: Thu Nov 26, 2009 10:41 am 
Regular
Regular

Joined: Mon Aug 07, 2006 5:07 am
Posts: 56
Your hibernate.cfg.xml has to be on the classpath as well ... that means in your WEB-INF/classes folders or somewhere under a source folder.

I use Maven to build, and I put my config files under the folders src/main/resources or srs/test/resources. Maven then puts those files in the correct position to be on the classpath.

You might already try just putting the files under your source folder. Your HBM.XML files should also be on the classpath, regularly next to the .java files which they map.


Top
 Profile  
 
 Post subject: Re: Initial SessionFactory creation failed
PostPosted: Thu Nov 26, 2009 10:45 am 
Regular
Regular

Joined: Mon Aug 07, 2006 5:07 am
Posts: 56
To get rid of the appender warnings, you have a make an appender or logger for the specific package.

I use this kind of log4j.xml configuration (also has to be on the classpath):
Code:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">

<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
   <appender name="console" class="org.apache.log4j.ConsoleAppender">
      <param name="Target" value="System.out" />
      <layout class="org.apache.log4j.PatternLayout">
         <param name="ConversionPattern"
            value="%-25d{dd/MM/yyyy HH:mm:ss,SSS} %-5p %C{1}.%M - %m%n" />
      </layout>
   </appender>
   <appender name="file" class="org.apache.log4j.FileAppender">
      <param name="File" value="./target/projectname-test.log" />
      <param name="Append" value="true" />
      <layout class="org.apache.log4j.PatternLayout">
         <param name="ConversionPattern"
            value="%-25d{dd/MM/yyyy HH:mm:ss,SSS} %-5p %C{1}.%M - %m%n" />
      </layout>
   </appender>

   <logger name="be.foo.projectname">
      <level value="debug" />
   </logger>
   <logger name="org.hibernate">
      <level value="info" />
   </logger>
   <logger name="org.hibernate.SQL">
      <level value="debug" />
   </logger>
   <logger name="org.hibernate.tool.hbm2ddl">
      <level value="debug" />
   </logger>

   <root>
      <priority value="warn" />
      <appender-ref ref="file" />
      <!-- <appender-ref ref="console"/> -->
   </root>

</log4j:configuration>

You can play with it a little to customize to your needs.


Top
 Profile  
 
 Post subject: Re: Initial SessionFactory creation failed
PostPosted: Thu Nov 26, 2009 11:10 am 
Beginner
Beginner

Joined: Thu Nov 26, 2009 8:20 am
Posts: 23
Hi Don Stevo,

concerning your first reply, I added the hibernate.current_session_context_class as you said, but there was no effect at all. I think the plugin has the same problem as the java project, they can´t find the hibernate.cfg.xml, which is very curious because I did the exact same thing as some guy on youtube:

http://www.youtube.com/watch?v=nLOiW9Lw4mo

The only difference is that all of my beans, the mapping xml files and the hibernate.cfg.xml were created via hibernateTools


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