所以我有一個問題,這裏的某個人可能能夠幫助我與推土機。推土機沒有正確映射收集
背景:我有推土機設置映射我的持久性實體到他們的DTO類。這非常簡單,我只是創建了一個POJO實體類的完全副本,並允許推土機通配符來查看該字段的名稱與源字段相匹配。我正在用here來處理自定義映射程序的休眠加載問題。我告訴Dozer如何通過一個類來映射每個類,該類掃描實體中名爲@EntityMapping(DTOxxx.class)的註釋。然後將其添加到映射器addMapping(製造商)
問題:(閱讀研究在年底,最新的信息,但它會幫助通過閱讀來獲取上下文這一切太) 的問題在於Dozer在某些情況下沒有正確地映射我的集合。例如,在我的CategoryEntity類中,我有一個Dozer需要映射的其他實體的集合。會發生什麼情況是推土機發現在這種情況下具有2個項目的集合,並且僅在新的DTO類集合中映射1個項目。
正如你可以toDomain後的圖像被稱爲在看到(這有mapper.map(源,desination)在它推土機調用)的DTO只有它應該具有2名對象的1從實體映射到它。這裏是你想看到它的toDomain方法包住:
@Transactional(readOnly=true)
public <T extends DomainObject> T toDomain(Class<T> clazz, Entity entity) {
if (entity == null) {
return null;
}
T domain = getCachedDomainObjects(clazz, entity.getId());
if (domain == null) {
domain = dozerMapper.map(entity, clazz);
cacheDomainObject(domain);
}
return domain;
}
我已經確定,如果你認爲它不是抓住一個緩存的實體。
所以我有點難以理解爲什麼在某些情況下會發生這種情況,而不是在其他情況下。在它工作的場合以及無法使用的場合,我看不到任何明顯的差異。如果有人遇到過這樣的問題,並認爲他們能夠幫助我,那太棒了!這裏是我的類都是從問題的例子:
CategoryEntity.java:
@EntityMapping(Category.class)
@javax.persistence.Entity(name = "categories")
public class CategoryEntity implements Entity, PureTable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(unique = true, nullable = false)
private int id = Entity.UNSAVED_ID;
@OneToMany(mappedBy = "pk.category", fetch = FetchType.LAZY)
@Cascade({CascadeType.SAVE_UPDATE})
private Set<IncidentJoinCategoryEntity> incidentJoinCategories =
new HashSet<IncidentJoinCategoryEntity>();
@Override
public int getId() {
return this.id;
}
public void setId(int id) {
this.id = id;
}
public Set<IncidentJoinCategoryEntity> getIncidentJoinCategories() {
return incidentJoinCategories;
}
public void setIncidentJoinCategories(Set<IncidentJoinCategoryEntity>
incidentJoinCategories) {
this.incidentJoinCategories = incidentJoinCategories;
}
}
這個類有完全匹配其值的DTO類:
Category.java:
public class Category {
int id;
Set<IncidentJoinCategory> incidentJoinCategories=
new HashSet<IncidentJoinCategory>();
@Override
public int getId() {
return id;
}
@Override
public void setId(int id) {
this.id = id;
}
public Set<IncidentJoinCategory> getIncidentJoinCategories() {
return incidentJoinCategories;
}
public void setIncidentJoinCategories(Set<IncidentJoinCategory>
incidentJoinCategories) {
this.incidentJoinCategories = incidentJoinCategories;
}
}
!!!!!!!!!!!!!!!!!!!!!!!!!! !!!!!!!!!研究!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
編輯#1:
好吧!所以我花了幾個小時來調試這個問題,以瞭解這裏發生了什麼。原來問題出現在MappingProcessor類的第749行(Dozer 5.4.0)或766行的最新消息來源(但我沒有檢查這是否仍然是最新消息來源中的問題,但懷疑它是固定的)。
此行是
((Set) field).addAll(result);
它是什麼,試圖在這裏映射是
HashSet<IncidentJoinCategoryEntity>
則addAll(結果)僅增加1項至第((集)字段)的集合。其中有2個項目的結果(在調試過程中,它也是大小2,我將提供我的變量快照)僅向((Set)字段)添加1個值。
result LinkedHashSet<E> (id=220)
map LinkedHashMap<K,V> (id=248)
accessOrder false
entrySet HashMap$EntrySet (id=251)
hashSeed -1187793029
header LinkedHashMap$Entry<K,V> (id=253)
keySet HashMap$KeySet (id=5829)
loadFactor 0.75
modCount 2
size 2
table HashMap$Entry<K,V>[16] (id=258)
threshold 12
useAltHashing false
values null
field HashSet<E> (id=221)
map HashMap<K,V> (id=247)
entrySet HashMap$EntrySet (id=5856)
hashSeed 1372273954
keySet HashMap$KeySet (id=5821)
loadFactor 0.75
modCount 2
size 1
table HashMap$Entry<K,V>[16] (id=5822)
threshold 12
useAltHashing false
values null
EDIT#2:
下載的源獲得更多的調試:
if (field == null) {
Class<? extends Set<?>> destSetType = (Class<? extends Set<?>>) fieldMap.getDestFieldType(destObj.getClass());
return CollectionUtils.createNewSet(destSetType, result);
} else {
System.out.println("----IN----");
// Bug #1822421 - Clear first so we don't end up with the removed orphans again
Set ret = (Set) field;
ret.clear();
//((Set) field).addAll(result);
for(Object res : result) {
System.out.println("FOUND " + res.toString());
ret.add(res);
}
System.out.println("END SIZE " + ret.size());
System.out.println("----OUT----");
return ret;
}
輸出,用於這種情況下:
----IN----
FOUND nz.c[email protected]3e2
FOUND nz.c[email protected]3e2
END SIZE 1
----OUT----
它的輸出是2項,但正如你可以看到@ 3e2他們是由於某種原因相同的項目。所以當你調用addAll時,它會刪除重複項,並且只留下1個項目。爲什麼Dozer意外地映射了2個相同的值?我檢查確保源對象集合沒有相同的項目翻倍,並且確實沒有。奇怪的確如此。
編輯#3:
我在這裏做進一步的測試與點點運氣。這是一個Dozer映射2相同值的問題,並且addAll將重複關閉,使其成爲列表中的一個項目。不幸的是,我無法很容易地調試addToSet中的遞歸方法,以確定發生這種情況的原因。
如果我想出其他東西,將會更新,否則我對這個問題沒有任何想法。