Dean Gibson Dean Gibson - 1 year ago 86
Java Question

Cannot find symbol HibernateUtil

I'm following the tutorial on Hibernate as seen here and in my 'FilmHelper' class I'm trying to get a Session object like so:

package dvdrental;

import org.hibernate.Session;

public class FilmHelper {

Session session = null;

public FilmHelper() {
this.session = HibernateUtil.getSessionFactory().getCurrentSession();


But I'm getting a 'cannot find symbol' error stating that it cannot find 'HibernateUtil' ... Has something changed in the way you obtain a session in Hibernate, is the tutorial out of date, or have I done something wrong?

Answer Source

I'm not sure this is how you should retrieve the session, especially if the FilmHelper instance is stored somewhere.

If you have access to an entity manager (I assume so because of the java-ee tag), you just need to do this:

Session s = (Session)entityManager.getDelegate();


The tutorial you linked has a part "Creating the Helper File", so maybe that's what's missing.

But please note that this is just a start. For more complex or robust solutions you should have a look at the documentation and maybe use JPA as an abstraction level.