JAXB unmarshalling to entities
I am working on a project involving an XML stream and converting that data to persisted data in my Database. So far I have gotten to the point where I can consume my webservice, I 开发者_StackOverflowgenerated some JAXB POJO's and have gotten unmarshaling of the XML stream started.
I am trying to figure out if there is a way to go right from XML to entities.
I know I can design my new database, generate some entities and then unmarshl the XML to the generated POJO's, then move the POJO data to entities and persist that. But that sounds pretty ridiculous. Anyone give me a hint where to look, I have not found anything googling the problem. I am using Netbeans 6.9.
You can create your own JAXB-annotated objects that are also Java Persistence API (JPA)-annotated objects. Using JPA you can persist those objects to your database. The benefit of keeping JAXB and JPA annotations on a single class is that any changes to your object only need to be made in that class, the UI representation, and an associated XSD
This does, however, in some ways break the ideas behind N tier architecture. You are effectively merging your business logic with your persistence tier. For the sake of removing duplication it might be a good idea, but occasionally you will find yourself using @Transient
and @XmlTransient
to put things only needed in XML or the database into the class without having them effect the areas where they aren't needed.
So it is possible, but you want to weigh the pros and cons before you do it. See also this question and this question for more thoughts on the trade-offs (all leaning to the side of putting both sets of annotations in one place).
This question covers some tools to minimize the effort of shuttling data between XML and a datastore via JAXB.
精彩评论