Announcement Announcement Module
Collapse
No announcement yet.
Best practice for mapping JAXB generated to domain objects Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • 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
    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

    Comment


    • #3
      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

      Comment


      • #4
        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

        Comment


        • #5
          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.

          Comment


          • #6
            @Paul:

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

            Comment


            • #7
              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.

              Comment


              • #8
                @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

                Comment


                • #9
                  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

                  Comment


                  • #10
                    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!

                    Comment

                    Working...
                    X