hibernate 中的hql 语句累积
来源:互联网 发布:淘宝店铺招牌是哪里 编辑:程序博客网 时间:2024/05/22 12:04
1./只查询十条数据
@SuppressWarnings("unchecked")
@Overridepublic List<NewsInfoEntity> listHotNews() {
List<NewsInfoEntity> lst = new ArrayList<NewsInfoEntity>();
try {
String hql = "from NewsInfoEntity order by pubTime desc";
Query query = getHibernateTemplate().getSessionFactory().getCurrentSession().createQuery(hql);
query.setMaxResults(10); //取出十条记录
lst = (List<NewsInfoEntity>)query.list();
} catch (DataAccessException e) {
e.printStackTrace();
}
return lst;
}
2.我先先看例子。
@SuppressWarnings("unchecked")
@Override
public List<ProxyBidEntity> queryMaxPriceTwoDiffentAuctionItem(AuctionItemEntity ai, Double price) {
Date date=Calendar.getInstance().getTime();
String hql ="from ProxyBidEntity pb where pb.auctionItem.closed=false and pb.auctionItem=:auctionItem and pb.auctionItem.auctionEvent.startTime<=:date and pb.maxBid>=:price group by pb.maxBid order by pb.maxBid";
Query query = getHibernateTemplate().getSessionFactory().getCurrentSession().createQuery(hql);
query.setParameter("auctionItem", ai); //参数对象是 对象
query.setTimestamp("date", date); //参数对象是 时间类型
query.setParameter("price", price); //参数对象是 对象 我们可以看到setParameter这个方法第二个参数是对象 因而我们的基本类型都是对象
query.setFirstResult(0);
query.setMaxResults(2); //筛选返回结果的个数
List<ProxyBidEntity> lst = (List<ProxyBidEntity>)query.list();
return lst;
}
//这个语句的难点是group by 和 order by 同时使用 group by 在前, order by 在后
3.
anser :不等号用<>4. sql 中 选择全部 使用的是 * 不可以使用select *,但可以select+列名
在hql中全部使用 : select u from User u
5.hql like语句的使用
"from objectName o where o.attributeName like '%"+attrubuteValue+"%'"
这种写法是不安全的,如果attributeValue的值是 ' or 1=1 or 'a' like ' 的话就把所有的纪录全查出来了。而且有些值还可能破坏你的系统,比如调用数据库的函数之类。
最好还是用下面的写法
String hql = "from EnterpriseEntity e where e.nodePath like:nodePath";
Query query = em.createQuery(hql);
query.setParameter("nodePath", nodePath + "%");
List<EnterpriseEntity> lst = query.getResultList();
return lst;
- hibernate 中的hql 语句累积
- Hibernate中的HQL模糊查询语句
- hibernate查询语句--HQL
- hibernate查询语句--HQL
- hibernate查询语句--HQL
- Hibernate查询语句+HQL
- hibernate查询语句---HQL
- hibernate查询语句--HQL
- hibernate查询语句--HQL
- hibernate查询HQL语句
- hibernate--HQL查询语句
- Hibernate 中HQL语句
- Hibernate 中HQL语句
- hibernate -- HQL语句总结
- hibernate -- HQL语句总结
- Hibernate中HQL语句
- Hibernate HQL语句总结
- hibernate -- HQL语句总结
- js_撞球游戏
- Cocos2dx场景切换效果汇总
- 从一道百度面试题到分析输入url到页面返回的过程(或者查询返回过程)
- Romantic
- H面试程序(0):字符串一些常用函数的实现
- hibernate 中的hql 语句累积
- HDU 1009 需要细心的题
- android 打开 ppt pptx doc docx xls xlsx pdf office 文档
- Ubuntu的学习shutdown联想到Android 的进程守护
- s3c2440 arm+linux 开发板移植rt3070驱动笔记
- 访问:69811次 积分:1969分 排名:第5637名 原创:137篇转载:34篇译文:3篇评论:8条
- 关于android应用--内存的优化
- GCD Again
- 容斥原理与多重集合