使用JAXB在对象图中检测循环

时间:2014-05-29 00:01:20

标签: xml jaxb jaxb2

如何克服这个错误?

@XmlRootElement(name="schema")
@Entity
public class SchemaBase implements Serializable{

/**
 * 
 */
private static final long serialVersionUID = 1L;

@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private int id_schema;  
private String name_schema;
private String description_schema;
private String measuresCaption_schema;
private String defaultRole_schema;


@OneToMany (mappedBy="schema",cascade={CascadeType.MERGE,CascadeType.PERSIST})
private List<CubeBase> cubes;




 @XmlElementWrapper(name = "cube")  
   @XmlElement(name = "cube")
public List<CubeBase> getCubes() {
    return cubes;
}

public void setCubes(List<CubeBase> cubes) {
    this.cubes = cubes;
}



public int getId_schema() {
    return id_schema;
}
public void setId_schema(int id_schema) {
    this.id_schema = id_schema;
}
public String getName_schema() {
    return name_schema;
}
@XmlAttribute(name="name")
public void setName_schema(String name_schema) {
    this.name_schema = name_schema;
}
public String getDescription_schema() {
    return description_schema;
}
@XmlAttribute(name="description")
public void setDescription_schema(String description_schema) {
    this.description_schema = description_schema;
}
public String getMeasuresCaption_schema() {
    return measuresCaption_schema;
}
@XmlAttribute(name="measuresCaption")
public void setMeasuresCaption_schema(String measuresCaption_schema) {
    this.measuresCaption_schema = measuresCaption_schema;
}
public String getDefaultRole_schema() {
    return defaultRole_schema;
}
@XmlAttribute(name="defaultRole")
public void setDefaultRole_schema(String defaultRole_schema) {
    this.defaultRole_schema = defaultRole_schema;
}     
public void addCubeToShema(List<CubeBase> cubes){
   for(CubeBase cubeBase:cubes)
       cubeBase.setSchema(this);
   this.cubes=cubes;
}

}

而另一个类是这样的:

@XmlRootElement(namespace ="esprit.olap.domain.SchemaBase")
@Entity
public class CubeBase implements Serializable{

/**
 * Private Attribute 
 */
private static final long serialVersionUID = 1L;




@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private int id_cube;
private String name_cube;
private String description_cube;
private String caption_cube;
private boolean visible;
private boolean cache;
private boolean enabled;

@ManyToOne
private SchemaBase schema;


public boolean isCache() {
    return cache;
}

public void setCache(boolean cache) {
    this.cache = cache;
}
public boolean isEnabled() {
    return enabled;
}

public void setEnabled(boolean enabled) {
    this.enabled = enabled;
}
public boolean isVisible() {
    return visible;
}

public void setVisible(boolean visible) {
    this.visible = visible;
}   

public SchemaBase getSchema() {
    return schema;
}

public void setSchema(SchemaBase schema) {
    this.schema = schema;
}
public int getId_cube() {
    return id_cube;
}

public void setId_cube(int id_cube) {
    this.id_cube = id_cube;
}
public String getName_cube() {
    return name_cube;
}

public void setName_cube(String name_cube) {
    this.name_cube = name_cube;
}
public String getDescription_cube() {
    return description_cube;
}

public void setDescription_cube(String description_cube) {
    this.description_cube = description_cube;
}
public String getCaption_cube() {
    return caption_cube;
}

public void setCaption_cube(String caption_cube) {
    this.caption_cube = caption_cube;
}

}

2 个答案:

答案 0 :(得分:0)

这似乎与JAXB A cycle is detected in the object graphJAXB Mapping cyclic references to XML

重复

(正常的Java序列化有逻辑来处理这种情况。我很惊讶JAXB没有;它并不难做到。再说一遍,我很惊讶JAXB已经变得如此流行。)

答案 1 :(得分:0)

在设置列表之前添加注释@XmlTransient, 在这个例子中,你必须在addCubeToShema方法之前添加@XmlTransient。