Preventing Netbeans JAXB generation trashing classes
I'm developing a SOAP service using JAX-WS and JAXB under Netbeans 6.8, and getting 开发者_如何学JAVAa little frustrated with Netbeans trashing my work every time the XSD schema my JAXB bindings are based upon changes.
To elaborate, the IDE automatically generates classes bound to the schema, which can then be (un)marshalled from/to XML using JAXB. To these classes I've added extra methods to (for example) convert to and from separate classes designed to be persisted to database with JPA. The problem is that whenever the schema changes and I rebuild, these classes are regenerated, and all my custom methods are deleted. I can manually replace them by copy-pasting from a backup file, but that is rather time-consuming and tedious. As I'm using an iterative design approach, the schema is changing rather frequently and I'm wasting an awful lot of time whenever it does, simply to reinstate my previous code.
While the IDE automatically regenerating the JAXB-bound classes is entirely reasonable and I don't mean to imply otherwise, I was wondering if anyone had any bright ideas as to how to prevent my extra work having to be manually reinstated every time my schema changes?
Making modifications to the XJC-generate source isn't really a good idea, for reasons that you've discovered. You should either use a binding customization or XJC to plugin to generate the additional code that you need, or else move your additional code out of the XJC-generate code and into separate source files.
If your additional code is there to convert between JAXB and JPA class models, then it can probably stand on its own as a distinct translation layer. It's not very OO that way, but it'll get around your problem.
Alternatively, there's an XJC plugin that is supposed to let you preserve code that's been manually added to the generated source, but it's poorly documented (and I haven't used it myself). You might have to dog around on http://jaxb.dev.java.net/ to find out how to use it.
Instead of modifying the generated classes to add methods - extend the generated classes and put your additional methods in your classes derived from the generated classes.
精彩评论