user818700
user818700

Reputation:

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?

Upvotes: 0

Views: 4571

Answers (3)

T. Popović
T. Popović

Reputation: 345

You need to make your own class HibernateUtil as folows:

  • Right-click the Source Packages node and select New > Other to open the New File wizard.
  • Select Hibernate from the Categories list and HibernateUtil.java (or other name) from the File Types list. Click Next.
  • Type HibernateUtil (or other name) for the class name and type package name in which the class will be stored. Click Finish.

It is supposed that IDE is NetBeans.

Upvotes: 0

Vinay Prajapati
Vinay Prajapati

Reputation: 7546

If you are exactly following the tutorial, check for package of HibernateUtil.java and it access i.e. whether it is public or protected.Also, check if you are missing import for HibernateUtil.java. If still issue is there, check HibernateUtil.java for any error related to library. As sometimes imported class have issue which causes cannot find alert in different class.

Upvotes: 0

Thomas
Thomas

Reputation: 88747

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();

UPDATE:

The tutorial you linked has a part "Creating the HibernateUtil.java 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.

Upvotes: 1

Related Questions