Reputation: 183
I'm showing you the class that already I've got and I would like to serialize with jaxb. Unluckly when I try to serialize positionSet inside Person I can't obtain completely all the attribute that there are inside the Person object
Code:
public class Person {
@Id
@Column(name = "ID")
private Integer id;
@Column(name = "FIRST_NAME")
@NotNullOnlyJsp
private String firstName;
@Column(name = "LAST_NAME")
@NotNullOnlyJsp
private String lastName;
@OneToMany(cascade = { CascadeType.ALL }, fetch = FetchType.LAZY, mappedBy = "person")
@org.hibernate.annotations.OrderBy(clause = "start_date asc")
private Set<Position> positionSet = new LinkedHashSet<Position>();
// getter and setter and other methods..
}
public class Position{
@Id
@Column(name = "ID")
private Integer id;
@Column(name = "FK_PERSON")
@NotNull
private Integer personId;
@Column(name = "FK_POSITION_TYPE")
@NotNull
private Integer positionTypeId;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "FK_POSITION_TYPE", insertable = false, updatable = false)
private Entity positionType;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "FK_PERSON", insertable = false, updatable = false)
// I need this annotation to avoid ciclyc graph
@XmlInverseReference(mappedBy="positionSet")
private Person person;
@Column(name = "FK_ORG_UNIT")
@NotNull
private Integer organizationUnitId;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "FK_ORG_UNIT", insertable = false, updatable = false)
// I need this annotation to avoid ciclyc graph
@XmlInverseReference(mappedBy="organizationUnitPositionSet")
private OrganizationUnit organizationUnit;
// getter and setter and other methods..
}
public class OrganizationUnit{
@Id
@Column(name = "ID")
private Integer id;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "FK_ORG_UNIT_TYPE", insertable = false, updatable = false)
private OrganizationUnitType organizationUnitType;
@Column(name = "FK_ORG_UNIT_TYPE")
private Integer organizationUnitTypeId;
@Column(name = "DESCRIPTION", length = 4000)
@NotNull
private String description;
@OneToMany(cascade = { CascadeType.ALL }, fetch = FetchType.LAZY, mappedBy = "organizationUnit")
@org.hibernate.annotations.OrderBy(clause = "start_date asc")
// I need this annotation to avoid ciclyc graph
private Set<Position> organizationUnitPositionSet = new LinkedHashSet<Position>();
// getter and setter and other methods..
}
Now As you can understand the relations are: Person One to Many Position Many to One OrganizationUnit Position has two properties: "positionType" and "organizationUnit" that references to OrganizationUnit When jaxb serialize I can see only the positionType element and anything about the organizationUnit element inside Position. I tried to check that the Position contains value and I figured out that the data are available inside the object. The difference between positionType and organizationUnit property inside Position Class is the @XmlInverseReference annotation that I need for organizationUnit property mapped by OrganizationUnit class while I don't need this annotation for positionType.
How Could I solve this problem? Why doesn't the annotation allow me to access to organizationUnit inside Position?
I hope that someone could help me. For showing you that it serialize but not correctly I'll show you the xml output file: I can't see organizationUnit property
<person>
// other property
<position-set>
<position>
<id>174215</id>
<discriminator>support</discriminator>
<endDate>2005-06-30T00:00:00</endDate>
<organizationUnitId>1234</organizationUnitId>
<positionType>
<id>2733</id>
<displayValue>BLABLA</displayValue>
<organization-unit-type>
<id>101</id>
<description>supportRole</description>
</organization-unit-type>
</positionType>
<startDate>2005-02-01T00:00:00</startDate>
</position>
</position-set>
This is the oxm file:
<?xml version="1.0"?>
<xml-bindings
xmlns="http://www.eclipse.org/eclipselink/xsds/persistence/oxm"
package-name="it.mymodel.ga.model" >
<xml-schema
element-form-default="QUALIFIED"/>
<java-types>
<java-type name="Person" xml-accessor-type="NONE">
<xml-root-element/>
<java-attributes>
<xml-element java-attribute="firstName" name="first-name" />
<xml-element java-attribute="lastName" name="last-name" />
<xml-element java-attribute="stringMap" name="string-map" />
<xml-element java-attribute="positionSet" name="position" >
<xml-element-wrapper name="position-set"/>
</xml-element>
</java-attributes>
</java-type>
<java-type name="Position"> <!-- I had to use this approach than xml-accessor-type="NONE" unlikely -->
<java-attributes>
<xml-element java-attribute="discriminator" />
<xml-element java-attribute="startDate" />
<xml-element java-attribute="endDate"/>
<xml-element java-attribute="organizationUnit" name="organization-unit"/>
<xml-element java-attribute="positionType" name="position-type"/>
<xml-transient java-attribute="person"/>
<xml-transient java-attribute="positionTypeId"/>
<xml-transient java-attribute="fileInfo"/>
<xml-transient java-attribute="personId"/>
<xml-transient java-attribute="priority"/>
<xml-transient java-attribute="uniqueIdentifier"/>
<xml-transient java-attribute="uuid"/>
<xml-transient java-attribute="removeFile"/>
</java-attributes>
</java-type>
<java-type name="OrganizationUnit">
<java-attributes>
<xml-transient java-attribute="description" name="description" />
<xml-element java-attribute="organizationUnitType" name="organization-unit-type"/>
<xml-transient java-attribute="displayAs"/>
<xml-transient java-attribute="organizationUnitTypeId"/>
<xml-element java-attribute="displayValue" />
</java-attributes>
</java-type>
<java-type name="OrganizationUnitType" >
<java-attributes>
<xml-element java-attribute="description"/>
<xml-transient java-attribute="priority"/>
</java-attributes>
</java-type>
</java-types>
</xml-bindings>
Upvotes: 2
Views: 1157
Reputation: 183
I simply solved by removing the annotation @XmlInverseReference on organizationUnit property inside Position class and I specified xml-transient for the property positionSet inside OrganizationUnit Class
oxm file :
<?xml version="1.0"?>
<xml-bindings
xmlns="http://www.eclipse.org/eclipselink/xsds/persistence/oxm"
package-name="mypackage" >
<xml-schema
element-form-default="QUALIFIED"/>
<java-types>
<java-type name="Person" xml-accessor-type="NONE">
<xml-root-element/>
<xml-type prop-order="firstName lastName addressSet contactSet integerMap dateMap stringMap positionSet personElementSet clobMap blobMap gaDictionaryMap "/>
<java-attributes>
<xml-element java-attribute="firstName" name="first-name" />
<xml-element java-attribute="lastName" name="last-name" />
<xml-element java-attribute="stringMap" name="string-map" >
<xml-java-type-adapter value="it.cineca.jaxb.adapter.StringMapAdapter" />
</xml-element>
<xml-element java-attribute="personElementSet" name="person-element">
<xml-element-wrapper name="person-element-set"/>
</xml-element>
<xml-element java-attribute="dateMap" name="date-map" >
<!-- <xml-java-type-adapter value="it.cineca.jaxb.adapter.DateMapAdapter" /> -->
</xml-element>
<xml-element java-attribute="positionSet" name="position" >
<xml-element-wrapper name="position-set"/>
</xml-element>
<xml-element java-attribute="addressSet" name="address">
<xml-element-wrapper name="address-set"/>
</xml-element>
<xml-element java-attribute="contactSet" name="contact">
<xml-element-wrapper name="contact-set"/>
</xml-element>
<xml-element java-attribute="integerMap" name="integer-map"/>
<xml-element java-attribute="blobMap" name="blob-map"/>
<xml-element java-attribute="clobMap" name="blob-map"/>
<xml-element java-attribute="gaDictionaryMap" name="ga-dictionary-map"/>
</java-attributes>
</java-type>
<java-type name="Position">
<java-attributes>
<xml-element java-attribute="discriminator" />
<xml-element java-attribute="startDate" />
<xml-element java-attribute="endDate"/>
<xml-element java-attribute="organizationUnit" name="organization-unit"/>
<xml-element java-attribute="positionType" name="position-type"/>
<xml-transient java-attribute="organizationUnitId"/>
<xml-transient java-attribute="person"/>
<xml-transient java-attribute="positionTypeId"/>
<xml-transient java-attribute="fileInfo"/>
<xml-transient java-attribute="personId"/>
<xml-transient java-attribute="priority"/>
<xml-transient java-attribute="uniqueIdentifier"/>
<xml-transient java-attribute="uuid"/>
<xml-transient java-attribute="removeFile"/>
</java-attributes>
</java-type>
<java-type name="OrganizationUnit">
<java-attributes>
<xml-element java-attribute="displayValue" />
<xml-element java-attribute="organizationUnitType" name="organization-unit-type"/>
<xml-element java-attribute="stringMap" name="string-map" />
<xml-element java-attribute="dateMap" name="date-map" />
<xml-element java-attribute="startDate" />
<xml-element java-attribute="endDate" />
<xml-transient java-attribute="addressSet"/>
<xml-transient java-attribute="contactSet"/>
<xml-transient java-attribute="blobMap"/>
<xml-transient java-attribute="clobMap"/>
<xml-transient java-attribute="childOrganizationUnitLinkSet" />
<xml-transient java-attribute="dataSet"/>
<xml-transient java-attribute="displayAs"/>
<xml-transient java-attribute="discriminatorSet"/>
<xml-transient java-attribute="lastModified"/>
<xml-transient java-attribute="lastModifiedString"/>
<xml-transient java-attribute="organizationUnitTypeId"/>
<xml-transient java-attribute="description"/>
<xml-transient java-attribute="parentOrganizationUnitLinkSet" />
<xml-transient java-attribute="positionSet"/>
<xml-transient java-attribute="principalContactMap"/>
</java-attributes>
</java-type>
</java-types>
</xml-bindings>
Upvotes: 1