Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Java Hibernate Basics Persisting Data with Hibernate Fetching Data with Hibernate

Anyone else get a syntax error when using the updated code for Hibernate 5.2+?

Hi,

I set my dependencies in my gradle.build file to the latest stable releases available:

dependencies { compile 'org.hibernate:hibernate-core:5.2.6.Final' compile 'com.h2database:h2:1.4.193' compile 'javax.transaction:jta:1.1'

}

and tried out the Update for Hibernate 5.2+ listed in the Teacher's Notes.

I got a syntax error on the method invocation session.getCriteriaBuilder();

I dug around in the online documentation, and getCriteriaBuilder does not seem to be a method of the Session class, but I can't find how to make it work.

Anyone have a fix for this?

Thanks,

Iver

4 Answers

Alexander Nikiforov
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Alexander Nikiforov
Java Web Development Techdegree Graduate 22,175 Points

Could you push your project on GitHub and share a link here so that I can reproduce the error?

Because I know the code above should work, and I need to see all your files.

I just git clone project by Chris. and replaced code from teachers notes, works like a charm.

Don't know where you look. but Session class HAS getCriteriaBuilder

Please check the docs below

https://docs.jboss.org/hibernate/orm/5.2/javadocs/org/hibernate/Session.html

In this docs it says that Session extends EntityManager that means that is inherits all the method from it

public interface Session extends SharedSessionContract, EntityManager, HibernateEntityManager, AutoCloseable {
 // code from Session
}

And if you go to EntityManager docs:

http://docs.oracle.com/javaee/6/api/javax/persistence/EntityManager.html?is-external=true

You will see there getCriteriaBuilder method:

Here is the direct link

http://docs.oracle.com/javaee/6/api/javax/persistence/EntityManager.html#getCriteriaBuilder()

Using new hibernate core was making trouble with lots of my methods, be sure to have the right dependency of this version by using all the dependency required. (you can download your hibernate core and find into libs directory the required dependency that could be missing)

I'm using Hibernate 5.2.9 Final and not any issue with getCriteriaBuilder() method. this is my syntax:

//Create CriteriaBuilder

CriteriaBuilder builder = session.getCriteriaBuilder();

Daniel Haughton
Daniel Haughton
765 Points

I had the same issue,I forgot to refresh my build.gradle This is what is in my grade file compile group: 'org.hibernate', name: 'hibernate-core', version: '5.2.6.Final'

Kareem Jeiroudi
Kareem Jeiroudi
14,984 Points

I'm using

'org.hibernate:hibernate-core:5.4.2.Final'

and following the updated documentation of Hibernate, you don't need to create any Criteria object. Instead, in the query string in createQuery(), you'd specify the entity name and it will automatically map the columns in the database to the fields in Contact, and therefore, will create a list of contacts when calling the method list(). Have a look at my code, it's functional.

@SuppressWarnings("unchecked")
  private static List<Contact> fetchAllContacts() {
    Session session = sessionFactory.openSession();
    session.beginTransaction();
    List<Contact> contacts = session.createQuery("from Contact").list();
    session.getTransaction().commit();
    session.close();
    return contacts;
  }