2012-02-10 40 views
2

的init-datasample.yml燈具loadModel當YML文件包含地圖對象指向另一個對象

Book(a): &a 
    title: Play in Action 
    price: 30.00 

Book(b): &b 
    title: Alice in Wonderland 
    price: 12.00 

Person(b): 
    name: Bob Joe 
    ratings: 
    ? *a: 8 
    ? *b: 8 

的Java類定義人

@Entity 
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE) 
public class Person extends Model 
{ 
    public String name; 

    // choice and importance score 
    @ElementCollection(targetClass=Integer.class) 
     @MapKeyClass(Book.class) 
    public Map<Book, Integer> ratings; 
} 

的Fixture.loadModels無法正確載入( 「init-datasample.yml」)不加載上面定義的初始評級。我試圖不把& a,& b,它也沒有工作。有人可以幫忙嗎?

謝謝。

回答

1

好的,得到了​​工作。想在這裏分享。如果任何人都可以看看,並認爲它是值得的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; 
    } 
0

在情況下,代碼塊中,有人想引用一個已經存在的實體(whitch裝載在例如較早@OnApplicationStart),你可以通過IDS是指他們:

燈具:

User(user01): 
    email: [email protected] 
    userGroup.id: ${models.UserGroup.getGroupForYml(models.UserGroup.GROUP_SALES)} 

用戶組型號:

@Entity 
public class UserGroup extends Model { 

    public static final String GROUP_ADMIN = "admin"; 
    public static final String GROUP_CUSTOMER = "customer"; 
    public static final String GROUP_SALES = "sales"; 

    @OneToMany(mappedBy = "userGroup") 
    public List<User> userList; 

    @Column(name="groupId") 
    public String group; 

    /** 
    * Used in the yml fixtures 
    * @param group - the group id 
    */ 
    public static Long getGroupForYml(String group) { 
     UserGroup ug = UserGroup.find("byGroup", group).first(); 
     return ug.id; 
    } 
} 

用戶型號:

@Entity 
@Table(name = "users") 
public class User extends BaseModel { 
    publis String email; 

    @ManyToOne 
    public UserGroup userGroup; 
} 
相關問題