一对多和多对一MOXy @XmlInverseReference EclipseLink 2.5

时间:2013-04-16 15:59:26

标签: jaxb eclipselink moxy

我正在向你展示我已经拥有的课程,我想用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..    
    }

现在您可以理解这些关系是: 一对多的人将多个组织定位于一个组织单位 Position有两个属性:“positionType”和“organizationUnit”,它们引用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 个答案:

答案 0 :(得分: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>