public static void copyProperties(java.lang.Object dest, java.lang.Object orig) throws java.lang.IllegalAccessException, java.lang.reflect.InvocationTargetExceptioCopy property values from the origin bean to the destination bean for all cases where the property names are the same.
/**//* * Created on 2006-4-26 */ package com.util; import java.beans.PropertyDescriptor; import java.util.Collection; import org.apache.commons.beanutils.PropertyUtils; /***//** * CopyUtil * @author Jkallen */ publicclass CopyUtil { /***//** * Copy properties of orig to dest * Exception the Entity and Collection Type * @param dest * @param orig * @return the dest bean */ publicstatic Object copyProperties(Object dest, Object orig) { if (dest ==null|| orig ==null) { return dest; } PropertyDescriptor[] destDesc = PropertyUtils.getPropertyDescriptors(dest); try{ for (int i =0; i < destDesc.length; i++) { Class destType = destDesc[i].getPropertyType(); Class origType = PropertyUtils.getPropertyType(orig, destDesc[i].getName()); if(destType !=null&& destType.equals(origType) &&!destType.equals(Class.class)) { if(!Collection.class.isAssignableFrom(origType)){ try{ Object value = PropertyUtils.getProperty(orig, destDesc[i].getName()); PropertyUtils.setProperty(dest, destDesc[i].getName(), value); }catch(Exception ex){ } } } } return dest; }catch(Exception ex) { thrownew CopyException(ex); // return dest; } } /***//** * Copy properties of orig to dest * Exception the Entity and Collection Type * @param dest * @param orig * @param ignores * @return the dest bean */ publicstatic Object copyProperties(Object dest, Object orig, String[] ignores) { if (dest ==null|| orig ==null) { return dest; } PropertyDescriptor[] destDesc = PropertyUtils.getPropertyDescriptors(dest); try{ for (int i =0; i < destDesc.length; i++) { if (contains(ignores, destDesc[i].getName())) { continue; } Class destType = destDesc[i].getPropertyType(); Class origType = PropertyUtils.getPropertyType(orig, destDesc[i].getName()); if(destType !=null&& destType.equals(origType) &&!destType.equals(Class.class)) { if(!Collection.class.isAssignableFrom(origType)){ Object value = PropertyUtils.getProperty(orig, destDesc[i].getName()); PropertyUtils.setProperty(dest, destDesc[i].getName(), value); } } } return dest; }catch(Exception ex) { thrownew CopyException(ex); } } staticboolean contains(String[] ignores, String name) { boolean ignored =false; for (int j =0; ignores !=null&& j < ignores.length; j++) { if (ignores[j].equals(name)) { ignored =true; break; } } return ignored; } }
可以看到,在范例1中通过方法copyProperties的时候,二者之间在的属性名必须相同(Copy property values from the origin bean to the destination bean for all cases where the property names are the same)。而在范例2中通过
Object value = PropertyUtils.getProperty(orig, destDesc[i].getName());
PropertyUtils.setProperty(dest, destDesc[i].getName(), value);
也是将源与目的之间copy相同的属性名。而VO是在前台显示,所以难免会用到PO中所不存在的属性值。比如PO中可能是一个对象,而VO中则可能是此对象的全部属性。其中的一些转换则需要依据前台需 要针对性地处理啦!
在copy的过程中,若实体中存在一对多,多对多等关系,则DTO中也应该存在此关系,此时不能直接将内部的DTO or List(一对多)里面的数据一次性全部拷过去,这时应该对每个DTO进行copy,当然,若在前台你无需用到相关的DTO则可以跳过。而对于一对多(LIST),而应将实体里面的每个一对多转换成对应的DTO,再依次放到LSIT里面,再将此LIST赋值给(父)DTO,这里面的关系如同(一个简单的)递归关系。---更新于5-25-2006