0

Issue

I am having a big problem with marshalling and demarshalling entities to XML for REST webservices purpose. I am using standart javax.xml and the objects are finally marshalled just fine. But when obtaining item from webservice, something is going wrong and all the collections are lost.

Let me show you an exmple entity.

Example entity

@XmlRootElement(name = "file")
@XmlAccessorType(XmlAccessType.FIELD)
@Entity
@Table(name = "files")
public class FileModel extends BaseEntity {

    @XmlID
    @Id
    @Column(name = "ID")
    private String id;

    @Column(name = "description")
    private String description;

    @XmlIDREF
    @ManyToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
    @JoinTable(name = "file_user", joinColumns = { @JoinColumn(name = "id_file", nullable = false, updatable = false) }, inverseJoinColumns = { @JoinColumn(name = "username", nullable = false, updatable = false) })
    private Collection<UserModel> users;

// Constructors

public FileModel() throws NoSuchAlgorithmException {
    super();
    this.users = new ArrayList<UserModel>();
// for the purpose of new objects while marshalling
}

Example marshalled webservice answer

<file>
   <id>1</id>
   <description>example</description>
   <users>rod</users>
</file>

Example xml sent to webservice, and should be demarshalled

<file>
   <id>1</id>
   <description>example</description>
   <users>rod</users>
   <users>anyone</users>
</file>

So I would expect the collection of users to be filled with additional user. But when I debugged the service, the incoming object's collections are completetely empty. So it results in removing all the users from the database. Why? :(

Of course anyone exists in the database (assumption) so it is not actually an issue of ORM but the object demarshalling. What am I doing wrong?

Thanks!

Atais
  • 10,857
  • 6
  • 71
  • 111
  • Is your web service even be invoked? – SSC Nov 17 '13 at 18:19
  • Yes. I am using Apache CXF to provide REST webservices. In my service I can see the incoming request, with PUT or POST data. But the post-demarshalled object is missing the Collections. – Atais Nov 17 '13 at 19:34
  • I believe this is an asnwer: http://stackoverflow.com/questions/7278406/serialize-a-jaxb-object-via-its-id testing :-) – Atais Nov 17 '13 at 21:22
  • Answer is to use XMLAdapters, like in the link I gave: [http://stackoverflow.com/questions/7278406/serialize-a-jaxb-object-via-its-id][1] [1]: http://stackoverflow.com/questions/7278406/serialize-a-jaxb-object-via-its-id – Atais Jun 05 '14 at 12:45

0 Answers0