Is there a way to configure the @XmlTransient JPA2.0 annotation that it will only block the JAXB mechanism when a Java-Object is serialized to xml and not when the incoming XML is transformed to a java object?
Background: I have a REST api which speaks XML. There is an endpoint to create a new Attachment object. As we speak of attachment there is a byte[] field in this class. In further listings of attachments i don’t want to deliver the byte[] content of each attachment.
@Entity
@XmlRootElement
public class Attachment {
private String name;
private String mimeType;
private byte[] dataPart;
public String getName() {
return name;
}
public void setName( String name ) {
this.name = name;
}
public String getMimeType() {
return mimeType;
}
public void setMimeType( String mimeType ) {
this.mimeType = mimeType;
}
public byte[] getDataPart() {
return dataPart.clone();
}
public void setDataPart( byte[] dataPart ) {
this.dataPart = dataPart.clone();
}
}
So, when i mark the getDataPart() with XmlTransient, the data of the incoming byte[] is ignored and set to null -> it’s lost.
Anyone an idea how to specifify the direction of the XmlTransient?
I have to answer the question myself =)
I solved this by using a custom XmlAdapter that will tranlsate the binary data only in one direction. This is still a hack and we are not using this any more. Reasons below.
Here is the adapter:
The entity needs to be annotated with this adapter:
This solution works as exepcted. But there is a drawback: One of the intentions was to not let hibernate load the binary data while processing/loading attachment data. But thats not how it works. The binary data is loaded by hibernate in this case because it is send to the XMLAdapter but not translated into base64 🙁