Question: How to bind SOAP <-> ORM JPA ?

DZone 's Guide to

Question: How to bind SOAP <-> ORM JPA ?

· Java Zone ·
Free Resource

After a few days of evaluating my options about binding my SOAP message objects to my JPA entities... I have:

A soap serializable type, let's say SoapType:
package soap.objects;

@XmlType(name = "", propOrder = {"number",})
@XmlRootElement(name = "Domain")
class SoapType implements Serializable {
@XmlElement(name = "entity.id")
protected long entityId;
public long getEntityId() { return entityId; }
public void setEntityId(long value) { this.entityId = value; }

@XmlElement(required = true)
protected String uri;
public String getUri() { return uri; }
public void setUri(String value) { this.uri = value; }

// ....other.fields.and.methods.here...

A JPA entity type, let's say SoapEntity:

package jpa.objects;

@Table(name = "SOAP_TYPE")
public class SoapEntity {
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "ID")
protected long id;
public long getId() { return id; }
public long setId(long id) { this.id = id; }

@Column(name = "URI", nullable = false, unique = true)
protected String uri;
public String getUri() { return uri; }
public String setUri(String uri) { this.uri = uri; }

// ....other.fields.and.methods.here...

As you can deduct from the short code samples, every time my web-service receives a SOAP message containing values that should be persistent in the database, I should convert the received objects to the JPA entities. And every time I read the data base to respond a service request, I should convert the read entity in the respective object that can be serialized in the SOAP messages. A dual channel repetitive copy mechanism - a copy layer - or binding framework. I don't like the term binding since the objects not only differs in format but the objects are part of completely different models - this is another topic I prefer to skip here.

Question: How to copy the values between the SOAP Message elements and the JPA entities?

Options I am considering right now:

  1. Brute force, copying one by one the fields and properties from one side to another. Simple, fast and the most reliable solution, but with a lot of identical code for all entities. The number of line codes required to copy is linearly coupled with the number of entities and their attributes (can be thousands of lines to code and maintain).
  2. Recursive Reflection: trying to match the getters and setters names.. it works fine for shallow copy but becomes weird with collections and nested complex types. Reflection reduces the number of lines and, depending on the simplicity of the models, can be a good option. You pay a bit of performance but in my early experiments, the cost/benefit of reflection is good. Dozer is a nice option here but it imposes quite a lot of dependencies and seems Spring oriented - so, if you are using Spring, go for it.
  3. Adapters using annotations: similar to the solution proposed here, but instead of mapping XML to Objects I plan to map different objects (from different models).
  4. Inheritance between the soap objects and entities: entities are declared subclasses of XML generated objects, so I need to copy only from xml to entites, the reverse order is naturally bounded by casting. What means you solve half of the problem :). This points to an unwanted coupling between the models, but reduce the processing effort, so the bargain between the beauty and the power is up to you.

I certainly will blog here if I find a good workaround to this problem, so I hope to hear from you something new. :)

From http://weblogs.java.net/blog/felipegaucho/


Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}