dao层extends BaseDaoJpa修改数据设为只读不做更新

来源:互联网 发布:怎么使用vim编译c语言 编辑:程序博客网 时间:2024/05/16 10:59

因为要在前台将特定数字改为字符串字段,在前台数据量太大不能在前台将数据进行替换,性能消耗太大,所以只能在后台处理,然而问题在这里:

问题一:将数据进行替换时设置字段值时数据库里面的数据也更新了,而我们的要求是特殊字段显示转换字符串,数据库的数据保持不变,

                试过好多种方法找到一条最简单实用 的方法解决这个问题

               即: session.setReadOnly(item, true);设为只读就解决了这个问题,只读取不做更新

public PageListHelper searchByArgs(PageListHelper page,Map<String, Object> argMap) {List args = new ArrayList();StringBuffer sb = new StringBuffer();StringBuffer countsb = new StringBuffer();String argSql = toArgSql(argMap);String sql = "select a from Item a  where a.deleted = 0 " + argSql;sb.append(sql);countsb.append("select count(a) from Item a where a.deleted = 0 ");countsb.append(argSql);sb.append(" order by publisherTime desc ");Query q = this.entityManager.createQuery(sb.toString());Query countQuery = this.entityManager.createQuery(countsb.toString());for (int i = 0; i < args.size(); i++) {q.setParameter(i + 1, args.get(i));countQuery.setParameter(i + 1, args.get(i));}Long count = (Long) countQuery.getSingleResult();q.setFirstResult(page.getStartPosition());q.setMaxResults(page.getObjectsPerPage());page.setFullListSize(Integer.parseInt(count.toString()));List<Item> itemList = q.getResultList();try {Session session = (Session) this.entityManager.getDelegate();for (int i = 0; i < itemList.size(); i++) {Item item = itemList.get(i);// 设置上牌地if (StringUtils.isNotBlank(item.getCarLicenseLocate())) {String provinceSql = "select name from province where code="+ item.getCarLicenseLocate().split("-")[0];org.hibernate.Query provinceQuery = session.createSQLQuery(provinceSql);String proviceName = provinceQuery.uniqueResult().toString();String zxSql = "select zx_flag from province where code="+ item.getCarLicenseLocate().split("-")[0];org.hibernate.Query zxQuery = session.createSQLQuery(zxSql);String zx = zxQuery.uniqueResult().toString();if ("1".equals(zx)) {//item.setCarLicenseLocate(proviceName);item.setCarLicenseLocateFormat(proviceName);} else {// 得到cityString citySql = "select name from city where code="+ item.getCarLicenseLocate().split("-")[1];org.hibernate.Query cityQuery = session.createSQLQuery(citySql);String cityName = cityQuery.uniqueResult().toString();item.setCarLicenseLocateFormat(proviceName + cityName);}/* * // 得到province String provinceSql = * "select name from province where code=" + * item.getCarLicenseLocate().split("-")[0]; * org.hibernate.Query provinceQuery = session * .createSQLQuery(provinceSql); String proviceName = * provinceQuery.uniqueResult() .toString(); // 得到city * String citySql = "select name from city where code=" + * item.getCarLicenseLocate().split("-")[1]; * org.hibernate.Query cityQuery = session * .createSQLQuery(citySql); String cityName = * cityQuery.uniqueResult().toString(); * item.setCarLicenseLocate(proviceName + " " + cityName); */}// 设置颜色if (StringUtils.isNotBlank(String.valueOf(item.getCarColor()))) {// detail_dic_nameString colorNameSql = "select t.detail_dic_name from (select * from sys_dic_item "+ "where dic_parent_code = 'color')as t where t.detail_dic_code='"+ item.getCarColor() + "'";org.hibernate.Query colorQuery = session.createSQLQuery(colorNameSql);String colorName = colorQuery.uniqueResult().toString();item.setCarColor(colorName);}session.setReadOnly(item, true);}page.setList(itemList);} catch (Exception e) {e.printStackTrace();log.error(e);}return page;}


0 0
原创粉丝点击