Search code examples
hibernatehqlcriteria

Conversion of below criteria into HQL


I have the following criteria please advise how can I convert the below criteria into HQL , as I want to use HQL

public List<tttBook> findtooks() {

            List<tttBook> tooks =null;
            Criteria criteria = session.createCriteria(tttBook.class);
            ProjectionList proList = Projections.projectionList();
            proList.add(Projections.property("Id"));
            proList.add(Projections.property("longName"));
            tooks = criteria.list();
            return tooks;

        }

also please let me know in this above criteria what is wrong since right now it is fetching all the attributes of the object and it takes lots of time i think there is something wrong with my projections implementation.


Solution

  • You can create another constructor in your object tttBook. Also you should follow naming convention and call class starting with capital letter and properties with small letter.

    package yourpath;
    
    public class TttBook {
       private Long id;
       private String longName;
    
       public TttBook(Long id, String longName) {
          this.id = id;
          this.longName = longName;
       }
    
       // getters, setters
    }
    

    Query

    List<TttBook > list = (List<TttBook >) session.createQuery("select 
       new yourpath.TttBook(id, longName) from TttBook").list();