Fixture loads hierachical model incorrectlly

Bug #480068 reported by true_cp
6
This bug affects 1 person
Affects Status Importance Assigned to Milestone
play framework
Status tracked in 1.0
1.0
Fix Released
Undecided
Unassigned
1.1
Fix Committed
Undecided
Unassigned

Bug Description

Below Class design will cause problems while loading yml file by Fixture:

public abstract Class Tag extends Model { ... }
public Class FunctionTag extends Tag { ... }

public abstract Class Post extends Model {
    @ManyToMany
    public Set<Tag> tags;
    ......
}
public Class Artical extends Post {
    ......
}

And we have below yml to initialize data:
FunctionTag(ft1):
    ......
Artical(a1):
    tags: [ft1]

Two problems here:
1. The "resolveDependencies" doesn't look into superClass to resolve relationships, so that the FunctionTag defined in yml file cannot be set to Post.tags.
2. After FunctionTag is persisted, the key which is put into "idCache" (e.g models.FunctionTag - ft1) is not the same with the key which generated by "resolveDependencies" method (e.g models.Tag - ft1).

Below is the proposed solution:
Old Code:
----------------------------------------------------------------------------------------------------
line 135:
                       idCache.put(type + "-" + id, JPASupport.findKey(model));
line 173:
        for (Field field : type.getDeclaredFields()) { ...... }
----------------------------------------------------------------------------------------------------
New Code:
----------------------------------------------------------------------------------------------------
line 135:
                        while (!cType.equals(JPASupport.class)) {
                            idCache.put(cType.getName() + "-" + id, JPASupport.findKey(model));
                            cType = cType.getSuperclass();
                        } // This solution might not solve the problem as the yml definition has duplicated id between different
                          // inherited classes. And it will generate some useless key and grow the idCache size.
line 173:
        Set<Field> fields = new HashSet<Field>();
        Class clazz = type;
        while (!clazz.equals(JPASupport.class)) {
            Collections.addAll(fields, clazz.getDeclaredFields());
            clazz = clazz.getSuperclass();
        }
        for (Field field : fields) { ...... }
----------------------------------------------------------------------------------------------------

Revision history for this message
Guillaume Bort (guillaume-bort) wrote :

Could you provide a TestCase as well ? It would be great.

Changed in play:
status: New → Confirmed
Revision history for this message
true_cp (true-cp) wrote :
Revision history for this message
true_cp (true-cp) wrote :
Changed in play:
status: Confirmed → Fix Committed
Revision history for this message
true_cp (true-cp) wrote :

Merge incorrect.
Fixtures.java line 185 shoudl be:
         for (Field field : fields) {

Update test case for this bug.

Changed in play:
status: Fix Committed → Incomplete
Revision history for this message
Guillaume Bort (guillaume-bort) wrote :

Ok I fix it. Thank for the test case.

Changed in play:
status: Incomplete → Fix Committed
To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.