集合到数组的映射
以下的集合会Dozer 会自动处理
- List to List
- List to Array
- Array to Array
- Set to Set
- Set to Array
- Set to List
使用建议的集合映射关系:
Dozer 会聪明的自己去对集合进行类之间的复制, 但还是建议在xml 文件中明确的写出来
在进行Array Set 和List 之间转换的时候, 多少还是会发生一些故障, 不管你是否明确的写出了映射:
- List to List
- Dest Hint req'd: NO
- Dest Hint allowed: YES
- If no dest hint specified: Dest list will contain the same data types in the source
- If hint is speficied: Dest list will contain objects that match dest hint(s) type
- Array to List
- Dest Hint req'd: NO
- Dest Hint allowed: YES
- If no dest hint specified: Dest list will contain the same data types in the source
- If hint is speficied: Dest list will contain objects that match dest hint(s) type
- List to Array
- Dest Hint req'd: NO
- Dest Hint allowed: YES
- If no dest hint specified: Dest array will contain data types defined by the array
- If hint is speficied: Dest list will contain objects that match dest hint(s) type (only if Object Array)
- Array to Array
- Dest Hint req'd: NO
- Dest Hint allowed: YES
- If no dest hint specified: Dest array will contain data types defined by the array
- If hint is speficied: Dest list will contain objects that match dest hint(s) type (only if Object Array)
- Set to Set
- Dest Hint req'd: NO
- Dest Hint allowed: YES
- If no dest hint specified: Dest list will contain the same data types in the source
- If hint is speficied: Dest list will contain objects that match dest hint(s) type
- Array to Set
- Dest Hint req'd: NO
- Dest Hint allowed: YES
- If no dest hint specified: Dest list will contain the same data types in the source
- If hint is speficied: Dest list will contain objects that match dest hint(s) type
- Set to Array
- Dest Hint req'd: NO
- Dest Hint allowed: YES
- If no dest hint specified: Dest array will contain data types defined by the array
- If hint is speficied: Dest list will contain objects that match dest hint(s) type (only if Object Array)
- List to Set
- Dest Hint req'd: NO
- Dest Hint allowed: YES
- If no dest hint specified: Dest list will contain the same data types in the source
- If hint is speficied: Dest list will contain objects that match dest hint(s) type
- Set to List
- Dest Hint req'd: NO
- Dest Hint allowed: YES
- If no dest hint specified: Dest list will contain the same data types in the source
- If hint is speficied: Dest list will contain objects that match dest hint(s) type
Using JDK 1.5 Generics for Collection Mapping
JDK 15 的泛型映射
如果你使用的是jdk1.5 以上的话,dozer 会自动的识别参数化过得映射, 比如
public class UserGroup {
private Set<User> users;
public Set<User> getUsers() {
return users;
}
public void setUsers(Set<User> aUsers) {
users = aUsers;
}
}
public class UserGroupPrime {
private List<UserPrime> users;
public List<UserPrime> getUsers() {
return users;
}
public void setUsers(List<UserPrime> aUsers) {
users = aUsers;
}
}
当执行此类转换的时候, 需要在List 中指定具体的类型, 即用List<> 方式构造
<field>
<a>arrayForLists</a><!-- changing an Integer [] to List and back again -->
<b>listForArray</b>
</field>
以下的例子会把 Integer []arrayForLists 转换为List<String>listForArray
<field>
<a>arrayForLists</a><!-- changing an Integer [] to List and back again -->
<b>listForArray</b>
<b-hint>java.lang.String</b-hint> <!— 注意看这段 -->
</field>
Primitive Array to Primitive Array (bi-directional)
基本类型的数组to 基本类型的数组
<field>
<a>anArray</a> <!-- converting int[] to int [] by name only -->
<b>theMappedArray</b>
</field>
Cumulative vs. Non-Cumulative List Mapping (bi-directional)
If you are mapping to a Class which has already been initialized, dozer will either 'add' or 'update' objects to your List. If your List or Set already has objects in it dozer checks the mapped List, Set, or Array and calls the contains() method to determine if it needs to 'add' or 'update'. This is determined using the relationship-type attribute on the field tag. The default is 'cumulative'. relationship-type can be specifed at the field mapping, class mapping, or global configuration level.
Dozer 非常的聪明, 他还支持将一个集合的内容添加或者更新到另外一个集合当中, 只是需要在xml 文件当中指定relationship-type 的值(non-cumulative| cumulative) dozer 是根据contains() 这个方法去判断的, 如果使用了relationship-type 这个属性, 那么默认就是'cumulative' 的
global configuration level....
<mappings>
<configuration>
<relationship-type>non-cumulative</relationship-type>
</configuration>
</mappings>
class mapping level....
<mappings>
<mapping relationship-type="non-cumulative">
<class-a>org.dozer.vo.TestObject</class-a>
<class-b>org.dozer.vo.TestObjectPrime</class-b>
<field>
<a>someList</a>
<b>someList</b>
</field>
</mapping>
</mappings>
field mapping level....
<field relationship-type="cumulative">
<a>hintList</a> <!-- objects will always be added to an existing List -->
<b>hintList</b>
<a-hint>org.dozer.vo.TheFirstSubClass</a-hint>
<b-hint>org.dozer.vo.TheFirstSubClassPrime</b-hint>
</field>
<field relationship-type="non-cumulative">
<a>unequalNamedList</a> <!-- objects will updated if already exist in List, added if they are not present -->
<b>theMappedUnequallyNamedList</b>
</field>
Orphans are elements which exist in a destination collection that did not exist within the source collection. Dozer will remove orphans by calling the 'remove' method on actual orphans of the underlying destination collection; it will not clear all. To determine elements which are orphans dozer uses the contains() method to check if the results contains orphans. The default is false.
移除孤立的字段的配置..( 英文这么多, 我写的就这么多, 精辟啊:) )
<field remove-orphans="true">
<a>srcList</a>
<b>destList</b> <!-- orphan objects will always be removed from an existing destination List -->
</field>