Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll

- 2x 2x
- 1.75x 1.75x
- 1.5x 1.5x
- 1.25x 1.25x
- 1.1x 1.1x
- 1x 1x
- 0.75x 0.75x
- 0.5x 0.5x
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
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up-
cdlvr
14,448 PointsUse Collection.forEach() instead of Collection.stream().forEach()
Posted by cdlvrcdlvr
14,448 Points0 Answers
-
Linda de Haan
12,413 Points1 Answer
-
Mohmed Taha
1,427 PointsData Persistence won't run in Java 9.0.1 on IntelliJ due to errors in XML parsing and JAXB issues, please help
Posted by Mohmed TahaMohmed Taha
1,427 Points1 Answer
-
Chitra Sharathchandra
6,188 Points1 Answer
-
Iver Band
8,389 Points4 Answers
-
PLUS
Reeti Gupta
Courses Plus Student 3,400 Points1 Answer
-
Jani Eronen
5,767 PointsNot sure what I did wrong, but the the id of my contacts does not generate the way it should. It goes like: 1, 33,65,97
Posted by Jani EronenJani Eronen
5,767 Points4 Answers
View all discussions for this video
Related 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