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 Retrieving Data with Hibernate

Lakshman Erukulla
Lakshman Erukulla
5,689 Points

can anyone answer this question

Can anyone answer the below question?

Unable to find the answer for the same.

LanguageRepository.java
import java.util.List;
import java.util.ArrayList;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Criteria;

public class LanguageRepository {
  // Assume sessionFactory has been initialized properly by its default constructor
  private static final SessionFactory sessionFactory = new SessionFactory();

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

    // TODO: Create Criteria
    Criteria criteria = null;

    // TODO: Get a list of all persisted Language entities
    List<Language> languages = null;

    // Close the session
    session.close();

    // Return the list
    return languages;
  }
}
Brandon Khan
Brandon Khan
21,619 Points

Hello Lakshman Erukulla,

The goal here is to return a list of Language class from the session object. If you check your imports you can see you are getting access to the Session class from import org.hibernate.Session; to see all of the methods or uses of this class you can check the documentation. HERE: "https://docs.jboss.org/hibernate/orm/3.5/api/org/hibernate/Session.html". We can see our first TODO is to create a Criteria. Checking your imports you can see you gain access to the Criteria class from import org.hibernate.Criteria; The documentation for Criteria can be found HERE:"https://docs.jboss.org/hibernate/orm/3.5/api/org/hibernate/Criteria.html". After reading the doc you can see that a Criteria is what you want to get from your session in the example given in the doc they do this by using the createCriteria method...

Criteria createCriteria(Class persistentClass) Create a new Criteria instance, for the given entity class, or a superclass of an entity class. Parameters: persistentClass - a class, which is persistent, or has persistent subclasses Returns: Criteria

This method lives in the Session class we know this from its documentation. In order to fill our criteria object we need to call session.createCriteria( Language.class ); You can see above that the createCriteria method takes a class as its argument and returns you a Criteria. Now that we have instantiated our Criteria we can move on to our next TODO asking us to fill our languages object with a List of type Language ( the class we gave to our criteria object ). In the Criteria class we can see there is a method that returns a list of classes from the Criteria that is of the type given as and argument when the Criteria was instantiated ( in our case Language ). That method is criteria.list()...

List list() throws HibernateException /* the throw here is the type of exception that can pop up if there is a problem with your results like the class you want doesn't exits */ Get the results. Returns: The list of matched query results. Throws: HibernateException - Indicates a problem either translating the criteria to SQL, exeucting the SQL or processing the SQL results.

TLDR: I just wanted to show you how i broke the problem down sorry if its too wordy...

// TODO: Create Criteria Criteria criteria = session.createCriteria( Language.class ) ;

// TODO: Get a list of all persisted Language entities List<Language> languages = criteria.list();

1 Answer