⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 compositeentityutils.java

📁 《j2ee开发全程实录》随书源码
💻 JAVA
字号:
package com.cownew.PIS.framework.bizLayer;

import java.util.Iterator;
import java.util.Set;

import org.hibernate.Session;

import com.cownew.PIS.framework.common.IBaseDAO;
import com.cownew.PIS.framework.common.IValueObject;
import com.cownew.PIS.framework.common.db.Selectors;

import com.cownew.PIS.framework.server.helper.ThreadVariableManager;
import com.cownew.ctk.common.PropertyUtils;

public class CompositeEntityUtils
{
	/**
	 * 删除已经被删除的子对象,因为hibernate对从集合中删除的子对象只会删除父子之间的关系,并没有删除数据库中的子对象字段
	 * 因此我们必须手动删除
	 * @param daoIntf  父对象dao接口
	 * @param newVO 更新以后的值对想,通常是update(IValueObject valueObject) 中传入的valueObject
	 * @param childProperty 要删除的子对象在父对象中的的属性
	 * @
	 */
	public static void updateChild(IBaseDAO daoIntf, IValueObject newVO,
			String childProperty) 
	{
		Session session = ThreadVariableManager.getInstance().getSession();

		Selectors selectors = new Selectors();
		selectors.add(childProperty);
		IValueObject oldVO = daoIntf.loadByPK(newVO.getId(), selectors);

		Set oldSet = (Set) PropertyUtils.getProperty(oldVO, childProperty);
		Set newSet = (Set) PropertyUtils.getProperty(newVO, childProperty);
		
		//如果集合为null,说明selectors没有取出改属性,所以就不处理
		if(oldSet==null||newSet==null)
		{
			return;
		}

		Iterator oldIt = oldSet.iterator();
		while (oldIt.hasNext())
		{
			IValueObject oldInfo = (IValueObject) oldIt.next();
			if (!exists(oldInfo, newSet))
			{
				session.delete(oldInfo);
			}
		}
	}

	/**
	 * vo是否存在set中(id相等)
	 * @param vo
	 * @param set
	 * @return
	 */
	private static boolean exists(IValueObject vo, Set set)
	{
		Iterator it = set.iterator();
		while (it.hasNext())
		{
			IValueObject info = (IValueObject) it.next();
			// 因为set中存储的是新对象,所以可能id为空,所以必须这么判断,不能info.getId().equals(vo.getId())
			if (vo.getId().equals(info.getId()))
			{
				return true;
			}
		}
		return false;
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -