属性名不一致的对象深度克隆
来源:互联网 发布:毕向东java教程35天版 编辑:程序博客网 时间:2024/05/21 06:39
参考文档:
http://outofmemory.cn/code-snippet/2749/java-value-object-between-copy-tool-use-mirror-tongyong
http://www.cnblogs.com/bjzhanghao/archive/2004/07/30/28607.html
被克隆的类:MovieSourceTemp.java
public class MovieSourceTemp { private Integer source_id; private String source_sign; private String source_name; private Integer source_type; private String appcode; public Integer getSource_id() { return source_id; } public void setSource_id(Integer source_id) { this.source_id = source_id; } public String getSource_sign() { return source_sign; } public void setSource_sign(String source_sign) { this.source_sign = source_sign; } public String getSource_name() { return source_name; } public void setSource_name(String source_name) { this.source_name = source_name; } public Integer getSource_type() { return source_type; } public void setSource_type(Integer source_type) { this.source_type = source_type; } public String getAppcode() { return appcode; } public void setAppcode(String appcode) { this.appcode = appcode; } public MovieSource toSource() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException{ Class<?> movieSourceTempClass = MovieSourceTemp.class; Class<?> movieSourceClass = MovieSource.class; Field[] fields = movieSourceClass.getDeclaredFields(); MovieSource movieSource = new MovieSource(); for (Field field : fields) { Column column = field.getAnnotation(Column.class); if (column==null){ continue; } Field fieldMovieSourceTemp = null; try { fieldMovieSourceTemp = movieSourceTempClass.getDeclaredField(column.name()); } catch (NoSuchFieldException e) { //e.printStackTrace(); continue; } if (fieldMovieSourceTemp!=null){ //从movieSourceTempClass拿出来 String movieSourceTempMethodName =fieldMovieSourceTemp.getName().substring(0, 1).toUpperCase()+fieldMovieSourceTemp.getName().substring(1); String getMethodName = "get"+movieSourceTempMethodName; Method getMethod = movieSourceTempClass.getMethod(getMethodName); //set到movieSourceClass这里面去 String movieSourceMethodName = field.getName().substring(0, 1).toUpperCase()+field.getName().substring(1); String setMethodName = "set"+movieSourceMethodName; Method setMethod = movieSourceClass.getMethod(setMethodName,field.getType()); Object value = getMethod.invoke(this); setMethod.invoke(movieSource,value); } } return movieSource; }//需要克隆的类:MovieSource.java
@Entity@Table(name = "movie_source")@Level(Level.TYPE.PRIVATE)@org.hibernate.annotations.Entity(dynamicUpdate=true,dynamicInsert=true)public class MovieSource extends AbstractItem implements ItemBase { /** * */ private static final long serialVersionUID = 1L; public MovieSource (){} @Column( name = "source_id" , nullable=false ,length = 10) @Id private Integer sourceId; @Column( name = "source_sign" ,length = 50 ) private String sourceSign; @Column( name = "source_name" ,length = 50 ) private String sourceName; @Column( name = "source_type" ,length = 2 ) private Integer sourceType; @Column( name = "appcode" ,length = 50 ) private String appcode; public Integer getSourceId() { return sourceId; } public void setSourceId(Integer sourceId) { this.sourceId = sourceId; } public String getSourceSign() { return sourceSign; } public void setSourceSign(String sourceSign) { this.sourceSign = sourceSign; } public String getSourceName() { return sourceName; } public void setSourceName(String sourceName) { this.sourceName = sourceName; } public Integer getSourceType() { return sourceType; } public void setSourceType(Integer sourceType) { this.sourceType = sourceType; } public String getAppcode() { return appcode; } public void setAppcode(String appcode) { this.appcode = appcode; } }
0 0
- 属性名不一致的对象深度克隆
- JAVA对象的深度克隆
- JAVA对象的深度克隆
- JavaScript的对象深度克隆
- js对象的深度克隆!
- js对象的深度克隆
- js对象的深度克隆
- Js深度克隆对象(对象的属性含有对象数组)
- 第四讲 属性名和字段名不一致的处理
- 解决字段名和属性名不一致的方案
- 常见数据类型的克隆(内含对象的深度克隆)
- javascript中对象的深度克隆
- JavaScript中对象的深度克隆
- 深入JAVA对象深度克隆的详解
- javascript中对象的深度克隆
- javascript中对象的深度克隆
- JavaScript的对象深度克隆方法
- javaScript中对象的深度克隆
- 雏鹰开发附件1 – Spring相关Maven配置
- 控制流如何处理错误
- io原理
- too many connections 解决方法
- Centos7 安装rabbitmq
- 属性名不一致的对象深度克隆
- iOS 视频文件和图片文件缓存路径
- 我们,除了代码还有诗和远方
- 谈谈我对攻读计算机研究生的一些看法
- SQL Server 的本地时间和UTC时间
- 关注别人CSDN博客的方法
- spring cloud-ribbon
- 安装锁定文件INSTALL.LOCK的用途
- 【学习笔记03】java面向对象-封装