JAXB Custom Bindings HashCodes and Equals

来源:百度文库 编辑:神马文学网 时间:2024/10/04 17:52:14
http://zeus.riskfocusinc.com/portal/display/ART/JAXB+Custom+Bindings+HashCodes+and+Equals Added by Greg Bujak , last edited by Greg Bujak on Mar 24, 2009 09:06 DIV.auto_complete {BACKGROUND: #fff; WIDTH: 350px}DIV.auto_complete UL {BORDER-RIGHT: #888 1px solid; PADDING-RIGHT: 0px; BORDER-TOP: #888 1px solid; PADDING-LEFT: 0px; PADDING-BOTTOM: 0px; MARGIN: 0px; BORDER-LEFT: #888 1px solid; WIDTH: 100%; PADDING-TOP: 0px; BORDER-BOTTOM: #888 1px solid; LIST-STYLE-TYPE: none}DIV.auto_complete UL LI {PADDING-RIGHT: 3px; PADDING-LEFT: 3px; PADDING-BOTTOM: 3px; MARGIN: 0px; PADDING-TOP: 3px}DIV.auto_complete UL LI.selected {BACKGROUND-COLOR: #ffb}DIV.auto_complete UL STRONG.highlight {PADDING-RIGHT: 0px; PADDING-LEFT: 0px; PADDING-BOTTOM: 0px; MARGIN: 0px; COLOR: #800; PADDING-TOP: 0px}
Labels:  fpml, jaxb, technical, java

When using generated classes, it's natural to want to use these domain objects in some sort of Collection. The problem presented here is that the hashCode and equals methods do not get generated, resulting in an obvious problem.

I will describe two ways to resolve this problem

  • Custom Bindings
  • xjc plugin plugins

Custom Bindings

Part of the FpML specification includes a command based messaging model fpml-msg-4-4.xsd. Part of the spec defines a message id, I will be using this to demonstrate the custom binding functionality. Of course what this will be demonstrating is how to use a custom class as opposed to generated code.

First, declare the Class. So in this case, we will be implementing a custom MessageId, which will inherit from the FpML declared org.fpml.model.MessageId, remember that org.fpml.model is the package which has been defined plugin.

public class MessageId extends org.fpml.model.MessageId {                        private static final long serialVersionUID = 1L;                        @Override                        public boolean equals(Object obj) {                        if (!(obj instanceof MessageId)) {                        return false;                        }                        MessageId mId = (MessageId)obj;                        if (mId.value == null || this.value == null) {                        return false;                        }                        return this.value.equals(mId.value);                        }                        @Override                        public int hashCode() {                        if(value == null) return super.hashCode();                        return super.value.hashCode();                        }                        @Override                        public String toString(){                        return value == null ? super.toString() : getValue();                        }                        }

Nothing special has been done here, just a custom implementation of hashCode and equals.

Next define the xjb file. In this case I have called it fpml.xjb and added it local to the xsd location. While it's possible to put the xjb in a custom location, I have not tinkered enough to get it picked up unless its in the same directory as the schema.

                                                                                                

For the plugin registration, see FpML Integration with JAXB and Maven.

For external references on customization and JAXB in general, see

  • http://weblogs.java.net/blog/kohsuke/archive/community_java_web_services_and_xml/index.html
  • http://ozgwei.blogspot.com/2007/08/generating-developer-friendly-codes.html

The ozgwei post actually has a great discussion on how to bind dates.

xjc plugin plugins

Turns out, maven-jaxb2-plugin, supports a very rich feature set. Check out

  • https://jaxb2-commons.dev.java.net/
  • https://maven-jaxb2-plugin.dev.java.net/docs/guide.html#JAXB2Plugins

There are a number of items which need to be added to the configuration to successfully use the plug-in:

  • add the dependency to the pom for org.jvnet.jaxb2_commons.basic
  • add the for the desired methods
  • include the dependency explicitly in the plug-in
                        org.jvnet.jaxb2.maven2                        maven-jaxb2-plugin                        0.6.3                                                                                                generate                                                                                                -Xequals                        -XhashCode                        -Xcopyable                                                ...                                                                                                                                                org.jvnet.jaxb2_commons                        basic                        0.4.1