hibernate4 Oracle executeQuery()方法执行删除无法删除数据库数据

 

项目开发中,使用的hibernate oracle删除数据时使用以下代码不能删除数据

              public void delPerson(String name) {
                         Session session = HibernateUtil.currentSession();
		String hql = "delete from Person where name= :name";
		Query query = session.createQuery(hql);
		query.setParameter("name", name);
		return query.executeUpdate();
              }

 改为类似下面的写法就行了

 

 

public void delPerson(Date createDate ) {
		Session session = HibernateUtil.currentSession();
		String time = DateUtil.convertDate2Str(createDate, "yyyy-MM-dd");
		String sql = "select id from person where to_char(trunc(tax_date, 'dd'), 'yyyy-mm-dd') = :taxDate";
		SQLQuery query = session.createSQLQuery(sql);
		query.setParameter("taxDate", time);
		List list = query.list();
		Iterator iterator = list.iterator();
		List<String> idList = new ArrayList<String>();
		while (iterator.hasNext()) {
			String obj = (String) iterator.next();
			idList.add(obj);
		}
		Person taxBean = new Person();
		for(String id : idList) {
			taxBean.setId(id);
			session.delete(taxBean);
			session.flush();//此处相当于提交一次,否则循环执行完后只会删除一条数据
		}
	}

 

 

java学习交流群:513650703

你可能感兴趣的:(hibernate4 Oracle executeQuery()方法执行删除无法删除数据库数据)