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

the Session's method createCriteria is deprecated, how should I proceed for establishing Class criteria?

/It says I should use JPA criteria, not sure how to use it :(/

@Deprecated Criteria createCriteria(Class persistentClass);

/**
 * Create {@link Criteria} instance for the given class (entity or subclasses/implementors), using a specific
 * alias.
 *
 * @param persistentClass The class, which is an entity, or has entity subclasses/implementors
 * @param alias The alias to use
 *
 * @return The criteria instance for manipulation and execution
 *
 * @deprecated (since 5.2) for Session, use the JPA Criteria
 */

3 Answers

Thank you very much, I already came up with an answer here at https://docs.jboss.org/hibernate/entitymanager/3.5/reference/en/html/querycriteria.html

Ended up this way

private static List<Contact> fetchAllContacts(){
        //Open Session
        Session session = sessionFactory.openSession();

        /* //Deprecated Way
        * Criteria criteria = session.createCriteria(Contact.class);
        * List<Contacts> contacts = criteria.list(); */

        //Get Criteria Builder
        CriteriaBuilder builder = session.getCriteriaBuilder();

        //Create Criteria
        CriteriaQuery<Contact> criteria = builder.createQuery(Contact.class);
        Root<Contact> contactRoot = criteria.from(Contact.class);
        criteria.select(contactRoot);

        //Use criteria to query with session to fetch all contacts
        List<Contact> contacts = session.createQuery(criteria).getResultList();

        //Close session
        session.close();

        return contacts;
    }

Cool : )

I had asked about this a few weeks back.

Chris Ramacciotti

Has suggested a solution for 'Spring with Hibernate' here:

https://github.com/treehouse/giflib-hibernate/commit/f97a2828a466e849d8ae84884b5dce60a66cf412

Take a look.

If you are watching 'Hibernate Basics' that will also work.

LOL!

it was explained in the teacher's notes... I just saw it XD!