Last active
August 29, 2015 14:06
-
-
Save alexsnaps/f46a87de25c8e41848a5 to your computer and use it in GitHub Desktop.
Bi-directional associations
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Address { | |
private final int id; | |
public Set<Employee> employees = new HashSet<Employee>(); | |
public Address(final int id) { | |
this.id = id; | |
} | |
public void removeEmployee(final Employee employee) { | |
if(employees.remove(employee)) { | |
employee.setAddress(null); | |
} | |
} | |
public void addEmployee(final Employee employee) { | |
if(employees.add(employee)) { | |
employee.setAddress(this); | |
} | |
} | |
public void setEmployees(Set<Employee> employees) { | |
for(Employee e : this.employees) { | |
if(!employess.contains(e)) { | |
removeEmployee(e); | |
} | |
} | |
for(Employee e : employees) { | |
addEmployee(e); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Employee { | |
private final int id; | |
public Address address; | |
public Employee(final int id) { | |
this.id = id; | |
} | |
public void setAddress(Address newAddress) { | |
if (address != newAddress) { | |
if(this.address != null) { | |
address.removeEmployee(this); | |
} | |
this.address = newAddress; | |
if(newAddress != null) { | |
newAddress.addEmployee(this); | |
} | |
} | |
} | |
@Override | |
public String toString() { | |
return "[Employee@" + id + "]"; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static void main(String[] args) { | |
Employee employee=new Employee(1); | |
Employee employee2=new Employee(2); | |
Address address=new Address(12); | |
Address address3=new Address(34); | |
address.employees.add(employee); | |
address.employees.add(employee2); | |
// MANUALLY SETTING THE OTHER END | |
employee.address = address; | |
employee2.address = address; | |
System.out.println(address.employees); // still lists the 2 employees | |
System.out.println(address3.employees); // lists nothing | |
// Moving the employees, doesn't update the set! | |
// employee.address = address3; | |
// employee2.address = address3; | |
// | |
// System.out.println(address.employees); // still lists the employees | |
// System.out.println(address3.employees); // lists nothing | |
// BUT | |
employee.setAddress(address3); | |
employee2.setAddress(address3); | |
System.out.println(address.employees); // No one here | |
System.out.println(address3.employees); // Works here notw | |
// WORKS BOTH WAYS | |
address.addEmployee(employee); | |
address.addEmployee(employee2); | |
System.out.println(address.employees); // Back here | |
System.out.println(address3.employees); // no one here | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment