2

What is props/cons of manual class writing and generation of JAXB classes from scheme. I am starting new project and would like to understand which way to choose to avid pitfalls.

I have complex structure of XML (basicly it is FreeSwitch configuration definition) with included repeatable types on different XML levels.

Andrii Liubimov
  • 497
  • 4
  • 12

3 Answers3

1

IMPO classes generation from schema should be used to avoid any discrepancy between schema and java classes.

As the schema will be shared between interfacing entities so it imperative for both the parties to have the code confirming to schema.

Juned Ahsan
  • 67,789
  • 12
  • 98
  • 136
1

Advantages of Starting from POJOs

  1. Easier to create a model that can be used with another mapping technology (such as JPA or JSON-binding).
  2. Can leverage @XmlElementWrapper on collection properties instead of a separate class being generated for that layer (see: How generate XMLElementWrapper annotation with xjc and customized binding).
  3. Can add your own business logic on the domain model.
  4. Specifying an XmlAdapter is cleaner (compare with http://blog.bdoughan.com/2011/08/xml-schema-to-java-generating.html).
  5. Easier to create a model that corresponds to your exact naming conventions.
  6. Easy to leverage JAXB providers extensions (such as MOXy's @XmlPath mapping).

Advantages of Generating Model from XML Schema

  1. Very easy to map a large XML schema to Java classes.
  2. You can greater confidence that your Java model corresponds exactly to your XML schema.

Mixing the Two

With JAXB it does not need to be a one or the other type choice. You can use an external bindings file so that during clas generation JAXB will use classes that you wrote for certain complex types and then have the rest of the generated model point to these.

Example


UPDATE

What about using constructors in JAXB ? As I know it is not allowed classes without puclic constructors. It is mean that I can't do some fields required. Is there proper way to deal with required fields ?

JAXB requires a no-arg constructor which can be private. If your class doesn't have a no-arg constructor then you can use an XmlAdapter.

Community
  • 1
  • 1
bdoughan
  • 147,609
  • 23
  • 300
  • 400
  • What about using constructors in JAXB ? As I know it is not allowed classes without puclic constructors. It is mean that I can't do some fields required. Is there proper way to deal with required fields ? – Andrii Liubimov Jan 15 '14 at 13:44
  • @AndriiLiubimov - I have updated my answer to address your comment. – bdoughan Jan 15 '14 at 14:25
0

JAXB generated classes are value classes and they have no functionality. In order to add any functionality, you should create your own classes using the generated beans. You can also extend or modify the generated beans which I don't recommend. I think this is one of the main problems.

On the other hand, generated schemas from manually annotated beans may not include all the needed constraints.

That's why I think there's no best one. You can choose both based on your needs.

Valeh Hajiyev
  • 3,216
  • 4
  • 19
  • 28