views:

73

answers:

1

have a project set up like the following:

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlSeeAlso;

@XmlRootElement(name="employee")
@XmlSeeAlso([EmployeeDesiredSkill.class, EmployeeDesiredTool.class,  EmployeeAreaOfExpertise.class, Project.class, Education.class])
@XmlAccessorType(XmlAccessType.NONE)
class Employee implements Comparable
{       
static hasMany = [employeeDesiredSkills:EmployeeDesiredSkill]     
 @XmlElement
     String name
    @XmlElement
    List<EmployeeDesiredSkill> employeeDesiredSkills = new ArrayList<EmployeeDesiredSkill>();
}


import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;

@XmlAccessorType(XmlAccessType.NONE)
class EmployeeDesiredSkill implements Comparable
{

boolean _deleted
@XmlElement
Skill skill
static belongsTo = [employee:Employee, skill:Skill]
}

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;

@XmlAccessorType(XmlAccessType.FIELD)
class Skill implements Comparable
{
    static hasMany = [roleSkills:RoleSkill, employeeDesiredSkills:EmployeeDesiredSkill]

    boolean _deleted
static transients = ['_deleted']
    @XmlAttribute
    String name
}

When I try to do the XML marshaling of this setup, I get valid values foe Employee, but all of the skills come back empty, like so:

<employee name = "Joe">
<employeeDesiredSkills>
    <skill name="">
        <_deleted>false</_deleted>
    </skill>
</employeeDesiredSkills>
<employeeDesiredSkills>
    <skill name="">
        <_deleted>false</_deleted>
    </skill>
</employeeDesiredSkills>
<employeeDesiredSkills>
    <skill name="">
        <_deleted>false</_deleted>
    </skill>
</employeeDesiredSkills>
<employeeDesiredSkills>
    <skill name="">
        <_deleted>false</_deleted>
    </skill>
</employeeDesiredSkills>
<employeeDesiredSkills>
    <skill name="">
        <_deleted>false</_deleted>
    </skill>
</employeeDesiredSkills>

In other words - name is never returned from the Skill objects. I think Employee must know how many employeeDesiredSkills are in the list (since 5 of them show up), but for some reason can not get the values in the Skill class.

What can I do to fix this? In my controller, if I do the following:

            def employee = Employee.get(session.empid);
            for(esd in employee.getEmployeeDesiredSkills()){println "EmployeeDesiredSkill:" + esd}

I get the full list of desired skills printed (5 of them), so I know the values are in the database already.

To me it seems like the EmployeeDesiredSkill class doesnt know which employee to return the skills for, but I am not sure if that is the right thinking or not. I dont really want to add a belongsTo = Employee to my Skills class, because I want those skills to possibly be used by other employees as well.

A: 

Not sure if this helps, but if I run your example directly in Java everything works:

Employee:

import java.util.ArrayList;
import java.util.List;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement(name = "employee")
@XmlAccessorType(XmlAccessType.NONE)
class Employee {
    @XmlElement
    String name;

    @XmlElement
    List<EmployeeDesiredSkill> employeeDesiredSkills = new ArrayList<EmployeeDesiredSkill>();
}

EmployeeDesiredSkill

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;

@XmlAccessorType(XmlAccessType.NONE)
class EmployeeDesiredSkill {

    boolean _deleted;

    @XmlElement
    Skill skill;
}

Skill

import javax.xml.bind.annotation.XmlAccessType; import javax.xml.bind.annotation.XmlAccessorType; import javax.xml.bind.annotation.XmlAttribute;

@XmlAccessorType(XmlAccessType.FIELD) class Skill {

boolean _deleted;

@XmlAttribute
String name;

}

With the following code:

import java.io.File;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;

public class Demo {

    public static void main(String[] args) throws Exception {
        JAXBContext jc = JAXBContext.newInstance(Employee.class);

        Unmarshaller unmarshaller = jc.createUnmarshaller();
        Employee employee = (Employee) unmarshaller.unmarshal(new File("input.xml"));

        Marshaller marshaller = jc.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.marshal(employee, System.out);

    }
}

Can produce/consume the following XML:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<employee>
    <employeeDesiredSkills>
        <skill name="A">
            <_deleted>false</_deleted>
        </skill>
    </employeeDesiredSkills>
    <employeeDesiredSkills>
        <skill name="B">
            <_deleted>false</_deleted>
        </skill>
    </employeeDesiredSkills>
    <employeeDesiredSkills>
        <skill name="C">
            <_deleted>false</_deleted>
        </skill>
    </employeeDesiredSkills>
    <employeeDesiredSkills>
        <skill name="D">
            <_deleted>false</_deleted>
        </skill>
    </employeeDesiredSkills>
    <employeeDesiredSkills>
        <skill name="E">
            <_deleted>false</_deleted>
        </skill>
    </employeeDesiredSkills>
</employee>
Blaise Doughan