Conversion of below criteria into HQL

无人久伴 提交于 2019-12-11 16:24:35

问题


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.


回答1:


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();



回答2:


maybe something like this

public List<tttBook> findtooks() {
    Session s = HiberUtil.getSessionFactory().openSession();
    List<tttBook> tooks =new ArrayList<tttBook>;
    List result = session.createQuery("select a.Id, a.longName from tttBook t").List();
    for(int i =0; i< result.size();i++){
        Object[] objects = result.get(i);
        tttBook t = new tttBook();
        t.setId(objects[0]);
        t.setLongName(objects[1]);
    tooks.add(t);
    }

    return tooks;

}


来源:https://stackoverflow.com/questions/18040684/conversion-of-below-criteria-into-hql

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!