2

我正在向您展示我已经拥有的课程,并且我想使用 jaxb 进行序列化。不幸的是,当我尝试在 Person 内部序列化 positionSet 时,我无法完全获得 Person 对象内部的所有属性

代码:

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

现在,正如您可以理解的那样,关系是: Person 一对多 Position 多对一 OrganizationUnit Position 有两个属性:“positionType”和“organizationUnit”,它们引用了 OrganizationUnit 当 jaxb 序列化时,我只能看到 positionType 元素和关于 organizationUnit 元素的任何内容内位置。我试图检查 Position 是否包含值,并且发现数据在对象内部可用。Position 类中positionType 和organizationUnit 属性之间的区别在于@XmlInverseReference 注释,我需要OrganizationUnit 类映射的organizationUnit 属性,而我不需要positionType 的此注释。

我该如何解决这个问题?为什么注释不允许我访问 Position 中的 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>
4

1 回答 1

1

我只是通过删除 Position 类内的 organizationUnit 属性上的注释 @XmlInverseReference 来解决,我为 OrganizationUnit 类内的属性 positionSet 指定了 xml-transient

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>
于 2013-04-17T14:58:34.957 回答