Java groovy属性类的JAXB注释
按如下方式设置项目:Java groovy属性类的JAXB注释,java,groovy,jaxb,grails,Java,Groovy,Jaxb,Grails,按如下方式设置项目: 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.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
}
def employee = Employee.get(session.empid);
for(esd in employee.getEmployeeDesiredSkills()){println "EmployeeDesiredSkill:" + esd}
我得到了所需技能的完整列表(其中5项),因此我知道这些值已经在数据库中了
在我看来,EmployeeDesiredSkill类似乎不知道该将技能返还给哪位员工,但我不确定这是否是正确的想法。我真的不想将belongsTo=Employee添加到我的技能类中,因为我希望其他员工也能使用这些技能 不确定这是否有帮助,但如果我直接用Java运行您的示例,一切正常: 员工:
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>();
}
技能
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;
}
导入javax.xml.bind.annotation.XmlAccessType;
导入javax.xml.bind.annotation.XmlAccessorType;
导入javax.xml.bind.annotation.XmlAttribute
@XmlAccessorType(XmlAccessType.FIELD)
课堂技能{
boolean _deleted;
@XmlAttribute
String name;
}
使用以下代码:
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);
}
}
可以生成/使用以下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>
假的
假的
假的
假的
假的
<?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>