Reputation:
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
Reputation: 345
You need to make your own class HibernateUtil as folows:
It is supposed that IDE is NetBeans.
Upvotes: 0
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
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