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

Sylwester Guzek
Sylwester Guzek
16,088 Points

Hibernate's legacy org.hibernate.Criteria API is deprecated;

I am getting warning: Hibernate's legacy org.hibernate.Criteria API is deprecated;

Looks that this course is getting a bit old.

1 Answer

Yanuar Prakoso
Yanuar Prakoso
15,196 Points

Yes it is already deprecated, but to be fair in those courses where Criteria API is used there are mentions of the latest code to use to fetch data for Hibernate 5.2+ in the Teacher's Notes . It should look like this:

public List<Category> findAll() {
    // Open a session
    Session session = sessionFactory.openSession();

    // DEPRECATED as of Hibernate 5.2.0
    // List<Category> categories = session.createCriteria(Category.class).list();

    // UPDATED: Create CriteriaBuilder
    CriteriaBuilder builder = session.getCriteriaBuilder();

    // UPDATED: Create CriteriaQuery
    CriteriaQuery<Category> criteria = builder.createQuery(Category.class);

    // UPDATED: Specify criteria root
    criteria.from(Category.class);

    // UPDATED: Execute query
    List<Category> categories = session.createQuery(criteria).getResultList();

    // Close session
    session.close();

    return categories;

As you can see it both includes the deprecated and updated code to serve the same purposes as Criteria API.

I hope this helps a little