在Java中交叉引用类上覆盖equals / hashCode会导致StackOverflowError
作者:互联网
我有两个代表两个不同数据库实体的类.他们在db中的关系为1:m,它在类结构中表示如下:
public class Company {
private List<Employee> employees;
public List<Employee> getEmployees() {
return employees;
}
public void setEmployees(List<Employee> employees) {
this.employees = employees;
}
}
public class Employee {
private Company company;
public Company getCompany() {
return company;
}
public void setCompany(Company company) {
this.company = company;
}
}
现在我想在这些类上重写equals / hashCode. Eclipse为我生成以下代码:
public class Company {
private List<Employee> employees;
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((employees == null) ? 0 : employees.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Company other = (Company) obj;
if (employees == null) {
if (other.employees != null)
return false;
} else if (!employees.equals(other.employees))
return false;
return true;
}
}
public class Employee {
private Company company;
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((company == null) ? 0 : company.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Employee other = (Employee) obj;
if (company == null) {
if (other.company != null)
return false;
} else if (!company.equals(other.company))
return false;
return true;
}
}
如果我运行以下测试:
public class EqualsTest {
@Test
public void testEquals() {
Company company1 = new Company();
Employee employee1 = new Employee();
employee1.setCompany(company1);
company1.setEmployees(Arrays.asList(employee1));
Company company2 = new Company();
Employee employee2 = new Employee();
employee2.setCompany(company2);
company2.setEmployees(Arrays.asList(employee2));
assertThat(company1, is(company2));
}
}
我希望它能够通过,因为company1和company2都有相同的员工列表,但是它因StackOverflowError而失败:
java.lang.StackOverflowError
at java.util.AbstractList$Itr.<init>(AbstractList.java:318)
at java.util.AbstractList$Itr.<init>(AbstractList.java:318)
at java.util.AbstractList$ListItr.<init>(AbstractList.java:377)
at java.util.AbstractList.listIterator(AbstractList.java:315)
at java.util.AbstractList.listIterator(AbstractList.java:284)
at java.util.AbstractList.equals(AbstractList.java:502)
at com.test.Company.equals(Company.java:37)
at com.test.Employee.equals(Employee.java:35)
at java.util.AbstractList.equals(AbstractList.java:507)
at com.test.Company.equals(Company.java:37)
at com.test.Employee.equals(Employee.java:35)
at java.util.AbstractList.equals(AbstractList.java:507)
at com.test.Company.equals(Company.java:37)
at com.test.Employee.equals(Employee.java:35)
...
我知道这种失败的原因是类中的交叉引用,因此是equals / hashCode方法.但是我应该如何实现equals / hashCode以避免不定式递归?
解决方法:
就像现在一样,公司的身份完全由其员工定义.同样,员工的身份仅由其公司定义.你知道这是如何导致相互逻辑依赖的吗?
您需要在代码中打破逻辑依赖性.您如何在逻辑上唯一地识别公司和员工?通常,您可以使用某种有意义的唯一标识符来执行此操作:名称(字符串),数字(int / long)或某些类似的基本字段组合.
标签:java,equals,stack-overflow,cross-reference 来源: https://codeday.me/bug/20190726/1539452.html