2013-04-16 56 views
2

我向你展示了我已經擁有的類,並且我想用jaxb序列化。 Unluckly當我試圖序列positionSet裏面的人,我不能完全獲得,有Person對象內部的所有屬性一對多和多對一MOXy @XmlInverseReference EclipseLink 2.5

代碼:

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..  
    } 

現在,你可以理解的關係是: 人一對多的位置多對一OrganizationUnit 位置有兩個屬性:引用OrganizationUnit的「positionType」和「organizationUnit」 當jaxb序列化時,我只能看到positionType元素以及任何關於Position內部的organizationUnit元素。 我試圖檢查位置包含值,我發現數據在對象內部可用。 位置類中positionType和organizationUnit屬性之間的區別在於,我需要OrganizationUnit類映射的organizationUnit屬性的@XmlInverseReference註釋,而不需要positionType的此註釋。

我該如何解決這個問題?爲什麼註釋不允許我訪問位置內的organizationUnit?

我希望有人能幫助我。 爲您展示它序列化,但不能正確 我會告訴你的XML輸出文件: 我看不到organizationUnit財產

<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> 

回答

1

我只是通過對內側的位置organizationUnit屬性中刪除註釋@XmlInverseReference解決類和我指定xml-transient屬性positionSet OrganizationUnit中的類

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>