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!