4
@XmlType  
@XmlAccessorType(XmlAccessType.FIELD)  // here I need this access
public class User implements Serializable 
{  
     // ...  

     @XmlTransient
     private Set<Values> values;

     // ...

     @XmlElement
     private Set<History> getXmlHistory()
     {
         return new CustomSet<Values, History>(Values);
     }

     private void setXmlHistory(final Set<History> aHistory)
     {
         this.values = new HashSet<Values>();
     }  
}  

When I am create User object in Java code and after create XML, then all normally.
But when I try to get User-object from XML, then field values always null. So setter not working here. May be setter needs some annotation too?

XML looks like

<user>  
   ...  
      <xmlHistory>  
       // ... record 1 
      </xmlHistory>  
      <xmlHistory>  
      // ... record 2 
      </xmlHistory>  
</user>  
Peter Hall
  • 53,120
  • 14
  • 139
  • 204
Ilya
  • 29,135
  • 19
  • 110
  • 158

1 Answers1

7

I do not believe that this is a JAXB problem, as the following model will work:

package forum10617267;

import java.io.Serializable;
import java.util.*;
import javax.xml.bind.annotation.*;

@XmlType
@XmlAccessorType(XmlAccessType.FIELD) // here I need this access
public class User implements Serializable {

    @XmlTransient
    private Set<History> history = new HashSet<History>();

    @XmlElement
    private Set<History> getXmlHistory() {
         return history;
    }

    private void setXmlHistory(final Set<History> aHistory) {
        this.history = aHistory;
    }

}

The problem you are seeing is a result of the logic you have in your get/set methods. Since your values field is not initialized, I am not sure how CustomSet would be able to update it.

package forum10617267;

import java.io.Serializable;
import java.util.*;
import javax.xml.bind.annotation.*;

@XmlType
@XmlAccessorType(XmlAccessType.FIELD) // here I need this access
public class User implements Serializable {

    @XmlTransient
    private Set<Values> values;

    @XmlElement
    private Set<History> getXmlHistory() {
         return new CustomSet<Values, History>(values);
    }

    private void setXmlHistory(final Set<History> aHistory) {
        this.values = new HashSet<Values>();
    }

}
bdoughan
  • 147,609
  • 23
  • 300
  • 400
  • Yep. I am trying to get not null value for my 'values' field. But it always null! – Ilya May 16 '12 at 12:36
  • @user1143825 - I noticed your accessor methods are private. Do you see the same behaviour if they are public? – bdoughan May 16 '12 at 12:39
  • @user1143825 - I have updated my answer. I believe the `issue` is that `CustomSet` is not updating the `values` field. The fix may be as simple as initializing the `values` field instead of it being null. – bdoughan May 16 '12 at 15:21
  • http://stackoverflow.com/questions/40480/is-java-pass-by-reference-or-pass-by-value ^Java is pass by value. That explains why OP's field was not initialized. Blaise is correct, it has nothing to do with JAXB. – KyleM Jun 29 '15 at 21:15