3

I am using jaxb to autogenerate java classes from an xsd file. I will need to persist the data that will be stored in the objects that will be instantiated from the classes. Is there some way that the hibernate code can be autogenerated in the same classes that are autogenerated by JAXB?

I will need to regenerate the classes many times in the course of development. If I have to write the hibernate code by hand, the only reasonable method I can imagine is to write separate classes with hibernate for persistence, and to write connector classes that migrate the autogenerated classes into the persistence classes. Otherwise, my hand-written hibernate code would be over-written every time I re-run jaxb based on fine tuning of the xsd file. If hibernate code were also autogenerated, I could end up using a lot fewer classes.

CodeMed
  • 9,527
  • 70
  • 212
  • 364

1 Answers1

2

Hyperjaxb3 is the way to go. It is a JAXB plugin that you include in the build process. When you run your xsd file through xjc, you will not only get xml-related annotations on the generated classes, you will also get JPA annotations. Without writing hardly any code, you will be able to take an XML document, persist it to a database, query the document from the database and get XML text back out. The generated code can be customized either in the xsd file or in an associated binding file (just like with vanilla xjc). We also utilize hbm2ddl on the produced classes to configure hibernate.

We have been actively using this on several schemas for the past couple of years.

As you edit your XML schema/bindings (and thus your DB schema), you will have to manually write a SQL migration script to upgrade any existing databases. It seems like most DBMS have a schema comparison tool that can be leveraged here. We automatically compare the freshly created schema to the migrated (from a baseline) schema on every build.

Rob
  • 6,247
  • 2
  • 25
  • 33
  • By the way, I moved the project to GitHub: https://github.com/highsource/hyperjaxb3 – lexicore Oct 08 '14 at 20:18
  • @CodeMed lexicore is the guy to get to help you (he is the author of hyperjaxb3). I will help if I can. – Rob Oct 09 '14 at 02:56
  • @Rob Thank you so much for the help you have given. I am encountering some problems getting the tutorial app into a new eclipse project. I took your suggestions and posted a step by step accounting of the things I am doing to try to get it into eclipse in another posting. Are you willing to take a look at the other posting? Here is the link: http://stackoverflow.com/questions/26286055/importing-hyperjaxb-purchase-order-tutorial-into-eclipse – CodeMed Oct 09 '14 at 20:50