因为对于产品分类的删除操作并不是真正的物理删除,只是将它的visible属性设置为不可见。所以,必须重写删除方法。
ProductTypeService接口
package com.charlie.shop.service;
import com.charlie.shop.dao.base.BaseDao;
import com.charlie.shop.domain.product.ProductType;
public interface ProductTypeService extends BaseDao<ProductType>{
}
ProductTypeServiceImpl类
package com.charlie.shop.service.impl;
import org.hibernate.Query;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.charlie.shop.dao.base.BaseDaoImpl;
import com.charlie.shop.domain.product.ProductType;
import com.charlie.shop.service.ProductTypeService;
@Service("productTypeService")
@Transactional
public class ProductTypeServiceImpl extends BaseDaoImpl<ProductType> implements ProductTypeService{
/**
* 重写删除方法,删除时并不真正做物理删除,只是设为不可见即可。
* 思路:利用hql语句 update ProductType t set t.visible=? where t.id in (?,?,?,?)
* 上面hql语句中最后部分(?,?,?,?),问号个数决定于形参ids的长度
*/
@Override
public void delete(Long... ids) {
if(ids==null||ids.length==0){
return;
}
StringBuffer hql = new StringBuffer();
//拼接hql语句
hql.append("update ProductType t set t.visible=? where t.id in (");
for(int i =0;i<ids.length;i++){
if(i<ids.length-1){
hql.append("?,");
}else{
hql.append("?)");
}
}
//创建Query对象
Query query = getSession().createQuery(hql.toString());
//设置Query的参数
query.setBoolean(0, false);
for(int i=1;i<=ids.length;i++){
query.setLong(i, ids[i-1]);
}
//执行hql
query.executeUpdate();
}
}