我已经实现hashCode()
,并equals()
使用从NetBeans中默认的对象:
@Override
public int hashCode() {
int hash = 5;
hash = 37 * hash + this.unitSystemID;
return hash;
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
LOGGER.debug(getClass().toString());
LOGGER.debug(this.getClass().getClassLoader().toString());
LOGGER.debug(obj.getClass().toString());
LOGGER.debug(obj.getClass().getClassLoader().toString());
if (getClass() != obj.getClass()) {
return false;
}
final UnitSystem other = (UnitSystem) obj;
if (this.unitSystemID != other.unitSystemID) {
return false;
}
return true;
}
在日志记录检查点,我得到:
units.UnitSystem-com.utilities.domain.units.UnitSystem类
units.UnitSystem-org.springframework.boot.devtools.restart.classloader.RestartClassLoader@42d353e2
units.UnitSystem-类com.utilities.domain.units.UnitSystem _ $$ _ jvst6b1_19ed
units.UnitSystem-org.springframework.boot.devtools.restart.classloader.RestartClassLoader@42d353e2
相等性此时equals
将失败,并返回false。
还有什么_$$_jvst6b1_19ed
?它从何而来?
据我了解,如果这些类来自相同的类加载器,则它们应该相等。在其他任何我使用过的地方,这个实现都没有问题。为什么要getClass()
退回不同的东西?
除非您实际上UnitSystem
自己继承了子类,否则不需要精确的类匹配,因此请替换
if (getClass() != obj.getClass()) {
return false;
}
与
if (! (obj instanceof UnitSystem)) {
return false;
}
您不能创建UnitSystem
类,final
因为您希望Hibernate能够创建子类代理,因此您没有绝对的保证UnitSystem
不会被非Hibernate代码子类化,但是确实需要这样的绝对保证吗?
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句