是否有可能爲使用jaxb生成的類生成equals和compareTo方法,我使用jaxb從模式生成類。這些類實際上具有GUID,它們可以被唯一標識,但我如何實現一個equals/compare方法,以便集合類(如Set)能夠識別同一個實體的重複實例?是否有可能爲使用jaxb生成的類生成equals和compareTo方法
回答
好的,這是另一種方法。
您可以使用-XcodeInjector
插件添加hashCode
和equals
方法。
看到這個問題:
Inserting code with XJC+xsd+jxb using the options " -Xinject-code -extension "
喜歡的東西:
<jxb:bindings schemaLocation="schema.xsd">
<jxb:bindings node="/xs:schema/xs:complexType[@name='MyItemType']">
<ci:code>
@Override
public int hashCode() { return guid == null? 0 : guid.hashCode();}
</ci:code>
</jxb:bindings>
</jxb:bindings>
如果這還不夠好,考慮filing an issue in JAXB2-Basics( 「允許的hashCode /等於選擇屬性」 )或者實現你自己的插件。
謝謝,看起來像一個解決方案 –
免責聲明:我的jaxb2-basics
筆者提供能夠產生hashCode
和equals
方法JAXB2插件。
下面是使用例子Maven的:
<plugin>
<groupId>org.jvnet.jaxb2.maven2</groupId>
<artifactId>maven-jaxb2-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>generate</goal>
</goals>
</execution>
</executions>
<configuration>
<extension>true</extension>
<args>
<arg>-Xequals</arg>
<arg>-XhashCode</arg>
</args>
<plugins>
<plugin>
<groupId>org.jvnet.jaxb2_commons</groupId>
<artifactId>jaxb2-basics</artifactId>
<version>...</version>
</plugin>
</plugins>
</configuration>
</plugin>
(見documentation爲Ant)
您可以使用-XsimpleHashCode
和-XsimpleEquals
其產生運行時少hashCode
和equals
方法(散列碼或等於計算內聯)或-XhashCode
/-Xequals
,它們生成「戰略」hashCode
和equals
方法(將散列碼/等於計算委託給傳遞的策略方法) 。
這裏是-XsimpleHashCode
產生:
public class Customer {
...
public int hashCode() {
int currentHashCode = 1;
{
currentHashCode = (currentHashCode* 31);
String theAddress;
theAddress = this.getAddress();
if (theAddress!= null) {
currentHashCode += theAddress.hashCode();
}
}
{
currentHashCode = (currentHashCode* 31);
Boolean theBlueEyes;
theBlueEyes = this.isBlueEyes();
if (theBlueEyes!= null) {
currentHashCode += theBlueEyes.hashCode();
}
}
{
currentHashCode = (currentHashCode* 31);
String theFamilyName;
theFamilyName = this.getFamilyName();
if (theFamilyName!= null) {
currentHashCode += theFamilyName.hashCode();
}
}
{
currentHashCode = (currentHashCode* 31);
String theGivenName;
theGivenName = this.getGivenName();
if (theGivenName!= null) {
currentHashCode += theGivenName.hashCode();
}
}
{
currentHashCode = (currentHashCode* 31);
List<String> theMiddleInitials;
theMiddleInitials = (this.isSetMiddleInitials()?this.getMiddleInitials():null);
if (theMiddleInitials!= null) {
currentHashCode += theMiddleInitials.hashCode();
}
}
{
currentHashCode = (currentHashCode* 31);
String thePostCode;
thePostCode = this.getPostCode();
if (thePostCode!= null) {
currentHashCode += thePostCode.hashCode();
}
}
{
currentHashCode = (currentHashCode* 31);
boolean theSingle;
theSingle = this.isSingle();
currentHashCode += (theSingle? 1231 : 1237);
}
return currentHashCode;
}
}
這裏是-XhashCode
產生:
public class Customer implements HashCode
{
...
public int hashCode(ObjectLocator locator, HashCodeStrategy strategy) {
int currentHashCode = 1;
{
String theAddress;
theAddress = this.getAddress();
currentHashCode = strategy.hashCode(LocatorUtils.property(locator, "address", theAddress), currentHashCode, theAddress);
}
{
Boolean theBlueEyes;
theBlueEyes = this.isBlueEyes();
currentHashCode = strategy.hashCode(LocatorUtils.property(locator, "blueEyes", theBlueEyes), currentHashCode, theBlueEyes);
}
{
String theFamilyName;
theFamilyName = this.getFamilyName();
currentHashCode = strategy.hashCode(LocatorUtils.property(locator, "familyName", theFamilyName), currentHashCode, theFamilyName);
}
{
String theGivenName;
theGivenName = this.getGivenName();
currentHashCode = strategy.hashCode(LocatorUtils.property(locator, "givenName", theGivenName), currentHashCode, theGivenName);
}
{
List<String> theMiddleInitials;
theMiddleInitials = (this.isSetMiddleInitials()?this.getMiddleInitials():null);
currentHashCode = strategy.hashCode(LocatorUtils.property(locator, "middleInitials", theMiddleInitials), currentHashCode, theMiddleInitials);
}
{
String thePostCode;
thePostCode = this.getPostCode();
currentHashCode = strategy.hashCode(LocatorUtils.property(locator, "postCode", thePostCode), currentHashCode, thePostCode);
}
{
boolean theSingle;
theSingle = this.isSingle();
currentHashCode = strategy.hashCode(LocatorUtils.property(locator, "single", theSingle), currentHashCode, theSingle);
}
return currentHashCode;
}
public int hashCode() {
final HashCodeStrategy strategy = JAXBHashCodeStrategy.INSTANCE;
return this.hashCode(null, strategy);
}
}
從我的POV, 「戰略」 的版本更加強大。類實現HashCode
或Equals
接受定位符和散列碼/等於策略的接口。這使您可以控制哈希碼計算或從外部進行比較。我經常在單元測試中使用它,不僅僅是爲了檢查兩個對象是否等於,而且也是爲了找出它們在哪裏不同,完全一樣。
這兩個插件都生成無反射方法(這對性能至關重要)。他們還考慮JAXB特殊情況,如JAXBElement
s,原始數組等。
謝謝,但有點困惑,你使用jaxbbasics而不是jaxb或以及? –
JAXB2 Basics是JAXB/XJC的一組插件,您不能使用它們,而必須使用* JAXB/XJC。 – lexicore
好吧,我的類已經有一個uniqueId字段,所以有辦法使用該字段來生成equals和hashcode。 –
- 1. 是否有可能使用JAXB生成共享對象?
- 2. 使用JAXB可以生成超類型?
- 3. 使用equals()和hashCode()方法從DTD生成Java類
- 4. 是否有可能爲REST服務生成Web引用類?
- 5. netbeans能否爲映射自動生成正確的hashCode()和equals()方法?
- 6. 我們是否需要HyperJAXB生成的hashCode&equals方法?
- 7. 是否有可能無法使用npm 5生成package-lock.json?
- 8. 是否有可能生成ASMX
- 9. 是否有可能爲xsd簡單類型生成xml?
- 10. 是否有可能爲Java類自動生成Hibernate映射?
- 11. JAXB沒有爲包含生成的Java類的類生成所需的模式?
- 12. 如何在jaxws中使用wsimport生成equals()和hashcode()方法
- 13. JAXB生成可笑的奇怪和長的方法名稱
- 14. 由jaxb生成的ObjectFactory方法
- 15. 使用JAXB從XMLSchema.xsd中生成Java類
- 16. JAXB:使用本地schemaLocation生成Java類
- 17. 如何使用java.util.Optional生成JaxB類?
- 18. JAXB生成沒有註釋的java類
- 19. Intellij爲方法和類生成javadoc
- 20. 從模式生成jaxb類
- 21. 從xsd生成jaxb groovy類
- 22. JAXB不生成Java類
- 23. 變化包生成JAXB類
- 24. JAXB Bean生成
- 25. 是否可以更改爲equals()和hashCode()的IntelliJ的代碼生成模板
- 26. 是否有可能使eclipse生成hashCode並等於HashCodeBuilder和EqualsBuilder
- 27. 是否有可能從部分類型生成嚴格類型?
- 28. 是否有一個JAXB插件可以生成構建器?
- 29. 是否有可能設置類型的輸入生成的TextBoxFor
- 30. 是否有可能更改Silverlight RiaServices生成的DomainContexts的方式?
可能的重複http://stackoverflow.com/questions/6523497/how-to-generate-equals-and-hashcode-methods-using-wsimport-in-jaxws –
沒有什麼不同,因爲想創建基於這是班級中的一個特殊元素,而不是通過查看所有元素。目前我使用的解決方案是始終使用基於所述id的自定義比較器的TreeSet。 –