我有以下标准请告知我如何将以下标准转换为HQL,因为我想使用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;
}
也请在上面的标准中让我知道什么是错的,因为现在它正在获取对象的所有属性,并且我需要花费大量时间,我认为我的预测实现有问题。
答案 0 :(得分:0)
也许是这样的
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;
}
答案 1 :(得分:0)
您可以在对象tttBook
中创建另一个构造函数。此外,您应该遵循命名约定并以大写字母和小写字母属性开始调用类。
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
}
查询
List<TttBook > list = (List<TttBook >) session.createQuery("select
new yourpath.TttBook(id, longName) from TttBook").list();