DAO层的持久化常用代码


 
 
保存方法
public ContentArea save(ContentArea bean) {
		getSession().save(bean);
		return bean;
	}



删除方法


@Override
	public int delete(Map<String, Object> map) {
		if (map == null)
			return 0;
		String className = getEntityClass().getName();
		String hql = ReferenceHqlUtil.getDeleteSql(className, map);
		Query query = ReferenceHqlUtil.getQuery(getSession(), map, hql);
		int result = query.executeUpdate();
		return result;
	}
@Override
 public ContentArea deleteById(int id) {
 ContentArea entity = super.get(id);
 if (entity != null) {
 getSession().delete(entity);
 }
 return entity;
 }

	@Override
	public void deleteByAId(int areaId) {
		Finder f = Finder.create("select bean from ContentArea bean where bean.areaId='" + areaId + "'");
		Query query = getSession().createQuery(f.getOrigHql());
		f.setParamsToQuery(query);
		List<ContentArea> list = query.list();
		if (list != null)
			for (ContentArea xpl : list) {
				getSession().delete(xpl);
			}

	}


获取List

@Override
	public List<RefArea> findByContentId(int contentId) {
		// String
		// hql="select t1 from RefArea t1 , ContentArea t2  where  t1.areaId =t2.areaId and t2.contentId=:contentId order by t2.priority";
		String hql = "select distinct t1 from RefArea t1 ,ContentArea t2 where t1.areaId =t2. areaId and  t2.contentId=:contentId order by t2.priority";
		Query query = getSession().createQuery(hql);
		query.setParameter("contentId", contentId);
		List<RefArea> list = query.list();
		return list;
	}

	public ContentArea getByContentIdAndObjId(int AreaId, int contentId) {
		String hql = "select bean from ContentArea bean where bean.contentId=:contentId and bean.areaId=:areaId";
		Query query = getSession().createQuery(hql);
		query.setParameter("contentId", contentId);
		query.setParameter("areaId", AreaId);
		return (ContentArea) query.uniqueResult();
	}


你可能感兴趣的:(DAO层的持久化常用代码)