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.  [ 4 posts ] 
Author Message
 Post subject: problem getting SessionFactory
PostPosted: Wed Nov 22, 2006 10:44 am 
Newbie

Joined: Tue Nov 21, 2006 5:47 am
Posts: 12
Hi all,
when executing a java application I receive this exception:
xception in thread "main" java.lang.IllegalStateException: Could not locate SessionFactory in JNDI.
My cfg.xml is :
<session-factory>
<property name="hibernate.connection.driver_class">org.gjt.mm.mysql.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://10.0.0.196:3306/test</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">admin</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<mapping resource="hibernate/Groupes.hbm.xml" />
<mapping resource="hibernate/Grppers.hbm.xml" />
<mapping resource="hibernate/Personnes.hbm.xml" />
</session-factory>


and my code java is :
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");
}

How to solve this problem
Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 22, 2006 10:54 am 
Newbie

Joined: Sat Nov 18, 2006 9:49 am
Posts: 10
Hi,
Access session from HibernateUtil using the code

Session session=HibernateUtil.currentSession();
Transaction tx=session.beginTransaction();
.........


Please use the HibernateUtil.java in tutorial. or create a java file as..

/***************HibernateUtil.java**************************/
import org.hibernate.*;
import org.hibernate.cfg.*;

public class HibernateUtil {

private static final SessionFactory sessionFactory;

static {
try {
// Create the SessionFactory
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 final ThreadLocal session = new ThreadLocal();

public static Session currentSession() throws HibernateException {
Session s = (Session) session.get();
// Open a new Session, if this Thread has none yet
if (s == null) {
s = sessionFactory.openSession();
session.set(s);
}
return s;
}

public static void closeSession() throws HibernateException {
Session s = (Session) session.get();
session.set(null);
if (s != null)
s.close();
}
}


Let me know if it works and dude some points.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 22, 2006 11:15 am 
Expert
Expert

Joined: Tue Jul 11, 2006 10:21 am
Posts: 457
Location: Columbus, Ohio
If you want to bind your SessionFactory to JNDI, you need to add

Code:
<property name="hibernate.session_factory_name">SessionFactory</property>


to your cfg.xml.


Top
 Profile  
 
 Post subject: RE:
PostPosted: Thu Nov 23, 2006 6:16 am 
Newbie

Joined: Tue Nov 21, 2006 5:47 am
Posts: 12
Thanks for your help.
I followed the example of HibernateUtil.java. But now U have this exception:
Could not instantiate dialect class
My cfg.xml is :
<session-factory>
<property name="hibernate.connection.driver_class">org.gjt.mm.mysql.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://10.0.0.196:3306/test</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">admin</property>
<property name="hibernate.dialect">net.sf.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.session_factory_name">SessionFactory</property>
<mapping resource="hibernate/Groupes.hbm.xml" />
<mapping resource="hibernate/Grppers.hbm.xml" />
<mapping resource="hibernate/Personnes.hbm.xml" />

</session-factory>


I have hibernate2.jar in my classpath but I dont knom what's wrong.
Thanks for help


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