Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Start a free Courses trial
to watch this video
We now have data stored in our database - great! Eventually you will want to retrieve that data, so in this video you will use a Hibernate `Session` to get a list of all contacts stored in the database. You will do this by leveraging a Hibernate `Criteria` object.
Update for Hibernate 5.2+
As of Hibernate 5.2.0, the createCriteria
approach of fetching entities is deprecated. So, if you list hibernate-core 5.2.0 or later in build.gradle, then you'll get a message in your IDE, with the method call likely crossed out. Here is an example of what you should do in Application.java, to bring your code up to date:
private static List<Contact> fetchAllContacts() {
// Open a session
Session session = sessionFactory.openSession();
// DEPRECATED: Create Criteria
// Criteria criteria = session.createCriteria(Contact.class);
// DEPRECATED: Get a list of Contact objects according to the Criteria object
// List<Contact> contacts = criteria.list();
// UPDATED: Create CriteriaBuilder
CriteriaBuilder builder = session.getCriteriaBuilder();
// UPDATED: Create CriteriaQuery
CriteriaQuery<Contact> criteria = builder.createQuery(Contact.class);
// UPDATED: Specify criteria root
criteria.from(Contact.class);
// UPDATED: Execute query
List<Contact> contacts = session.createQuery(criteria).getResultList();
// Close the session
session.close();
return contacts;
}
Java 8 Features
Using Github With This Course
You can complete this course entirely using code created by you on your local machine. However, if you choose to use the code I've made available to you, you have two options:
- Use the project files linked at the bottom of this page, or
- Use the Github repository I've made available (recommended)
If you choose the recommended option of using the Github repository, it can be found at
https://github.com/treehouse/contactmgr-hibernate
To utilize Github with this course, you can download the Github desktop client for your system or use the command line interface provided with Git.
Clone this repository to your machine using the Github desktop client, or using the following command:
git clone git@github.com:treehouse/contactmgr-hibernate.git
To update your local repository to match a video's starting point, you can use the git checkout ...
command in combination with the stage and video number. For example, to update your local repository to match the starting point of Stage 5, Video 4, you'd use the following:
git checkout -f s5v4
Notice the use of the -f option. This forces Git to override all local changes, so be aware: this will cause any changes you made to be lost.
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up