DAO code not working with Vaadin

I took the code from here:
Hibernate DAO Tutorial
and run it in a java Console application with out problems:


Main.java (Console application)
AdminService service = new AdminServiceImpl();
service.setAdminDAO(new AdminDAOHibernateImpl());

service.registerEmployee(....);
service.getAllEmployees();

this code in a java console projekt works fine.

But then i created a new Vaadin projekt with the same code and always get this error:


java.lang.NoClassDefFoundError: Could not initialize class com.vaadintest.data.dao.AdminDAOHibernateImpl
	com.vaadintest.Dao_vaadin_testApplication.init(Dao_vaadin_testApplication.java:17)
	com.vaadin.Application.start(Application.java:554)
	com.vaadin.terminal.gwt.server.AbstractApplicationServlet.startApplication(AbstractApplicationServlet.java:1182)
	com.vaadin.terminal.gwt.server.AbstractApplicationServlet.service(AbstractApplicationServlet.java:466)
	javax.servlet.http.HttpServlet.service(HttpServlet.java:717)

This error occures only if “new AdminDAOHibernateImpl()” is called. “AdminService service = new AdminServiceImpl();” works fine.

Hi,

The error looks like a Classpath problem, so check that all needed classes / .jars are found on classpath.

HTH,
/Jonatan

Hi, i found the “problem”

original DAO.java (not working) :


public class DAO {
	
	@SuppressWarnings("unused")
	private static final Logger log = Logger.getAnonymousLogger();
	@SuppressWarnings("unchecked")
	
	private static final ThreadLocal session = new ThreadLocal();
	private static final SessionFactory sessionFactory = new Configuration()
			.configure().buildSessionFactory();
	
	protected DAO() {
	}
...
}

.configure() reads the “hibernate.cfg.xml” with all the needed settings and mappings. Code no.2 dont need this file, settings were made by .setProperty();

this code is working, but dont know why :wink:


public class DAO {
	private static final Logger log = Logger.getAnonymousLogger();
	private static final ThreadLocal session = new ThreadLocal();
	private static final SessionFactory sessionFactory ;
	
	static{
    	try {
        	AnnotationConfiguration cnf = new AnnotationConfiguration();
            cnf.setProperty(Environment.DRIVER, "com.mysql.jdbc.Driver");
            cnf.setProperty(Environment.URL, ....);
            ..................

            cnf.addAnnotatedClass(User.class);

            sessionFactory = cnf.buildSessionFactory();
        } catch (Throwable ex) {
           .................
        }

	}

	protected DAO(){
		
	}

...
}

The error message with the classpath is wrong, everything is fine with it.
The problem is that with code no 1 the DAO and all inherited classes could not be created !?

The Problem has to do somethind with the sessionFactory. Can someone explain me whats the problem with code no.1?