-->
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.  [ 9 posts ] 
Author Message
 Post subject: SessionFactory problem
PostPosted: Mon Sep 22, 2008 7:58 am 
Beginner
Beginner

Joined: Mon Mar 17, 2008 11:25 am
Posts: 42
hi ,
I have fallowing problem with SessionFactory. When I'm calling method findById iI recive error :

22.09.2008 12:46:03 DAO.AdressenHome getSessionFactory
SCHWERWIEGEND: Could not locate SessionFactory in JNDI
javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file: java.naming.factory.initial
at javax.naming.spi.NamingManager.getInitialContext(Unknown Source)
at javax.naming.InitialContext.getDefaultInitCtx(Unknown Source)
at javax.naming.InitialContext.getURLOrDefaultInitCtx(Unknown Source)
at javax.naming.InitialContext.lookup(Unknown Source)
at DAO.AdressenHome.getSessionFactory(AdressenHome.java:30)
at DAO.AdressenHome.<init>(AdressenHome.java:25)
at DAO.AdressenHome.main(AdressenHome.java:127)
Exception in thread "main" java.lang.IllegalStateException: Could not locate SessionFactory in JNDI
at DAO.AdressenHome.getSessionFactory(AdressenHome.java:33)
at DAO.AdressenHome.<init>(AdressenHome.java:25)
at DAO.AdressenHome.main(AdressenHome.java:127)

please help.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Sep 22, 2008 4:47 pm 
Beginner
Beginner

Joined: Mon Mar 17, 2008 11:25 am
Posts: 42
here is code :
1.hibernate.cfg.xml
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 >

      <!-- local connection properties -->
      <property name="hibernate.connection.url">jdbc:mysql://test-server/Elaser</property>
      <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
      <property name="hibernate.connection.username">patryk</property>
      <property name="hibernate.connection.password">elaser</property>
      <!-- property name="hibernate.connection.pool_size"></property -->

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

        <property name="hibernate.show_sql">false</property>
        <property name="hibernate.transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</property>
       
    </session-factory>
</hibernate-configuration>


2. HibernateUtil
Code:
package util;

import org.hibernate.SessionFactory;
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;
    }

}



3. DAO-class
Code:
package DAO;
// default package
// Generated 22.09.2008 09:49:57 by Hibernate Tools 3.2.1.GA

import java.util.List;
import javax.naming.InitialContext;

import model.Adressen;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.hibernate.SessionFactory;
import static org.hibernate.criterion.Example.create;

/**
* Home object for domain model class Adressen.
* @see .Adressen
* @author Hibernate Tools
*/
public class AdressenHome {

   private static final Log log = LogFactory.getLog(AdressenHome.class);

   private final SessionFactory sessionFactory = getSessionFactory();

   protected SessionFactory getSessionFactory() {
      try {
         return (SessionFactory) new InitialContext()
               .lookup("SessionFactory");
      } catch (Exception e) {
         log.error("Could not locate SessionFactory in JNDI", e);
         throw new IllegalStateException(
               "Could not locate SessionFactory in JNDI");
      }
   }

   public void persist(Adressen transientInstance) {
      log.debug("persisting Adressen instance");
      try {
         sessionFactory.getCurrentSession().persist(transientInstance);
         log.debug("persist successful");
      } catch (RuntimeException re) {
         log.error("persist failed", re);
         throw re;
      }
   }

   public void attachDirty(Adressen instance) {
      log.debug("attaching dirty Adressen instance");
      try {
         sessionFactory.getCurrentSession().saveOrUpdate(instance);
         log.debug("attach successful");
      } catch (RuntimeException re) {
         log.error("attach failed", re);
         throw re;
      }
   }

   public void attachClean(Adressen instance) {
      log.debug("attaching clean Adressen instance");
      try {
         sessionFactory.getCurrentSession().lock(instance, LockMode.NONE);
         log.debug("attach successful");
      } catch (RuntimeException re) {
         log.error("attach failed", re);
         throw re;
      }
   }

   public void delete(Adressen persistentInstance) {
      log.debug("deleting Adressen instance");
      try {
         sessionFactory.getCurrentSession().delete(persistentInstance);
         log.debug("delete successful");
      } catch (RuntimeException re) {
         log.error("delete failed", re);
         throw re;
      }
   }

   public Adressen merge(Adressen detachedInstance) {
      log.debug("merging Adressen instance");
      try {
         Adressen result = (Adressen) sessionFactory.getCurrentSession()
               .merge(detachedInstance);
         log.debug("merge successful");
         return result;
      } catch (RuntimeException re) {
         log.error("merge failed", re);
         throw re;
      }
   }

   public Adressen findById(java.lang.String id) {
      log.debug("getting Adressen instance with id: " + id);
      try {
         Adressen instance = (Adressen) sessionFactory.getCurrentSession()
               .get("Adressen", id);
         if (instance == null) {
            log.debug("get successful, no instance found");
         } else {
            log.debug("get successful, instance found");
         }
         return instance;
      } catch (RuntimeException re) {
         log.error("get failed", re);
         throw re;
      }
   }

   public List<Adressen> findByExample(Adressen instance) {
      log.debug("finding Adressen instance by example");
      try {
         List<Adressen> results = (List<Adressen>) sessionFactory
               .getCurrentSession().createCriteria("Adressen").add(
                     create(instance)).list();
         log.debug("find by example successful, result size: "
               + results.size());
         return results;
      } catch (RuntimeException re) {
         log.error("find by example failed", re);
         throw re;
      }
   }}


code was generated with hibernate tools. where is the problem?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 23, 2008 3:22 am 
Newbie

Joined: Fri Sep 19, 2008 6:59 am
Posts: 6
Get the SessionFactory using
HibernateUtil.getSessionFactory() instead of Initial Context

_________________
Rahul


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 23, 2008 3:29 am 
Beginner
Beginner

Joined: Mon Mar 17, 2008 11:25 am
Posts: 42
do I need HibernateUtil.class???


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 23, 2008 3:29 am 
Beginner
Beginner

Joined: Mon Mar 17, 2008 11:25 am
Posts: 42
do I need HibernateUtil.class???


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 23, 2008 3:33 am 
Beginner
Beginner

Joined: Mon Mar 17, 2008 11:25 am
Posts: 42
I made as you said . I recive now following error:


Code:
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
Initial SessionFactory creation failed.java.lang.NoClassDefFoundError: org/slf4j/impl/StaticLoggerBinder
Exception in thread "main" java.lang.ExceptionInInitializerError
   at util.HibernateUtil.<clinit>(HibernateUtil.java:18)
   at DAO.AdressenHome.getSessionFactory(AdressenHome.java:31)
   at DAO.AdressenHome.<init>(AdressenHome.java:27)
   at DAO.AdressenHome.main(AdressenHome.java:130)
Caused by: java.lang.NoClassDefFoundError: org/slf4j/impl/StaticLoggerBinder
   at org.slf4j.LoggerFactory.<clinit>(LoggerFactory.java:60)
   at org.hibernate.cfg.Configuration.<clinit>(Configuration.java:151)
   at util.HibernateUtil.<clinit>(HibernateUtil.java:14)
   ... 3 more


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 23, 2008 3:46 am 
Senior
Senior

Joined: Wed Sep 19, 2007 9:31 pm
Posts: 191
Location: Khuntien (Indonesia)
It means that you need slf4j.jar. Put the jar into your classpath.

patrick1984 wrote:
I made as you said . I recive now following error:


Code:
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
Initial SessionFactory creation failed.java.lang.NoClassDefFoundError: org/slf4j/impl/StaticLoggerBinder
Exception in thread "main" java.lang.ExceptionInInitializerError
   at util.HibernateUtil.<clinit>(HibernateUtil.java:18)
   at DAO.AdressenHome.getSessionFactory(AdressenHome.java:31)
   at DAO.AdressenHome.<init>(AdressenHome.java:27)
   at DAO.AdressenHome.main(AdressenHome.java:130)
Caused by: java.lang.NoClassDefFoundError: org/slf4j/impl/StaticLoggerBinder
   at org.slf4j.LoggerFactory.<clinit>(LoggerFactory.java:60)
   at org.hibernate.cfg.Configuration.<clinit>(Configuration.java:151)
   at util.HibernateUtil.<clinit>(HibernateUtil.java:14)
   ... 3 more


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 23, 2008 3:54 am 
Beginner
Beginner

Joined: Mon Mar 17, 2008 11:25 am
Posts: 42
ok I put the jars into bulit path and I recive "few" errors :)
java.lang.StackOverflowError
at java.lang.reflect.Method.<init>(Unknown Source)
at java.lang.reflect.Method.copy(Unknown Source)
at java.lang.reflect.ReflectAccess.copyMethod(Unknown Source)
at sun.reflect.ReflectionFactory.copyMethod(Unknown Source)
at java.lang.Class.searchMethods(Unknown Source)
at java.lang.Class.getMethod0(Unknown Source)
at java.lang.Class.getMethod(Unknown Source)
at org.apache.commons.logging.LogFactory.getContextClassLoader(LogFactory.java:438)
at org.apache.commons.logging.LogFactory$1.run(LogFactory.java:222)
at java.security.AccessController.doPrivileged(Native Method)
at org.apache.commons.logging.LogFactory.getFactory(LogFactory.java:218)
at org.apache.commons.logging.LogFactory.getLog(LogFactory.java:370)
at org.slf4j.impl.JCLLoggerFactory.getLogger(JCLLoggerFactory.java:69)
at org.slf4j.LoggerFactory.getLogger(LoggerFactory.java:88)
at org.apache.commons.logging.impl.SLF4JLogFactory.getInstance(SLF4JLogFactory.java:155)
at org.apache.commons.logging.LogFactory.getLog(LogFactory.java:370)
at org.slf4j.impl.JCLLoggerFactory.getLogger(JCLLoggerFactory.java:69)
at org.slf4j.LoggerFactory.getLogger(LoggerFactory.java:88)


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 05, 2008 4:18 pm 
Newbie

Joined: Fri Dec 05, 2008 3:21 pm
Posts: 6
I got the stack overflow error that you mention, and fixed it by including only the slf4j files below:

slf4j-api-1.5.2.jar
slf4f-simple-1.5.2.jar

Previously I'd downloaded all of the slf4j files and copied to the lib directory.


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