Search code examples
javahibernatecreatecriteria

Can I select specific columns with Hibernate "createCriteria()"?


I used createCriteria() and setFetchMode() methods to join and select. I don't need all columns so I want to select specific columns to improve performance, but I can't find how to do it. Maybe should I use HQL instead?


Solution

  • You will need to set Projection on specific properties.

    Example:

     criteria.setProjection( Projections.projectionList()
        .add( Projections.property("cat.name"), "catName" )
        .add( Projections.property("kit.name"), "kitName" )
     );