Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Java Spring with Hibernate Integrating Hibernate with Spring Fetching Data with Hibernate in Spring

zlatkap
zlatkap
21,494 Points

Explain Concept of creating entity of Class with .class - in this case Category.class

I still do not get how to use it and where what what is the concept or what does this do specifically. in this case (List<Category> categories = session.createCriteria(Category.class).list();) .... I saw it in so many videos and tutorials and still not getting it - what is the specific role of Category.class and what does it represents?

1 Answer

Well, it's about the Criteria API and they could probably make five different courses on the subject and they still wouldn't have covered it.

But here's the short version: The Criteria API is one of the components of the Java Persistence API (JPA) which defines how a Java app and a database should work together. Java only knows about objects, while the database has tables with rows that can be queried with SQL. Criteria is one of those things that build bridges between the two. With Criteria, you can build a query using Java objects, and it will transform it into a valid SQL query in the background. Then it will take the result of the SQL query (a bunch of rows from a database table) and turn them back into Java objects that you can use in your code.

The line that you quote basically just sends a query to the database to get all the data from the category table, then turn them into Category objects and put them into a Java List. You pass it the Category.class as a parameter to say that you want Java objects of the type Category. Then Hibernate does the rest of the job: from the configuration of your app, it knows how Category objects are mapped to the database.

You might also encounter this updated version of the code, which looks more complicated but does exactly the same, only with the latest version of Hibernate and JPA:

public List<Gif> findAll() {
        Session session = sessionFactory.openSession();

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

        // Create CriteriaQuery
        //Here you create a new query. The "Gif.class" means you want the query to return Gif objects
        CriteriaQuery<Gif> criteria = builder.createQuery(Gif.class);

        //Specify the root of the query, the entity that's the basis for the query. 
        //Here you just want all Gifs from the gif table, so it's obvious, but it can be more complicated with complex queries
        criteria.from(Gif.class);

        // Execute the query, then put all the data in a Java List
        List<Gif> gifs = session.createQuery(criteria).getResultList();

        session.close();
        return gifs;
    }

Honestly, I tried to understand how Criteria really works beyond the super simple case above, and it's pretty overwhelming. You won't encounter Criteria in Treehouse courses beyond what I just told you about, though. There are other ways to query data from the database. One of them is so called "Typed Queries" with JPQL. It has its drawbacks but is definitely easier. And in your next Spring course I believe you'll meet Spring Data JPA, which makes querying data so easy you almost forget there's a database somewhere in the background.