XSD2HBM

23 06 2006

XPDL is a large schema (1000+ lines), I wanted to create an entity model from the schema, but I also wanted that entity model to make sense. So I looked at HyperJaxB, JaxB, Castor and number of other technologies. These are all great technologies, except for 2 things. With a complex real life schema, all tend to represent the Object model like a dom. Its not that easy to make the object model understand the xsd. For instance means a list of attributes, not an new object that implements a container that contains attributes. My second requirement is that the generated model should persist in Hibernate. This is where the JaxB like mapping technologies fall over. The bean model that is created is so complex that it looks completely mad when mapped into a database. Its almost impossible to do anything with.

So in the spirit of change, I threw all the JaxB code out. Then I remembered that to get a good entity model with hibernate, with an efficient database schema, the most effective way is to edit the hbm file by hand, and work from there. But XSD is XML and so is the HBM file. A quick transform later with some annotation hints in the XSD and we have converted 1000 lines of XSD into a reasonable hbm entity model. Unlike the HyperJaxb model which contained 344 entities, this contains 36 entities.

All you do, is read your xsd, annotate what you want to be sets, and specify entities to ignore, data types and lengths. Then apply the transform and you get a hbm. Plug that into Hibernate Eclipse Synchronizer and you get the Model Objects and the DAO’s and away you go.

The only thing this doesn’t do yet, is marshall the Java model into XML and out of XML, but since I want mixed namespace, complex XML I will probably hand code the SAX. At least with 36 beans, this is managable. I would have needed JaxB for 344 beans!

Attached are the current version of the transform and an example annotated schema, this is not the complete schema since that might just be confusing. You can find the xsd here and the xsl here

Advertisements

Actions

Information

8 responses

19 12 2006
Stefano

Hi,
i’m searching for a xsd2hbm tool and i’ve find you.

When you say:
“All you do, is read your xsd, annotate what you want to be sets, and specify entities to ignore, data types and lengths. Then apply the transform and you get a hbm.”
How exactly i’ve to apply the transform? wich tool i’ve to use to compile your example?

If i want to generate java classes from xml-schema with the needed annotation for hibernate the steps i’ve to do are:
xsd->hbm->java or there are other ways to do that?

Thanks a lot

ps: sorry for my bad english =)

Stefano

19 12 2006
Ian Boston

xlstproc xsl2hbm.xsl example.xsd

Should probably work

1 08 2011
Leyry Hynemonth

Hello,

The tow links are broken.
Can you update them ?
I’m working on the same problem.

Tank you.

1 08 2011
Ian

I think wordpress deleted them so I have put 2 up on GitHub as gists,

xsd2hbm.xsl https://gist.github.com/1118117
example.xsd https://gist.github.com/1118119

HTH
Ian

3 08 2011
Leyry Hynemonth

Thank you 😉
It makes me a good starting point

17 11 2011
Tanuj Batra

I think this xslt is specific to your xsd..right? Can you provide me the generic XSL which will work for all XSDs?

17 11 2011
Tanuj Batra

Is it possible to make such a generalized xsl?

17 11 2011
Ian

@Tanuj,
It was a long time ago I wrote this, so I may get things wrong. The XSL is a generic XSL. It assumes that the model described in the XSD is capable of defining a HBM type model. If thats the case then it coverts the XSD into HBM xml. If you want finer control over what is done, you can annotate the XSD with attributes to specify things like sets, lists, maps etc. Have a look through the example.xsd for xsd2hbm attributes. The example.xsd is the XSD for XPDL, and the XSL contains some (2 IIRC) references to it, however you could easily remove these.
HTH
Ian




%d bloggers like this: