问题
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