java - Is there a way to map a Set to a List with BeanUtils.copyProperties? -


I'm using BeanUtils.copyProperties to convert two beans

Beanutils. Copproperties (organization, deto organization);

I have to do a list in one bean and set one to in the other.

First Been:

  Public Category Form {Private Set & lt; Organization & gt; Organization; }  

Second bean:

  Public final category DTOForm {Private list & lt; DTOOrganization & gt; Organization; }  

The result is an exception as described here:

Is it possible to optimize BeanUtils.copyProperties ?

You can solve it by using custom converters. The main idea is that set <

ListEntity, which has list

/ Code> Property (as far as I do not ignore the setters and gates):

  Public Class List {Private list & lt; Integer & gt; Col = new arreelist & lt; & Gt; (); Public listing & lt; Integer & gt; GetCol () {Return Colonel; } Public Zero Set Call (list & gt; integer & gt; col) {this.col = col; Set}}  SetEntity, which has  set  property:  

  public class SetEntity {private set} & Lt; Integer & gt; Col = new hashset & lt; & Gt; (); Public set & lt; Integer & gt; GetCol () {Return Colonel; } Public Zero Set Call (set & lt; integer & gt; col) {this.col = col; }}  

Simple test class to work:

  public class exam {public static zero major (string. Ars.) Throws InvocationTargetException, IllegalAccessException {SetEntity se = New SetEntity (); Se.getCol () add (1). Se.getCol () add (2). ListEntity le = New ListEntity (); ConvertUtility Register (new converter) {@ Override public & lt; T & gt; T convert (class  tClass, object o) {list list = new arreelist & lt; & Gt; (); Iterator = ((set) o). Iterator (); While (this haas next ()) {list.add (it.next ());} return (t) list;}}, list Class); BeanUtils.copyProperties (from, to); Println (. Se.getCol () toString ()); Println (. Le.getCol () toString ()); }}  

The main idea of ​​this code sniper: We register converter for all the destination class List properties, this is some object o To list . Assuming that o is a collection, we repeat it and then return the newly created list.

As a result, both 1 and 2 in le If you do not need this converter anymore, You can unregister by using


Comments

Popular posts from this blog

winforms - C# Form - Property Change -

c# - NewtonSoft JArray - how to select multiple elements with LINQ -

javascript - amcharts makechart not working -