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.
解决方案
Advantages of Starting from POJOs
Easier to create a model that can be used with another mapping
technology (such as JPA or JSON-binding).
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).
Can add your own business logic on the domain model.
Easier to create a model that corresponds to your exact naming conventions.
Easy to leverage JAXB providers extensions (such as MOXy's @XmlPath mapping).
Advantages of Generating Model from XML Schema
Very easy to map a large XML schema to Java classes.
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.