-->
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.  [ 5 posts ] 
Author Message
 Post subject: hibernate.cfg.xml
PostPosted: Thu Nov 11, 2004 1:48 pm 
Beginner
Beginner

Joined: Thu Apr 29, 2004 12:45 pm
Posts: 45
Hi everyone,

I'm having a diffiuculty connecting to my database with a Hibernate application. All configuration data are listed below in hibernate.cfg.xml . I know the data are correct because they work fine in a different Hibernate application. However, this time I am using hibernate.cfg.xml, which is located in the same directory as my HibernateUtil class, which is where the SessionFactory is created. Also, the db driver is listed as a classpath pathelement in the build script. I keep getting the error listed below. Can anyone think of a way to fix it? Thanks in advance!

Itchy



Here is the error:

com/myco/myapp/hibernate/util/HibernateUtil.java [12:1] variable sessionFactory might not have been initialized
public class HibernateUtil {




Here is the HibernateUtil code that creates the session factory:

private static final SessionFactory sessionFactory;
sessionFactory = new Configuration().configure().buildSessionFactory();




Here is hibernate.cfg.xml

<!-- Generated file - Do not edit! -->

<hibernate-configuration>
<!-- a SessionFactory instance listed as /jndi/name -->
<session-factory>
<!-- properties -->
<property name="dialect">net.sf.hibernate.dialect.SAPDBDialect</property>
<property name="show_sql">true</property>
<property name="use_outer_join">false</property>
<property name="connection.username">Developer</property>
<property name="connection.password">Developer</property>
<property name="connection.driver_class">com.sap.dbtech.jdbc.DriverSapDB</property>
<property name="connection.url">jdbc:sapdb://DBMACHINE/MYDB</property>
<!-- mapping files -->
<mapping resource="com/myco/myapp/hibernate/CompanyHibernateImpl.hbm.xml"/>
</session-factory>
</hibernate-configuration>


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 11, 2004 3:53 pm 
Beginner
Beginner

Joined: Wed Mar 17, 2004 4:13 pm
Posts: 21
Location: San Diego, CA
This looks like a compile time error.

You need to do:
private static final SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();

Get it? All in one line. You declare a final, but don't set it to anything. Finals need to be set when you declare them.

Chrisjan


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 11, 2004 5:22 pm 
Beginner
Beginner

Joined: Thu Apr 29, 2004 12:45 pm
Posts: 45
Hi Chrisjan,

Thanks for answering my post. I'm using the example given in the Hibernate docs. I posted it below, but you can also see it here: http://www.hibernate.org/hib_docs/refer ... ngwithcats

Also, I've used this class before, except that I set sessionFactory to a JNDI data source from JBoss. It worked fine. If I do like you suggest by declaring and setting sessionFactory all at once, then the other procedures won't have access to it.

I can't try it until tomorrow anyway because I'm no longer at work.

Itchy


import net.sf.hibernate.*;
import net.sf.hibernate.cfg.*;

public class HibernateUtil {

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

private static final SessionFactory sessionFactory;

static {
try {
// Create the SessionFactory
sessionFactory = new Configuration().configure().buildSessionFactory();
} catch (Throwable ex) {
log.error("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();
}
}


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 11, 2004 5:40 pm 
Beginner
Beginner

Joined: Wed Mar 17, 2004 4:13 pm
Posts: 21
Location: San Diego, CA
Oops, okay, I'm wrong about setting the finals only when you declare them. They can be set later, but should only be set once.

There shouldn't be any access problems if you create the factory at declaration. The problem is that you don't have the code to catch and handle any exception thrown from it, which is why the HibernateUtil uses a static initializer there.

So, I don't realy see anything wrong with the code. It still does look like a compile time error. Is that when you see it? Or is it at runtime?

Can you be specific about the exact line that you see the error, and post your entire Util class?

Perhaps you are trying to access sessionFactory before the static initializer?

Chrisjan


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 12, 2004 11:17 am 
Beginner
Beginner

Joined: Thu Apr 29, 2004 12:45 pm
Posts: 45
Hi Chrisjan,

I think I solved it. The problem was that I used the wrong error handling in the static initializer. I'd previously been using this class within JBoss and setting sessionFactory from a JNDI datasource, which requires different error handling -- different from the Hibernate tutorial example. And after converting to a standalone application, I didn't change it back the right way.

This was really frustrating because the compiler didn't report anything about the incorrect error handling. It merely told me that sessionFactory wan't getting initialized. Go figure.

Thanks for your input on this. I appreciate you taking the time to analyze it.

Itchy


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