Results 1 to 10 of 10

Thread: Best practice for mapping JAXB generated to domain objects

  1. #1

    Default Best practice for mapping JAXB generated to domain objects

    Howdy all,

    In my project I am using xjc to generate classes suitable for marshalling/unmarshalling the request/response objects.

    Are there any best practices for adapting the JAXB objects with the application domain objects?

    My current model uses builders. But this is quite cumbersome.

    I've looked at using CommonUtil copyBean, but this only really works if there is a one-to-one mapping between the domain entities and the XSD data model.

    How are other people managing this problem?

    If the answer is in TFM, please put me to the right place. I'm simply not seeing it.

    Thanks,
    Curtis

  2. #2
    Join Date
    Oct 2008
    Location
    Poland, Wrocław
    Posts
    424

    Default

    Hi

    The best way is to use the JAXB classes as domain objects. Currently (after several projects where I've xjc's my JAXB classes) I feel it's very easy and quick to write your JAXB classes manually and annotate them as you wish. That way you control everything - names, namespaces, sequences, etc.
    You can also annotate the classes with JSR-303 annotations (Bean Validation) and use them as SpringMVC's form backing objects - you get nice mechanism of creating/editing/viewing your JAXB objects in WWW forms and persisting them to XML upon submission.

    If there are any reasons to create parallel domain model, you can always use some bean mappers - you've mentioned the simplest (copyBean), but there are more sophisticated ones - e.g. Dozer (http://sourceforge.net/projects/dozer).

    regards
    Grzegorz Grzybek

  3. #3

    Default

    Grzegorz:

    Thank you for bringing Dozer to my attention. It looks very promising.

    Just a question about using your JAXB objects for your domain objects. It sounds like you are hand writing the objects. So then are you taking a Contract-Last approach?

    Curtis

  4. #4
    Join Date
    Oct 2008
    Location
    Poland, Wrocław
    Posts
    424

    Default

    Hi

    It sounds like you are hand writing the objects. So then are you taking a Contract-Last approach?
    No - I create my XSD in parallel - I know it's somehow redundant, but I like both clean/non-generated classes and XSDs

    regards
    Grzegorz Grzybek

  5. #5
    Join Date
    Oct 2005
    Posts
    80

    Default

    My opinion is a bit different. My model objects are distinct from the JAXB objects and I transform them where appropriate. I do this to prevent tying my business logic to the schema of the web service -- the relationship is too strong and brittle. To me, they are distinctly different layers, and it shows its usefulness when there are many ways to interact with a platform. Otherwise, the advice above will work where there are not a multiplicity of concerns.

  6. #6

    Default

    @Paul:

    How then do you map between your model objects and JAXB objects? Are you using hand coded builders or some tool?

  7. #7
    Join Date
    Oct 2005
    Posts
    80

    Default

    I hand-code the conversion. In Spring 3, there is a ConversionService<S, T> that's pretty useful for this task. So I've written a handful of converters for the service and the abstraction works pretty well.

  8. #8
    Join Date
    Oct 2008
    Location
    Poland, Wrocław
    Posts
    424

    Default

    @paul4christ79:

    You're right - sometimes it's better to create separate models (one for web services messages and one for "real" business domain model), but usually they're just the same models.

    In my case I have two models - one for Hibernate mapping and one for OXM. Both models' classes are used as form objects, but Hibernate objects are stored in RDBMS columns (property per column) and OXM objects are stored as Blobs. In this case I don't have WebService layer.

    My point is - if you have small number of objects (less than ~50) it's (for me) better to manually create both XSDs and classes - it gives you the good feeling of controlling everything

    regards
    Grzegorz Grzybek

  9. #9
    Join Date
    Dec 2007
    Posts
    11

    Default

    Quote Originally Posted by Grzegorz Grzybek View Post
    Hi


    No - I create my XSD in parallel - I know it's somehow redundant, but I like both clean/non-generated classes and XSDs

    regards
    Grzegorz Grzybek
    I also start off by writing my JAXB model classes, then I use schemagen to generate an XSD from it and then tighten up the XSD with any restrictions needed.

    As to whether you use the JAXB classes as your domain objects, or have another layer you map to, I would consider that a judgement that the developer needs to make on a case by case basis, based on complexity, liability to change, deadlines etc etc

  10. #10
    Join Date
    Apr 2009
    Posts
    15

    Default

    Good question Curtis! I was going to post the exact same question, luckily I found yours first.

    Although they usually represent the same/similar logical entities, I also like to keep my hibernate domain objects and JAXB-classes separate, because in my case they typically have different change requirements, i.e. web service contracts with 3rd parties have to be more stable than my internal domain model.

    So I've resorted to using builders/assemblers as well, which is why I was looking for a new solution, as its a pain!

    I've looked briefly into using Dozer, but wasn't sold. Have you tried it yet? I see that there have been a couple of releases lately so maybe I'll look again. IDE support for it would be great (they have an Eclipse plugin but not IntelliJ)

    In the past I've also put the bean mapping code in my hibernate objects themselves, e.g.

    UserElement xmlUser = user.toXml() and
    User user = User.fromXml(userElement);

    which is convenient because you can easily map a nested object graph by allowing each class to map itself, but on the other hand you pollute hibernate classes with JAXB generated-classes which isn't ideal. I'd prefer to put that code in the JAXB-classes, but that isn't possible if you auto-generate them, which leads to me consider a parallel model but I'm nervous about going down that route.

    Generally I feel quite restricted by uselessness of the XJC-generated classes.

    So basically I've tried a couple of different ways and am yet to find something that I'm totally happy with!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •