hibernate 怎么删除前几条记录
publicvoiddeleteByCard_no(Stringid){hql="fromT_consumewherecard_no='"+id+"'orderbysta...
public void deleteByCard_no( String id )
{
hql="from T_consume where card_no ='"+id+"' order by start_time ";
try {
consume_list =dao.queryConsumeOrder(hql);
for(int i=0;i<consume_list.size();i++)
{
T_consume consume = new T_consume();
consume=(T_consume)consume_list.get(i);
dao.deleteObject(consume);
}
} catch (Exception e) {
e.printStackTrace();
}
}
public List<T_consume> queryConsumeOrder(String hql) throws Exception{
try {
Session session =this.getSessionFactory().openSession();
Query q=getSession().createQuery(hql);
q.setFirstResult(0) ;
q.setMaxResults(2);
List<T_consume> consume_list = q.list();
return consume_list;
} catch (Exception e) {
e.printStackTrace();
throw new Exception("查询数据库表中记录错误.");
}
} 展开
{
hql="from T_consume where card_no ='"+id+"' order by start_time ";
try {
consume_list =dao.queryConsumeOrder(hql);
for(int i=0;i<consume_list.size();i++)
{
T_consume consume = new T_consume();
consume=(T_consume)consume_list.get(i);
dao.deleteObject(consume);
}
} catch (Exception e) {
e.printStackTrace();
}
}
public List<T_consume> queryConsumeOrder(String hql) throws Exception{
try {
Session session =this.getSessionFactory().openSession();
Query q=getSession().createQuery(hql);
q.setFirstResult(0) ;
q.setMaxResults(2);
List<T_consume> consume_list = q.list();
return consume_list;
} catch (Exception e) {
e.printStackTrace();
throw new Exception("查询数据库表中记录错误.");
}
} 展开
1个回答
展开全部
LZ说的有点含模不清,hibernate 封装的delete()方法,该方法被重载,但均不能实现删除前几条
。hibernate可以执行sql语句,那么可以通过如下语句实现,当然LZ应该知道,但我还是写了一下
delete a where a.id in(select * from (select a.id from a order by a.date) where rownum<=5);
解释:
以上语名为,查出"a"表中日期离当前日期最近的(看需求可以不排序),然后取前5条,当然也可以是"N"条
。hibernate可以执行sql语句,那么可以通过如下语句实现,当然LZ应该知道,但我还是写了一下
delete a where a.id in(select * from (select a.id from a order by a.date) where rownum<=5);
解释:
以上语名为,查出"a"表中日期离当前日期最近的(看需求可以不排序),然后取前5条,当然也可以是"N"条
追问
谢谢,这个问题我已经解决了,是先查出前n条,再删。
追答
嗯
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询