Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.
Bug 370573 - Enhancement: support @XmlInverseReference with @XmlElementRef
Summary: Enhancement: support @XmlInverseReference with @XmlElementRef
Status: RESOLVED FIXED
Alias: None
Product: z_Archived
Classification: Eclipse Foundation
Component: Eclipselink (show other bugs)
Version: unspecified   Edit
Hardware: PC Windows XP
: P3 normal (vote)
Target Milestone: ---   Edit
Assignee: Matt MacIvor CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2012-02-03 11:39 EST by Blaise Doughan CLA
Modified: 2022-06-09 10:09 EDT (History)
2 users (show)

See Also:


Attachments
Propsed fix and tests (11.84 KB, patch)
2012-05-25 15:40 EDT, Matt MacIvor CLA
no flags Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Blaise Doughan CLA 2012-02-03 11:39:48 EST
Since the values of a property are all related through an inheritance relationship, the following use case should be supported.

import java.util.*;
import javax.xml.bind.annotation.*;
import org.eclipse.persistence.oxm.annotations.XmlInverseReference;

@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class Parent {

    @XmlInverseReference(mappedBy="children")
    private Parent parent;

    @XmlElementRef
    private Set<Parent> children = new HashSet<Parent>();


}
Comment 1 Blaise Doughan CLA 2012-02-03 11:41:25 EST
Implementation Notes:
- To support this use case we will need to support XMLInverseReferenceMapping on XMLChoiceObjectMapping and XMLChoiceCollectionMapping.
- oxm.xml and schema will need to be updated to support this configuration.
Comment 2 Michal Politowski CLA 2012-05-21 09:50:02 EDT
Is there any chance that the target for this will be moved to 2.4.0?

This is apparently exactly what I miss right at the moment (except between two different classes in my case).
Or maybe I am mistaken? Because I was able to describe my use case with the current external metadata schema, it only does not work.

<java-type name="Parent">
 <java-attributes>
  <xml-element-ref java-attribute="children"/>
 </java-attributes>
</java-type>

<java-type name="Child">
 <java-attributes>
  <xml-inverse-reference mapped-by="children" java-attribute="parent" xml-accessor-type="FIELD"/>
  </java-attributes>
</java-type>
Comment 3 Matt MacIvor CLA 2012-05-24 13:56:32 EDT
I tried this in the latest Trunk and 2.4 streams and it seems to work. I attempted both with annotations using the object model Blaise provided and with the external bindings Michal provided and in both cases, the unmarshal worked and the backpointer seemed to be set correctly. 

I guessed a little at the object model based on the xml bindings. 

package bug370573;

import java.util.List;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement
public class Parent {

    public List<Child> children;
    
}



package bug370573;

import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement
public class Child {

    Parent parent;
}

...
<java-type name="Parent">
 <java-attributes>
  <xml-element-ref java-attribute="children"/>
 </java-attributes>
</java-type>

<java-type name="Child">
 <java-attributes>
  <xml-inverse-reference mapped-by="children" java-attribute="parent"
xml-accessor-type="FIELD"/>
  </java-attributes>
</java-type>
...


Unmarshalling the following xml document:
<parent>
   <child/>
   <child/>
</parent>


Using these classes and external bindings on the latest trunk everything seems to work. Can you verify that this problem still persists for you on the latest 2.4 or trunk nightly? It may have been resolved by the fixes for another inverse-reference bug that has been checked in recently.
Comment 4 Michal Politowski CLA 2012-05-25 05:44:12 EDT
Tested with eclipselink-2.4.0-20120525.052533-3357.jar

It indeed works with the basic model, but unfortunately fails for subclasses of Child.

After adding a trivial subclass to the context

@XmlRootElement
public class ChildSubclass extends Child {}

and after unmarshaling

<parent>
   <child/>
   <childSubclass/>
</parent>

the first list element has the backlink to parent set, the second one does not.
Comment 5 Matt MacIvor CLA 2012-05-25 15:40:19 EDT
Created attachment 216315 [details]
Propsed fix and tests
Comment 6 Michal Politowski CLA 2012-05-28 06:46:33 EDT
Looks like it works in the latest 2.4.0 nightly. Thank you.
Unfortunately it turns out that I have also a case where I have to combine element refs and inverse references with adapters, and this does not seem to work, even though inverse references from adapted classes work with regular xml-element mappings.

I will describe this case in a separate bugreport.
Comment 7 Michal Politowski CLA 2012-05-28 07:21:06 EDT
(In reply to comment #6)
> I will describe this case in a separate bugreport.

The bug 380803
Comment 8 Matt MacIvor CLA 2012-05-28 15:07:15 EDT
Attached patch checked in to SVN.
Closing bug as fixed, since additional issues with xml-inverse-reference are being treated as a separate bug.
Comment 9 Eclipse Webmaster CLA 2022-06-09 10:06:30 EDT
The Eclipselink project has moved to Github: https://github.com/eclipse-ee4j/eclipselink
Comment 10 Eclipse Webmaster CLA 2022-06-09 10:09:44 EDT
The Eclipselink project has moved to Github: https://github.com/eclipse-ee4j/eclipselink