好的,得到了工作。想在這裏分享。如果任何人都可以看看,並認爲它是值得的git來源,請協助這樣做。
問題是此時嵌入式不支持。因此,Fixtures類中的resolveDependencies不能解析定義的映射中的引用。
我的工作aorund是擴展Fixtures類,並隱藏loadModels方法。和 的
Fixtures.loadmodels(String name, Map<String, Object> idCache) {
... ...
if (f.getType().isAssignableFrom(Map.class)) {
/* new below */
ElementCollection ec = f.getAnnotation(ElementCollection.class);
MapKeyClass mkc = f.getAnnotation(MapKeyClass.class);
if (ec != null && mkc != null) {
Map mapper = (Map) objects.get(key).get(f.getName());
if (mapper == null) continue;
Class targetClass = ec.targetClass();
Class mapKeyClass = mkc.value();
Map<Object, Object> fieldValue = new HashMap();
for (Object k : mapper.keySet()) {
Object mapKey = retrieveObject(mapKeyClass, k, idCache);
Object targetValue = retrieveObject(targetClass, mapper.get(k), idCache);
fieldValue.put(mapKey, targetValue);
}
f.set(model, fieldValue);
} else {
f.set(model, objects.get(key).get(f.getName()));
}
}
... ...
}
/* new method below */
private static Object retrieveObject(Class targetClass, Object alias, Map<String, Object> idCache)
{
if (targetClass.isAssignableFrom(alias.getClass())) {
return alias;
}
Object targetValue = idCache.get(targetClass.getCanonicalName() + "-"+ alias);
if (targetValue != null) {
targetValue = Model.Manager.factoryFor(targetClass).findById(targetValue);
}else {
try {
targetValue = targetClass.getConstructor(String.class).newInstance(alias.toString());
} catch(Exception ex) {
ex.printStackTrace();
return null;
}
}
return targetValue;
}