栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

使用Hibernate使用瞬态对象更新持久对象

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

使用Hibernate使用瞬态对象更新持久对象

不,Hibernate(或JPA)没有提供现成的功能,但是使用JavaBeans机制(或在其之上提供抽象层的许多库之一)并不难实现。

使用纯Javabeans自省

这是一种方法,该方法使用标准JavaBeans 机制从中复制所有属性到

beanA
beanB
如果它们为null
beanB
Introspector

public static void copyBeanProperties(    final Object beanA, final Object beanB){    if(beanA.getClass() != beanB.getClass()){        // actually, this may be a problem, because beanB may be a        // cglib-created subclass        throw new IllegalArgumentException();    }    try{        for( final PropertyDescriptor pd : Introspector   .getBeanInfo(beanB.getClass(), Object.class)   .getPropertyDescriptors()){ if(pd.getReadMethod().invoke(beanB)==null){     pd.getWriteMethod().invoke(beanB,         pd.getReadMethod().invoke(beanA)     ); }        }    } catch(IntrospectionException e){        throw new IllegalStateException(e);    } catch(IllegalAccessException e){        throw new IllegalStateException(e);    } catch(InvocationTargetException e){        throw new IllegalStateException(e);    }}

当然,这只是一个快速而肮脏的实现,但这应该可以帮助您入门。

使用Apache Commons / BeanUtils

这是一个使用Commons /
BeanUtils的
优雅版本。它隐藏了您的反射,并提供基于地图的属性访问:

public static void copyBeanProperties(final Object beanA, final Object beanB){    try{        @SuppressWarnings("unchecked") // this should be safe        final Map<String, Object> beanAProps = PropertyUtils.describe(beanA);        @SuppressWarnings("unchecked") // this should be safe        final Map<String, Object> beanBProps = PropertyUtils.describe(beanB);        if(!beanAProps.keySet().containsAll(beanBProps.keySet())){ throw new IllegalArgumentException("Incompatible types: "     + beanA + ", " + beanB);        }        for(final Entry<String, Object> entryA : beanAProps.entrySet()){ if(beanBProps.get(entryA.getKey()) == null){     PropertyUtils.setMappedProperty(beanB, entryA.getKey(),         entryA.getValue()); }        }    } catch(final IllegalAccessException e){        throw new IllegalStateException(e);    } catch(final InvocationTargetException e){        throw new IllegalStateException(e);    } catch(final NoSuchMethodException e){        throw new IllegalStateException(e);    }}

使用Spring的BeanWrapper

这是使用Spring

BeanWrapper
接口
的另一个版本(最不冗长,因为Spring在所有反射之上都提供了抽象,并且它是自己的异常处理),但是不幸的
BeanWrapper
是,该技术仅适用于Spring
IOC容器(当然,如果您不使用容器):

public static void copyBeanProperties(final Object beanA, final Object beanB){    final BeanWrapper wrapperA = new BeanWrapperImpl(beanA);    final BeanWrapper wrapperB = new BeanWrapperImpl(beanB);    try{        for(final PropertyDescriptor descriptor : wrapperB .getPropertyDescriptors()){ final String propertyName = descriptor.getName(); if(wrapperB.getPropertyValue(propertyName) == null){     wrapperB.setPropertyValue(propertyName,         wrapperA.getPropertyValue(propertyName)); }        }    } catch(final  InvalidPropertyException e){        throw new IllegalArgumentException("Incompatible types: " + beanA + ", " + beanB, e);    }}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/417618.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号